diff --git a/App/CompactViewController.swift b/App/CompactViewController.swift index a810a74d..f775a6a3 100644 --- a/App/CompactViewController.swift +++ b/App/CompactViewController.swift @@ -222,12 +222,15 @@ private struct CompactView: View { private struct Content: View where LaunchModel: LaunchProtocol { @Environment(\.scenePhase) private var scenePhase + @Environment(\.horizontalSizeClass) private var horizontalSizeClass @EnvironmentObject private var browser: BrowserViewModel @EnvironmentObject private var library: LibraryViewModel + @EnvironmentObject private var navigation: NavigationViewModel @FetchRequest( sortDescriptors: [NSSortDescriptor(keyPath: \ZimFile.size, ascending: false)], predicate: ZimFile.openedPredicate ) private var zimFiles: FetchedResults + /// this is still hacky a bit, as the change from here re-validates the view /// which triggers the model to be revalidated @Default(.hasSeenCategories) private var hasSeenCategories @@ -258,7 +261,7 @@ private struct Content: View where LaunchModel: LaunchProtocol { case .list: LocalLibraryList() case .welcome(let welcomeViewState): - WelcomeCatalog(viewState: welcomeViewState, showLibrary: showLibrary) + WelcomeCatalog(viewState: welcomeViewState) } } } @@ -269,10 +272,6 @@ private struct Content: View where LaunchModel: LaunchProtocol { .onAppear { browser.updateLastOpened() } - .task { - debugPrint("library: \(library)") - debugPrint("") - } .onDisappear { // since the browser is comming from @Environment, // by the time we get to .onDisappear, @@ -298,6 +297,22 @@ private struct Content: View where LaunchModel: LaunchProtocol { browser.refreshVideoState() } } + .onChange(of: library.state) { state in + guard state == .complete else { return } + showTheLibrary() + } + } + + private func showTheLibrary() { + #if os(macOS) + navigation.currentItem = .categories + #else + if horizontalSizeClass == .regular { + navigation.currentItem = .categories + } else { + showLibrary() + } + #endif } } #endif diff --git a/Views/BrowserTab.swift b/Views/BrowserTab.swift index 1d84cb10..caef7a73 100644 --- a/Views/BrowserTab.swift +++ b/Views/BrowserTab.swift @@ -87,7 +87,10 @@ struct BrowserTab: View { private struct Content: View where LaunchModel: LaunchProtocol { @Environment(\.isSearching) private var isSearching + @Environment(\.horizontalSizeClass) private var horizontalSizeClass @EnvironmentObject private var browser: BrowserViewModel + @EnvironmentObject private var library: LibraryViewModel + @EnvironmentObject private var navigation: NavigationViewModel @FetchRequest( sortDescriptors: [NSSortDescriptor(keyPath: \ZimFile.size, ascending: false)], predicate: ZimFile.openedPredicate @@ -135,11 +138,19 @@ struct BrowserTab: View { case .catalog(.list): LocalLibraryList() case .catalog(.welcome(let welcomeViewState)): - WelcomeCatalog(viewState: welcomeViewState, showLibrary: nil) + WelcomeCatalog(viewState: welcomeViewState) } } } } + .onChange(of: library.state) { state in + guard state == .complete else { return } + showTheLibrary() + } + } + + private func showTheLibrary() { + navigation.currentItem = .categories } } } diff --git a/Views/LocalLibraryList.swift b/Views/LocalLibraryList.swift index e3f2a17b..01a0b9b5 100644 --- a/Views/LocalLibraryList.swift +++ b/Views/LocalLibraryList.swift @@ -17,6 +17,7 @@ import SwiftUI import Combine import Defaults +/// Displays a grid of available local ZIM files. Used on new tab. struct LocalLibraryList: View { @EnvironmentObject private var browser: BrowserViewModel @FetchRequest( diff --git a/Views/WelcomeCatalog.swift b/Views/WelcomeCatalog.swift index 68c1c59d..87e71ce5 100644 --- a/Views/WelcomeCatalog.swift +++ b/Views/WelcomeCatalog.swift @@ -17,17 +17,14 @@ import SwiftUI import Combine import Defaults +/// Displays the Logo and 2 buttons open file | fetch catalog. +/// Used on new tab, when no ZIM files are available struct WelcomeCatalog: View { @Environment(\.horizontalSizeClass) private var horizontalSizeClass @Environment(\.verticalSizeClass) private var verticalSizeClass - @EnvironmentObject private var navigation: NavigationViewModel @EnvironmentObject private var library: LibraryViewModel - @Default(.hasSeenCategories) private var hasSeenCategories let viewState: WelcomeViewState - /// Used only for iPhone - let showLibrary: (() -> Void)? - var body: some View { ZStack { LogoView() @@ -48,21 +45,6 @@ struct WelcomeCatalog: View { x: geometry.size.width * 0.5, y: logoCalc.buttonCenterY) .frame(maxWidth: logoCalc.buttonsWidth) - .onChange(of: library.state) { state in - if state == .error { - hasSeenCategories = true - } - guard state == .complete else { return } -#if os(macOS) - navigation.currentItem = .categories -#elseif os(iOS) - if horizontalSizeClass == .regular { - navigation.currentItem = .categories - } else { - showLibrary?() - } -#endif - } if viewState == .error { Text("library_refresh_error.retrieve.description".localized) .foregroundColor(.red) @@ -107,7 +89,7 @@ struct WelcomeCatalog: View { } label: { HStack { Spacer() - if library.state == .inProgress { + if viewState == .loading { Text("welcome.button.status.fetching_catalog.text".localized) } else { Text("welcome.button.status.fetch_catalog.text".localized) @@ -115,60 +97,15 @@ struct WelcomeCatalog: View { Spacer() }.padding(6) } - .disabled(library.state == .inProgress) + .disabled(viewState == .loading) .font(.subheadline) .buttonStyle(.bordered) } } -// -//struct Welcome: View { -// @Environment(\.horizontalSizeClass) private var horizontalSizeClass -// @Environment(\.verticalSizeClass) private var verticalSizeClass -// @EnvironmentObject private var browser: BrowserViewModel -// @EnvironmentObject private var library: LibraryViewModel -// @EnvironmentObject private var navigation: NavigationViewModel -// @Default(.hasSeenCategories) private var hasSeenCategories -// -// /// Used only for iPhone -// let showLibrary: (() -> Void)? -// -// var body: some View { -// if zimFiles.isEmpty || !FeatureFlags.hasLibrary { -// ZStack { -// if !FeatureFlags.hasLibrary && browser.isLoading != false { -// LoadingView() -// } else { -// LogoView() -// welcomeContent -// .onAppear { -// if !hasSeenCategories, library.state == .complete { -// // safety path for upgrading user with no ZIM files, but fetched categories -// // to make sure we do display the buttons -// hasSeenCategories = true -// } -// } -// if library.state == .inProgress { -// if hasSeenCategories { -// LoadingProgressView() -// } else { -// LoadingMessageView(message: "welcome.button.status.fetching_catalog.text".localized) -// } -// } -// } -// }.ignoresSafeArea() -// } else if FeatureFlags.hasLibrary { -// -// } -// } -// -// -//} struct WelcomeView_Previews: PreviewProvider { static var previews: some View { - WelcomeCatalog(viewState: .loading, - showLibrary: nil).environmentObject(LibraryViewModel()).preferredColorScheme(.light).padding() - WelcomeCatalog(viewState: .error, - showLibrary: nil).environmentObject(LibraryViewModel()).preferredColorScheme(.dark).padding() + WelcomeCatalog(viewState: .loading).environmentObject(LibraryViewModel()).preferredColorScheme(.light).padding() + WelcomeCatalog(viewState: .error).environmentObject(LibraryViewModel()).preferredColorScheme(.dark).padding() } }