From 417ed0a8ee2ee4ef6e0c6e4d776e29adc51010bc Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Fri, 11 Jun 2021 23:40:35 +0200 Subject: [PATCH] Refactor views --- Apple TV/ChannelView.swift | 29 +++-------------- Apple TV/PopularVideosView.swift | 27 ++++----------- Apple TV/SearchView.swift | 25 +++++++++++--- Apple TV/SearchedVideosView.swift | 47 --------------------------- Apple TV/VideosView.swift | 46 ++++++++++++++++++++++++++ Model/AppState.swift | 2 +- Pearvidious.xcodeproj/project.pbxproj | 8 ++--- Shared/ContentView.swift | 2 +- 8 files changed, 83 insertions(+), 103 deletions(-) delete mode 100644 Apple TV/SearchedVideosView.swift create mode 100644 Apple TV/VideosView.swift diff --git a/Apple TV/ChannelView.swift b/Apple TV/ChannelView.swift index 05420010..c3d962e8 100644 --- a/Apple TV/ChannelView.swift +++ b/Apple TV/ChannelView.swift @@ -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() -// } -// } diff --git a/Apple TV/PopularVideosView.swift b/Apple TV/PopularVideosView.swift index 52090238..4d865fa6 100644 --- a/Apple TV/PopularVideosView.swift +++ b/Apple TV/PopularVideosView.swift @@ -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 } } diff --git a/Apple TV/SearchView.swift b/Apple TV/SearchView.swift index 8e9ba4b6..f80dc2a2 100644 --- a/Apple TV/SearchView.swift +++ b/Apple TV/SearchView.swift @@ -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 } } diff --git a/Apple TV/SearchedVideosView.swift b/Apple TV/SearchedVideosView.swift deleted file mode 100644 index bd1aa619..00000000 --- a/Apple TV/SearchedVideosView.swift +++ /dev/null @@ -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() -// } -// } diff --git a/Apple TV/VideosView.swift b/Apple TV/VideosView.swift new file mode 100644 index 00000000..9cf6f71e --- /dev/null +++ b/Apple TV/VideosView.swift @@ -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) + } +} diff --git a/Model/AppState.swift b/Model/AppState.swift index 70113d7d..57c9c941 100644 --- a/Model/AppState.swift +++ b/Model/AppState.swift @@ -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 diff --git a/Pearvidious.xcodeproj/project.pbxproj b/Pearvidious.xcodeproj/project.pbxproj index d76f319f..3f94a91e 100644 --- a/Pearvidious.xcodeproj/project.pbxproj +++ b/Pearvidious.xcodeproj/project.pbxproj @@ -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 = ""; }; 37AAF27F26737550007FC770 /* SearchView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchView.swift; sourceTree = ""; }; 37AAF2812673791F007FC770 /* SearchedVideosProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchedVideosProvider.swift; sourceTree = ""; }; - 37AAF28726737A13007FC770 /* SearchedVideosView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchedVideosView.swift; sourceTree = ""; }; 37AAF2892673AB89007FC770 /* ChannelView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelView.swift; sourceTree = ""; }; 37AAF28B2673ABD3007FC770 /* ChannelVideosProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelVideosProvider.swift; sourceTree = ""; }; 37AAF28F26740715007FC770 /* AppState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppState.swift; sourceTree = ""; }; 37AAF2932674086B007FC770 /* TabSelection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabSelection.swift; sourceTree = ""; }; + 37AAF29926740A01007FC770 /* VideosView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VideosView.swift; sourceTree = ""; }; 37D4B0C22671614700C925CA /* PearvidiousApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PearvidiousApp.swift; sourceTree = ""; }; 37D4B0C32671614700C925CA /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; 37D4B0C42671614800C925CA /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; @@ -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 */, diff --git a/Shared/ContentView.swift b/Shared/ContentView.swift index 66095bed..77a3465d 100644 --- a/Shared/ContentView.swift +++ b/Shared/ContentView.swift @@ -18,7 +18,7 @@ struct ContentView: View { .tag(TabSelection.channel) } - SearchView() + SearchView(state: state, tabSelection: $tabSelection) .tabItem { Image(systemName: "magnifyingglass") } .tag(TabSelection.search) }