Reduce Welcome view

This commit is contained in:
Balazs Perlaki-Horvath 2024-10-17 10:13:08 +02:00
parent d94c862110
commit 79589e74bf
4 changed files with 39 additions and 75 deletions

View File

@ -222,12 +222,15 @@ private struct CompactView: View {
private struct Content<LaunchModel>: View where LaunchModel: LaunchProtocol { private struct Content<LaunchModel>: View where LaunchModel: LaunchProtocol {
@Environment(\.scenePhase) private var scenePhase @Environment(\.scenePhase) private var scenePhase
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
@EnvironmentObject private var browser: BrowserViewModel @EnvironmentObject private var browser: BrowserViewModel
@EnvironmentObject private var library: LibraryViewModel @EnvironmentObject private var library: LibraryViewModel
@EnvironmentObject private var navigation: NavigationViewModel
@FetchRequest( @FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \ZimFile.size, ascending: false)], sortDescriptors: [NSSortDescriptor(keyPath: \ZimFile.size, ascending: false)],
predicate: ZimFile.openedPredicate predicate: ZimFile.openedPredicate
) private var zimFiles: FetchedResults<ZimFile> ) private var zimFiles: FetchedResults<ZimFile>
/// this is still hacky a bit, as the change from here re-validates the view /// this is still hacky a bit, as the change from here re-validates the view
/// which triggers the model to be revalidated /// which triggers the model to be revalidated
@Default(.hasSeenCategories) private var hasSeenCategories @Default(.hasSeenCategories) private var hasSeenCategories
@ -258,7 +261,7 @@ private struct Content<LaunchModel>: View where LaunchModel: LaunchProtocol {
case .list: case .list:
LocalLibraryList() LocalLibraryList()
case .welcome(let welcomeViewState): case .welcome(let welcomeViewState):
WelcomeCatalog(viewState: welcomeViewState, showLibrary: showLibrary) WelcomeCatalog(viewState: welcomeViewState)
} }
} }
} }
@ -269,10 +272,6 @@ private struct Content<LaunchModel>: View where LaunchModel: LaunchProtocol {
.onAppear { .onAppear {
browser.updateLastOpened() browser.updateLastOpened()
} }
.task {
debugPrint("library: \(library)")
debugPrint("")
}
.onDisappear { .onDisappear {
// since the browser is comming from @Environment, // since the browser is comming from @Environment,
// by the time we get to .onDisappear, // by the time we get to .onDisappear,
@ -298,6 +297,22 @@ private struct Content<LaunchModel>: View where LaunchModel: LaunchProtocol {
browser.refreshVideoState() 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 #endif

View File

@ -87,7 +87,10 @@ struct BrowserTab: View {
private struct Content<LaunchModel>: View where LaunchModel: LaunchProtocol { private struct Content<LaunchModel>: View where LaunchModel: LaunchProtocol {
@Environment(\.isSearching) private var isSearching @Environment(\.isSearching) private var isSearching
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
@EnvironmentObject private var browser: BrowserViewModel @EnvironmentObject private var browser: BrowserViewModel
@EnvironmentObject private var library: LibraryViewModel
@EnvironmentObject private var navigation: NavigationViewModel
@FetchRequest( @FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \ZimFile.size, ascending: false)], sortDescriptors: [NSSortDescriptor(keyPath: \ZimFile.size, ascending: false)],
predicate: ZimFile.openedPredicate predicate: ZimFile.openedPredicate
@ -135,11 +138,19 @@ struct BrowserTab: View {
case .catalog(.list): case .catalog(.list):
LocalLibraryList() LocalLibraryList()
case .catalog(.welcome(let welcomeViewState)): 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
} }
} }
} }

View File

@ -17,6 +17,7 @@ import SwiftUI
import Combine import Combine
import Defaults import Defaults
/// Displays a grid of available local ZIM files. Used on new tab.
struct LocalLibraryList: View { struct LocalLibraryList: View {
@EnvironmentObject private var browser: BrowserViewModel @EnvironmentObject private var browser: BrowserViewModel
@FetchRequest( @FetchRequest(

View File

@ -17,17 +17,14 @@ import SwiftUI
import Combine import Combine
import Defaults 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 { struct WelcomeCatalog: View {
@Environment(\.horizontalSizeClass) private var horizontalSizeClass @Environment(\.horizontalSizeClass) private var horizontalSizeClass
@Environment(\.verticalSizeClass) private var verticalSizeClass @Environment(\.verticalSizeClass) private var verticalSizeClass
@EnvironmentObject private var navigation: NavigationViewModel
@EnvironmentObject private var library: LibraryViewModel @EnvironmentObject private var library: LibraryViewModel
@Default(.hasSeenCategories) private var hasSeenCategories
let viewState: WelcomeViewState let viewState: WelcomeViewState
/// Used only for iPhone
let showLibrary: (() -> Void)?
var body: some View { var body: some View {
ZStack { ZStack {
LogoView() LogoView()
@ -48,21 +45,6 @@ struct WelcomeCatalog: View {
x: geometry.size.width * 0.5, x: geometry.size.width * 0.5,
y: logoCalc.buttonCenterY) y: logoCalc.buttonCenterY)
.frame(maxWidth: logoCalc.buttonsWidth) .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 { if viewState == .error {
Text("library_refresh_error.retrieve.description".localized) Text("library_refresh_error.retrieve.description".localized)
.foregroundColor(.red) .foregroundColor(.red)
@ -107,7 +89,7 @@ struct WelcomeCatalog: View {
} label: { } label: {
HStack { HStack {
Spacer() Spacer()
if library.state == .inProgress { if viewState == .loading {
Text("welcome.button.status.fetching_catalog.text".localized) Text("welcome.button.status.fetching_catalog.text".localized)
} else { } else {
Text("welcome.button.status.fetch_catalog.text".localized) Text("welcome.button.status.fetch_catalog.text".localized)
@ -115,60 +97,15 @@ struct WelcomeCatalog: View {
Spacer() Spacer()
}.padding(6) }.padding(6)
} }
.disabled(library.state == .inProgress) .disabled(viewState == .loading)
.font(.subheadline) .font(.subheadline)
.buttonStyle(.bordered) .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 { struct WelcomeView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
WelcomeCatalog(viewState: .loading, WelcomeCatalog(viewState: .loading).environmentObject(LibraryViewModel()).preferredColorScheme(.light).padding()
showLibrary: nil).environmentObject(LibraryViewModel()).preferredColorScheme(.light).padding() WelcomeCatalog(viewState: .error).environmentObject(LibraryViewModel()).preferredColorScheme(.dark).padding()
WelcomeCatalog(viewState: .error,
showLibrary: nil).environmentObject(LibraryViewModel()).preferredColorScheme(.dark).padding()
} }
} }