From ef5c75a2bca1697f8e4d77714793879c44e5085b Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Wed, 29 Sep 2021 13:45:00 +0200 Subject: [PATCH] Previews environment objects fixtures --- Fixtures/Video+Fixtures.swift | 9 +++++++++ Fixtures/View+Fixtures.swift | 22 ++++++++++++++++++++++ Model/InvidiousAPI.swift | 4 ++-- Pearvidious.xcodeproj/project.pbxproj | 8 ++++++++ Shared/Navigation/ContentView.swift | 1 + Shared/Player/VideoDetails.swift | 2 +- Shared/Player/VideoPlayerView.swift | 2 +- Shared/Playlists/AddToPlaylistView.swift | 4 +--- Shared/Playlists/PlaylistsView.swift | 2 +- Shared/Settings/SettingsView.swift | 6 +----- Shared/Trending/TrendingView.swift | 8 +++----- Shared/Videos/VideosCellsHorizontal.swift | 3 +-- Shared/Videos/VideosCellsVertical.swift | 3 +-- Shared/Views/SearchView.swift | 17 +++++++++++------ Shared/Views/SignInRequiredView.swift | 9 ++++++--- Shared/Watch Now/WatchNowView.swift | 3 +-- macOS/Settings/InstancesSettingsView.swift | 2 +- tvOS/TVNavigationView.swift | 6 +----- 18 files changed, 72 insertions(+), 39 deletions(-) create mode 100644 Fixtures/View+Fixtures.swift diff --git a/Fixtures/Video+Fixtures.swift b/Fixtures/Video+Fixtures.swift index 84bbb3a6..3df29b11 100644 --- a/Fixtures/Video+Fixtures.swift +++ b/Fixtures/Video+Fixtures.swift @@ -47,4 +47,13 @@ extension Video { static var allFixtures: [Video] { [fixture, fixtureLiveWithoutPublishedOrViews, fixtureUpcomingWithoutPublishedOrViews] } + + static func fixtures(_ count: Int) -> [Video] { + var result = [Video]() + while result.count < count { + result.append(allFixtures.shuffled().first!) + } + + return result + } } diff --git a/Fixtures/View+Fixtures.swift b/Fixtures/View+Fixtures.swift new file mode 100644 index 00000000..c82e79f5 --- /dev/null +++ b/Fixtures/View+Fixtures.swift @@ -0,0 +1,22 @@ +import Foundation +import SwiftUI + +struct FixtureEnvironmentObjectsModifier: ViewModifier { + func body(content: Content) -> some View { + content + .environmentObject(InstancesModel()) + .environmentObject(InvidiousAPI()) + .environmentObject(NavigationModel()) + .environmentObject(PlaybackModel()) + .environmentObject(PlaylistsModel()) + .environmentObject(RecentsModel()) + .environmentObject(SearchModel()) + .environmentObject(SubscriptionsModel(api: InvidiousAPI())) + } +} + +extension View { + func injectFixtureEnvironmentObjects() -> some View { + modifier(FixtureEnvironmentObjectsModifier()) + } +} diff --git a/Model/InvidiousAPI.swift b/Model/InvidiousAPI.swift index 46ef7a7e..d8787d31 100644 --- a/Model/InvidiousAPI.swift +++ b/Model/InvidiousAPI.swift @@ -8,8 +8,8 @@ final class InvidiousAPI: Service, ObservableObject { @Published var account: Instance.Account! = .empty - @Published var validInstance = true - @Published var signedIn = true + @Published var validInstance = false + @Published var signedIn = false func setAccount(_ account: Instance.Account) { self.account = account diff --git a/Pearvidious.xcodeproj/project.pbxproj b/Pearvidious.xcodeproj/project.pbxproj index 468823a6..36a717d8 100644 --- a/Pearvidious.xcodeproj/project.pbxproj +++ b/Pearvidious.xcodeproj/project.pbxproj @@ -265,6 +265,9 @@ 37FD43DE2704717F0073EE42 /* DefaultAccountHint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37FD43DD2704717F0073EE42 /* DefaultAccountHint.swift */; }; 37FD43DF2704717F0073EE42 /* DefaultAccountHint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37FD43DD2704717F0073EE42 /* DefaultAccountHint.swift */; }; 37FD43E02704717F0073EE42 /* DefaultAccountHint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37FD43DD2704717F0073EE42 /* DefaultAccountHint.swift */; }; + 37FD43E32704847C0073EE42 /* View+Fixtures.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37FD43E22704847C0073EE42 /* View+Fixtures.swift */; }; + 37FD43E42704847C0073EE42 /* View+Fixtures.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37FD43E22704847C0073EE42 /* View+Fixtures.swift */; }; + 37FD43E52704847C0073EE42 /* View+Fixtures.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37FD43E22704847C0073EE42 /* View+Fixtures.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -402,6 +405,7 @@ 37F64FE326FE70A60081B69E /* RedrawOnViewModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RedrawOnViewModifier.swift; sourceTree = ""; }; 37FD43DB270470B70073EE42 /* InstancesSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstancesSettingsView.swift; sourceTree = ""; }; 37FD43DD2704717F0073EE42 /* DefaultAccountHint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultAccountHint.swift; sourceTree = ""; }; + 37FD43E22704847C0073EE42 /* View+Fixtures.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+Fixtures.swift"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -554,6 +558,7 @@ 37F49BA226CAA59B00304AC0 /* Playlist+Fixtures.swift */, 3748186926A764FB0084E870 /* Thumbnail+Fixtures.swift */, 3748186526A7627F0084E870 /* Video+Fixtures.swift */, + 37FD43E22704847C0073EE42 /* View+Fixtures.swift */, ); path = Fixtures; sourceTree = ""; @@ -1073,6 +1078,7 @@ 378E50FF26FE8EEE00F49626 /* AccountsMenuView.swift in Sources */, 37C7A1DA267CACF50010EAD6 /* TrendingCountry.swift in Sources */, 37977583268922F600DD52A8 /* InvidiousAPI.swift in Sources */, + 37FD43E32704847C0073EE42 /* View+Fixtures.swift in Sources */, 37BE0BD626A1D4A90092E2DB /* PlayerViewController.swift in Sources */, 37BA793F26DB8F97002A0235 /* ChannelVideosView.swift in Sources */, 37C194C726F6A9C8005D3B96 /* RecentsModel.swift in Sources */, @@ -1208,6 +1214,7 @@ 37B767DC2677C3CA0098BAA8 /* PlayerModel.swift in Sources */, 3797758C2689345500DD52A8 /* Store.swift in Sources */, 37141674267A8E10006CA35D /* Country.swift in Sources */, + 37FD43E42704847C0073EE42 /* View+Fixtures.swift in Sources */, 37AAF2A126741C97007FC770 /* SubscriptionsView.swift in Sources */, 37732FF12703A26300F04329 /* ValidationStatusView.swift in Sources */, 37BA794C26DC30EC002A0235 /* AppSidebarPlaylists.swift in Sources */, @@ -1310,6 +1317,7 @@ 379775952689365600DD52A8 /* Array+Next.swift in Sources */, 3705B180267B4DFB00704544 /* TrendingCountry.swift in Sources */, 373CFACD26966264003CB2C6 /* SearchQuery.swift in Sources */, + 37FD43E52704847C0073EE42 /* View+Fixtures.swift in Sources */, 37141675267A8E10006CA35D /* Country.swift in Sources */, 37152EEC26EFEB95004FB96D /* LazyView.swift in Sources */, 37484C2726FC83E000287258 /* InstanceFormView.swift in Sources */, diff --git a/Shared/Navigation/ContentView.swift b/Shared/Navigation/ContentView.swift index a3be2539..990d1312 100644 --- a/Shared/Navigation/ContentView.swift +++ b/Shared/Navigation/ContentView.swift @@ -61,5 +61,6 @@ struct ContentView: View { struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() + .injectFixtureEnvironmentObjects() } } diff --git a/Shared/Player/VideoDetails.swift b/Shared/Player/VideoDetails.swift index 16b2067a..9d10a29f 100644 --- a/Shared/Player/VideoDetails.swift +++ b/Shared/Player/VideoDetails.swift @@ -186,6 +186,6 @@ struct VideoDetails: View { struct VideoDetails_Previews: PreviewProvider { static var previews: some View { VideoDetails(video: Video.fixture) - .environmentObject(SubscriptionsModel()) + .injectFixtureEnvironmentObjects() } } diff --git a/Shared/Player/VideoPlayerView.swift b/Shared/Player/VideoPlayerView.swift index 1e09a8af..e818fdab 100644 --- a/Shared/Player/VideoPlayerView.swift +++ b/Shared/Player/VideoPlayerView.swift @@ -109,7 +109,7 @@ struct VideoPlayerView_Previews: PreviewProvider { } .sheet(isPresented: .constant(true)) { VideoPlayerView(Video.fixture) - .environmentObject(NavigationModel()) + .injectFixtureEnvironmentObjects() } } } diff --git a/Shared/Playlists/AddToPlaylistView.swift b/Shared/Playlists/AddToPlaylistView.swift index 69498611..11b40318 100644 --- a/Shared/Playlists/AddToPlaylistView.swift +++ b/Shared/Playlists/AddToPlaylistView.swift @@ -171,8 +171,6 @@ struct AddToPlaylistView: View { struct AddToPlaylistView_Previews: PreviewProvider { static var previews: some View { AddToPlaylistView(video: Video.fixture) - .environmentObject(PlaylistsModel([Playlist.fixture])) - .environmentObject(SubscriptionsModel()) - .environmentObject(NavigationModel()) + .injectFixtureEnvironmentObjects() } } diff --git a/Shared/Playlists/PlaylistsView.swift b/Shared/Playlists/PlaylistsView.swift index 840c8547..5bc0fff7 100644 --- a/Shared/Playlists/PlaylistsView.swift +++ b/Shared/Playlists/PlaylistsView.swift @@ -220,6 +220,6 @@ struct PlaylistsView: View { struct PlaylistsView_Provider: PreviewProvider { static var previews: some View { PlaylistsView() - .environmentObject(NavigationModel()) + .injectFixtureEnvironmentObjects() } } diff --git a/Shared/Settings/SettingsView.swift b/Shared/Settings/SettingsView.swift index bd3f314c..f68a1de8 100644 --- a/Shared/Settings/SettingsView.swift +++ b/Shared/Settings/SettingsView.swift @@ -65,11 +65,7 @@ struct SettingsView: View { struct SettingsView_Previews: PreviewProvider { static var previews: some View { SettingsView() - .environmentObject(InstancesModel()) - .environmentObject(InvidiousAPI()) - .environmentObject(NavigationModel()) - .environmentObject(SearchModel()) - .environmentObject(SubscriptionsModel()) + .injectFixtureEnvironmentObjects() #if os(macOS) .frame(width: 600, height: 300) #endif diff --git a/Shared/Trending/TrendingView.swift b/Shared/Trending/TrendingView.swift index 538be03e..4bf8a13d 100644 --- a/Shared/Trending/TrendingView.swift +++ b/Shared/Trending/TrendingView.swift @@ -4,7 +4,7 @@ import SwiftUI struct TrendingView: View { @StateObject private var store = Store<[Video]>() - private var videos: [Video] + private var videos = [Video]() @Default(.trendingCategory) private var category @Default(.trendingCountry) private var country @@ -149,9 +149,7 @@ struct TrendingView: View { struct TrendingView_Previews: PreviewProvider { static var previews: some View { - TrendingView(Video.allFixtures + Video.allFixtures + Video.allFixtures) - .environmentObject(InvidiousAPI()) - .environmentObject(NavigationModel()) - .environmentObject(SubscriptionsModel()) + TrendingView(Video.allFixtures) + .injectFixtureEnvironmentObjects() } } diff --git a/Shared/Videos/VideosCellsHorizontal.swift b/Shared/Videos/VideosCellsHorizontal.swift index b62d167a..03f6ea75 100644 --- a/Shared/Videos/VideosCellsHorizontal.swift +++ b/Shared/Videos/VideosCellsHorizontal.swift @@ -60,7 +60,6 @@ struct VideosCellsHorizontal: View { struct VideoCellsHorizontal_Previews: PreviewProvider { static var previews: some View { VideosCellsHorizontal(videos: Video.allFixtures) - .environmentObject(NavigationModel()) - .environmentObject(SubscriptionsModel()) + .injectFixtureEnvironmentObjects() } } diff --git a/Shared/Videos/VideosCellsVertical.swift b/Shared/Videos/VideosCellsVertical.swift index 2be9ae98..8d0dd9b5 100644 --- a/Shared/Videos/VideosCellsVertical.swift +++ b/Shared/Videos/VideosCellsVertical.swift @@ -75,7 +75,6 @@ struct VideosCellsVertical: View { struct VideoCellsVertical_Previews: PreviewProvider { static var previews: some View { VideosCellsVertical(videos: Video.allFixtures) - .frame(minWidth: 1000) - .environmentObject(NavigationModel()) + .injectFixtureEnvironmentObjects() } } diff --git a/Shared/Views/SearchView.swift b/Shared/Views/SearchView.swift index 5b82a7ca..7c7e4ef1 100644 --- a/Shared/Views/SearchView.swift +++ b/Shared/Views/SearchView.swift @@ -20,8 +20,11 @@ struct SearchView: View { @EnvironmentObject private var recents @EnvironmentObject private var state - init(_ query: SearchQuery? = nil) { + private var videos = [Video]() + + init(_ query: SearchQuery? = nil, videos: [Video] = [Video]()) { self.query = query + self.videos = videos } var body: some View { @@ -82,6 +85,10 @@ struct SearchView: View { state.queryText = query!.query state.resetQuery(query!) } + + if !videos.isEmpty { + state.store.replace(videos) + } } .searchable(text: $state.queryText, placement: searchFieldPlacement) { ForEach(state.querySuggestions.collection, id: \.self) { suggestion in @@ -269,7 +276,7 @@ struct SearchView: View { var searchDurationButton: some View { Button(action: { self.searchDuration = self.searchDuration.next() }) { - Text(self.searchDate.name) + Text(self.searchDuration.name) .font(.system(size: 30)) .padding(.horizontal) .padding(.vertical, 2) @@ -334,10 +341,8 @@ struct SearchView: View { struct SearchView_Previews: PreviewProvider { static var previews: some View { NavigationView { - SearchView(SearchQuery(query: "Is Google Evil")) - .environmentObject(NavigationModel()) - .environmentObject(SearchModel()) - .environmentObject(SubscriptionsModel()) + SearchView(SearchQuery(query: "Is Google Evil"), videos: Video.fixtures(30)) + .injectFixtureEnvironmentObjects() } } } diff --git a/Shared/Views/SignInRequiredView.swift b/Shared/Views/SignInRequiredView.swift index debef824..bff849f9 100644 --- a/Shared/Views/SignInRequiredView.swift +++ b/Shared/Views/SignInRequiredView.swift @@ -44,9 +44,11 @@ struct SignInRequiredView: View { .font(.title3) .padding(.vertical) - if instances.isEmpty { - openSettingsButton - } + #if !os(tvOS) + if instances.isEmpty { + openSettingsButton + } + #endif #if os(tvOS) openSettingsButton @@ -73,5 +75,6 @@ struct SignInRequiredView_Previews: PreviewProvider { SignInRequiredView(title: "Subscriptions") { Text("Only when signed in") } + .environmentObject(InvidiousAPI()) } } diff --git a/Shared/Watch Now/WatchNowView.swift b/Shared/Watch Now/WatchNowView.swift index 80cbf3b6..4f71295c 100644 --- a/Shared/Watch Now/WatchNowView.swift +++ b/Shared/Watch Now/WatchNowView.swift @@ -40,7 +40,6 @@ struct WatchNowView: View { struct WatchNowView_Previews: PreviewProvider { static var previews: some View { WatchNowView() - .environmentObject(SubscriptionsModel()) - .environmentObject(NavigationModel()) + .injectFixtureEnvironmentObjects() } } diff --git a/macOS/Settings/InstancesSettingsView.swift b/macOS/Settings/InstancesSettingsView.swift index 691e9f2b..080c7d2b 100644 --- a/macOS/Settings/InstancesSettingsView.swift +++ b/macOS/Settings/InstancesSettingsView.swift @@ -122,6 +122,6 @@ struct InstancesSettingsView_Previews: PreviewProvider { InstancesSettingsView() } .frame(width: 400, height: 270) - .environmentObject(InstancesModel()) + .injectFixtureEnvironmentObjects() } } diff --git a/tvOS/TVNavigationView.swift b/tvOS/TVNavigationView.swift index 64a06e64..f76745ff 100644 --- a/tvOS/TVNavigationView.swift +++ b/tvOS/TVNavigationView.swift @@ -57,10 +57,6 @@ struct TVNavigationView: View { struct TVNavigationView_Previews: PreviewProvider { static var previews: some View { TVNavigationView() - .environmentObject(InvidiousAPI()) - .environmentObject(NavigationModel()) - .environmentObject(SearchModel()) - .environmentObject(InstancesModel()) - .environmentObject(SubscriptionsModel()) + .injectFixtureEnvironmentObjects() } }