Update version
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import SwiftUI
|
||||
import CoreData
|
||||
|
||||
struct ContentView: View {
|
||||
@EnvironmentObject var iapService: IAPService
|
||||
@EnvironmentObject var adMobService: AdMobService
|
||||
@EnvironmentObject var tabSelection: TabSelectionStore
|
||||
@EnvironmentObject var coreDataStack: CoreDataStack
|
||||
@AppStorage("onboardingCompleted") private var onboardingCompleted = false
|
||||
@AppStorage("faceIdEnabled") private var faceIdEnabled = false
|
||||
@AppStorage("pinEnabled") private var pinEnabled = false
|
||||
@@ -11,6 +13,7 @@ struct ContentView: View {
|
||||
@AppStorage("lockOnBackground") private var lockOnBackground = false
|
||||
@Environment(\.scenePhase) private var scenePhase
|
||||
@State private var isUnlocked = false
|
||||
@State private var resolvedOnboardingCompleted: Bool?
|
||||
|
||||
private var lockEnabled: Bool {
|
||||
faceIdEnabled || pinEnabled
|
||||
@@ -19,7 +22,9 @@ struct ContentView: View {
|
||||
var body: some View {
|
||||
ZStack {
|
||||
Group {
|
||||
if !onboardingCompleted {
|
||||
if !isReadyForContent {
|
||||
AppLaunchLoadingView(messageKey: "loading_data")
|
||||
} else if resolvedOnboardingCompleted == false {
|
||||
OnboardingView(onboardingCompleted: $onboardingCompleted)
|
||||
} else {
|
||||
mainContent
|
||||
@@ -55,6 +60,62 @@ struct ContentView: View {
|
||||
isUnlocked = false
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
if coreDataStack.isLoaded {
|
||||
syncOnboardingState()
|
||||
}
|
||||
}
|
||||
.onChange(of: coreDataStack.isLoaded) { _, loaded in
|
||||
if loaded {
|
||||
syncOnboardingState()
|
||||
}
|
||||
}
|
||||
.onChange(of: onboardingCompleted) { _, completed in
|
||||
resolvedOnboardingCompleted = completed
|
||||
}
|
||||
}
|
||||
|
||||
private var isReadyForContent: Bool {
|
||||
coreDataStack.isLoaded && resolvedOnboardingCompleted != nil
|
||||
}
|
||||
|
||||
private func syncOnboardingState() {
|
||||
let settings = AppSettings.getOrCreate(in: coreDataStack.viewContext)
|
||||
var resolved = settings.onboardingCompleted || onboardingCompleted
|
||||
if !resolved && hasExistingData() {
|
||||
resolved = true
|
||||
}
|
||||
if settings.onboardingCompleted != resolved {
|
||||
settings.onboardingCompleted = resolved
|
||||
CoreDataStack.shared.save()
|
||||
}
|
||||
if onboardingCompleted != resolved {
|
||||
onboardingCompleted = resolved
|
||||
}
|
||||
resolvedOnboardingCompleted = resolved
|
||||
}
|
||||
|
||||
private func hasExistingData() -> Bool {
|
||||
let context = coreDataStack.viewContext
|
||||
|
||||
let sourceRequest: NSFetchRequest<InvestmentSource> = InvestmentSource.fetchRequest()
|
||||
sourceRequest.fetchLimit = 1
|
||||
sourceRequest.resultType = .countResultType
|
||||
if (try? context.count(for: sourceRequest)) ?? 0 > 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
let snapshotRequest: NSFetchRequest<Snapshot> = Snapshot.fetchRequest()
|
||||
snapshotRequest.fetchLimit = 1
|
||||
snapshotRequest.resultType = .countResultType
|
||||
if (try? context.count(for: snapshotRequest)) ?? 0 > 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
let accountRequest: NSFetchRequest<Account> = Account.fetchRequest()
|
||||
accountRequest.fetchLimit = 1
|
||||
accountRequest.resultType = .countResultType
|
||||
return ((try? context.count(for: accountRequest)) ?? 0) > 0
|
||||
}
|
||||
|
||||
private var mainContent: some View {
|
||||
@@ -84,7 +145,7 @@ struct ContentView: View {
|
||||
}
|
||||
.tag(3)
|
||||
|
||||
SettingsView()
|
||||
SettingsView(iapService: iapService)
|
||||
.tabItem {
|
||||
Label("Settings", systemImage: "gearshape.fill")
|
||||
}
|
||||
@@ -105,6 +166,7 @@ struct ContentView: View {
|
||||
|
||||
#Preview {
|
||||
ContentView()
|
||||
.environmentObject(CoreDataStack.shared)
|
||||
.environmentObject(IAPService())
|
||||
.environmentObject(AdMobService())
|
||||
.environmentObject(AccountStore(iapService: IAPService()))
|
||||
|
||||
Reference in New Issue
Block a user