Untitled
unknown
swift
2 years ago
2.4 kB
23
Indexable
import SwiftUI
import Combine
@MainActor
struct LaunchViewModel: View {
@ObservedObject var viewModel = AuthenticationViewModel()
@State private var navigationDestination: NavigationDestination? = .welcomeScreen
enum NavigationDestination {
case welcomeScreen
case onboardingScreen
case mainScreen
case mainAuthScreen
}
var body: some View {
NavigationView {
Group {
switch navigationDestination {
case .welcomeScreen:
WelcomeScreen()
.onAppear {
async {
await handleWelcomeScreen()
}
}
case .onboardingScreen:
OnBoardingScreen()
case .mainScreen:
MainScreenView()
.environmentObject(viewModel)
case .mainAuthScreen:
MainAuthView(isLoggedIn: $viewModel.isLoggedIn)
case .none:
ProgressView()
}
}
.onAppear {
async {
await handleOnAppear()
}
}
}
}
private func handleWelcomeScreen() async {
let isNewUser = await viewModel.checkIfNewUser()
if isNewUser {
navigationDestination = .onboardingScreen
} else {
await checkAuthentication()
}
}
private func handleOnAppear() async {
if UserDefaults.standard.bool(forKey: "hasSeenWelcomeScreen") {
let isNewUser = await viewModel.checkIfNewUser()
if isNewUser {
navigationDestination = .onboardingScreen
} else {
await checkAuthentication()
}
} else {
UserDefaults.standard.set(true, forKey: "hasSeenWelcomeScreen")
UserDefaults.standard.set(true, forKey: "isNewUser")
}
}
private func checkAuthentication() async {
let isAuthenticated = await viewModel.checkIfAuthenticated()
if isAuthenticated {
navigationDestination = .mainScreen
viewModel.isLoggedIn = true
} else {
navigationDestination = .mainAuthScreen
viewModel.isLoggedIn = false
}
}
}
Editor is loading...
Leave a Comment