Refactor views

This commit is contained in:
Arkadiusz Fal 2021-06-11 23:40:35 +02:00
parent 314c3b4968
commit 417ed0a8ee
8 changed files with 83 additions and 103 deletions

View File

@ -7,26 +7,12 @@ struct ChannelView: View {
@Binding var tabSelection: TabSelection
var body: some View {
Group {
List {
ForEach(videos) { video in
VideoThumbnailView(video: video)
.contextMenu {
Button("Close \(video.author) channel", action: {
state.closeChannel()
tabSelection = .popular
})
}
.listRowInsets(listRowInsets)
VideosView(state: state, tabSelection: $tabSelection, videos: videos)
.task {
async {
provider.load()
}
}
.listStyle(GroupedListStyle())
}
.task {
async {
provider.load()
}
}
}
var listRowInsets: EdgeInsets {
@ -43,10 +29,3 @@ struct ChannelView: View {
return provider.videos
}
}
//
// struct ChannelView_Previews: PreviewProvider {
// static var previews: some View {
// ChannelView()
// }
// }

View File

@ -3,32 +3,19 @@ import SwiftUI
struct PopularVideosView: View {
@ObservedObject private var provider = PopularVideosProvider()
@ObservedObject var state: AppState
@Binding var tabSelection: TabSelection
var body: some View {
Group {
List {
ForEach(provider.videos) { video in
VideoThumbnailView(video: video)
.contextMenu {
Button("\(video.author) Channel", action: {
state.setChannel(from: video)
tabSelection = .channel
})
}
.listRowInsets(listRowInsets)
VideosView(state: state, tabSelection: $tabSelection, videos: videos)
.task {
async {
provider.load()
}
}
.listStyle(GroupedListStyle())
}
.task {
async {
provider.load()
}
}
}
var listRowInsets: EdgeInsets {
EdgeInsets(top: .zero, leading: .zero, bottom: .zero, trailing: 30)
var videos: [Video] {
return provider.videos
}
}

View File

@ -2,17 +2,32 @@ import SwiftUI
struct SearchView: View {
@ObservedObject private var provider = SearchedVideosProvider()
@ObservedObject var state: AppState
@Binding var tabSelection: TabSelection
@State var query = ""
var body: some View {
SearchedVideosView(provider: provider, query: $query)
VideosView(state: state, tabSelection: $tabSelection, videos: videos)
.searchable(text: $query)
}
}
var videos: [Video] {
var newQuery = query
struct SearchView_Previews: PreviewProvider {
static var previews: some View {
SearchView()
if let url = URLComponents(string: query),
let queryItem = url.queryItems?.first(where: { item in item.name == "v" }),
let id = queryItem.value
{
newQuery = id
}
if newQuery != provider.query {
provider.query = newQuery
provider.load()
}
return provider.videos
}
}

View File

@ -1,47 +0,0 @@
import SwiftUI
struct SearchedVideosView: View {
@ObservedObject var provider = SearchedVideosProvider()
@Binding var query: String
var body: some View {
Group {
List {
ForEach(videos) { video in
VideoThumbnailView(video: video)
.listRowInsets(listRowInsets)
}
}
.listStyle(GroupedListStyle())
}
}
var listRowInsets: EdgeInsets {
EdgeInsets(top: .zero, leading: .zero, bottom: .zero, trailing: 30)
}
var videos: [Video] {
var newQuery = query
if let url = URLComponents(string: query),
let queryItem = url.queryItems?.first(where: { item in item.name == "v" }),
let id = queryItem.value
{
newQuery = id
}
if newQuery != provider.query {
provider.query = newQuery
provider.load()
}
return provider.videos
}
}
// struct SearchedVideosView_Previews: PreviewProvider {
// static var previews: some View {
// SearchedVideosView()
// }
// }

46
Apple TV/VideosView.swift Normal file
View File

@ -0,0 +1,46 @@
import SwiftUI
struct VideosView: View {
@ObservedObject var state: AppState
@Binding var tabSelection: TabSelection
var videos: [Video]
var body: some View {
Group {
List {
ForEach(videos) { video in
VideoThumbnailView(video: video)
.contextMenu {
if state.showingChannel {
closeChannelButton(name: video.author)
} else {
openChannelButton(from: video)
}
}
.listRowInsets(listRowInsets)
}
}
.listStyle(GroupedListStyle())
}
}
func closeChannelButton(name: String) -> some View {
Button("Close \(name) Channel", action: {
state.closeChannel()
tabSelection = .popular
})
}
func openChannelButton(from video: Video) -> some View {
Button("\(video.author) Channel", action: {
state.openChannel(from: video)
tabSelection = .channel
})
}
var listRowInsets: EdgeInsets {
EdgeInsets(top: .zero, leading: .zero, bottom: .zero, trailing: 30)
}
}

View File

@ -5,7 +5,7 @@ class AppState: ObservableObject {
@Published var channelID: String?
@Published var channel: String?
func setChannel(from video: Video) {
func openChannel(from video: Video) {
channel = video.author
channelID = video.channelID
showingChannel = true

View File

@ -12,7 +12,6 @@
37AAF2822673791F007FC770 /* SearchedVideosProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF2812673791F007FC770 /* SearchedVideosProvider.swift */; };
37AAF2832673791F007FC770 /* SearchedVideosProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF2812673791F007FC770 /* SearchedVideosProvider.swift */; };
37AAF2842673791F007FC770 /* SearchedVideosProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF2812673791F007FC770 /* SearchedVideosProvider.swift */; };
37AAF28826737A13007FC770 /* SearchedVideosView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF28726737A13007FC770 /* SearchedVideosView.swift */; };
37AAF28A2673AB89007FC770 /* ChannelView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF2892673AB89007FC770 /* ChannelView.swift */; };
37AAF28C2673ABD3007FC770 /* ChannelVideosProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF28B2673ABD3007FC770 /* ChannelVideosProvider.swift */; };
37AAF28D2673ABD3007FC770 /* ChannelVideosProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF28B2673ABD3007FC770 /* ChannelVideosProvider.swift */; };
@ -23,6 +22,7 @@
37AAF2942674086B007FC770 /* TabSelection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF2932674086B007FC770 /* TabSelection.swift */; };
37AAF2952674086B007FC770 /* TabSelection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF2932674086B007FC770 /* TabSelection.swift */; };
37AAF2962674086B007FC770 /* TabSelection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF2932674086B007FC770 /* TabSelection.swift */; };
37AAF29A26740A01007FC770 /* VideosView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AAF29926740A01007FC770 /* VideosView.swift */; };
37D4B0D92671614900C925CA /* Tests_iOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37D4B0D82671614900C925CA /* Tests_iOS.swift */; };
37D4B0E32671614900C925CA /* Tests_macOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37D4B0E22671614900C925CA /* Tests_macOS.swift */; };
37D4B0E42671614900C925CA /* PearvidiousApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37D4B0C22671614700C925CA /* PearvidiousApp.swift */; };
@ -84,11 +84,11 @@
37AAF27D26737323007FC770 /* PopularVideosView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PopularVideosView.swift; sourceTree = "<group>"; };
37AAF27F26737550007FC770 /* SearchView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchView.swift; sourceTree = "<group>"; };
37AAF2812673791F007FC770 /* SearchedVideosProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchedVideosProvider.swift; sourceTree = "<group>"; };
37AAF28726737A13007FC770 /* SearchedVideosView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchedVideosView.swift; sourceTree = "<group>"; };
37AAF2892673AB89007FC770 /* ChannelView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelView.swift; sourceTree = "<group>"; };
37AAF28B2673ABD3007FC770 /* ChannelVideosProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelVideosProvider.swift; sourceTree = "<group>"; };
37AAF28F26740715007FC770 /* AppState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppState.swift; sourceTree = "<group>"; };
37AAF2932674086B007FC770 /* TabSelection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabSelection.swift; sourceTree = "<group>"; };
37AAF29926740A01007FC770 /* VideosView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VideosView.swift; sourceTree = "<group>"; };
37D4B0C22671614700C925CA /* PearvidiousApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PearvidiousApp.swift; sourceTree = "<group>"; };
37D4B0C32671614700C925CA /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
37D4B0C42671614800C925CA /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
@ -220,8 +220,8 @@
37AAF27D26737323007FC770 /* PopularVideosView.swift */,
37AAF2892673AB89007FC770 /* ChannelView.swift */,
37AAF27F26737550007FC770 /* SearchView.swift */,
37AAF28726737A13007FC770 /* SearchedVideosView.swift */,
37D4B1822671681B00C925CA /* PlayerView.swift */,
37AAF29926740A01007FC770 /* VideosView.swift */,
37D4B18B26717B3800C925CA /* VideoThumbnailView.swift */,
37D4B1AE26729DEB00C925CA /* Info.plist */,
37D4B15E267164AF00C925CA /* Assets.xcassets */,
@ -533,13 +533,13 @@
37AAF28026737550007FC770 /* SearchView.swift in Sources */,
37D4B19526717CE100C925CA /* PopularVideosProvider.swift in Sources */,
37D4B1842671684E00C925CA /* PlayerView.swift in Sources */,
37AAF28826737A13007FC770 /* SearchedVideosView.swift in Sources */,
37D4B1802671650A00C925CA /* PearvidiousApp.swift in Sources */,
37D4B1B22672A01000C925CA /* DataProvider.swift in Sources */,
37AAF29226740715007FC770 /* AppState.swift in Sources */,
37D4B18E26717B3800C925CA /* VideoThumbnailView.swift in Sources */,
37D4B1B62672A30700C925CA /* VideoDetailsProvider.swift in Sources */,
37AAF27E26737323007FC770 /* PopularVideosView.swift in Sources */,
37AAF29A26740A01007FC770 /* VideosView.swift in Sources */,
37AAF2962674086B007FC770 /* TabSelection.swift in Sources */,
37AAF28A2673AB89007FC770 /* ChannelView.swift in Sources */,
37AAF28E2673ABD3007FC770 /* ChannelVideosProvider.swift in Sources */,

View File

@ -18,7 +18,7 @@ struct ContentView: View {
.tag(TabSelection.channel)
}
SearchView()
SearchView(state: state, tabSelection: $tabSelection)
.tabItem { Image(systemName: "magnifyingglass") }
.tag(TabSelection.search)
}