diff --git a/iosApp/iosApp/RootView.swift b/iosApp/iosApp/RootView.swift new file mode 100644 index 0000000..dd3ff19 --- /dev/null +++ b/iosApp/iosApp/RootView.swift @@ -0,0 +1,66 @@ +import SwiftUI +import ComposeApp + +/// Shared authentication state manager +class AuthenticationManager: ObservableObject { + static let shared = AuthenticationManager() + + @Published var isAuthenticated: Bool = false + private let sharedViewModel: ComposeApp.AuthViewModel + + private init() { + self.sharedViewModel = ComposeApp.AuthViewModel() + checkAuthenticationStatus() + } + + func checkAuthenticationStatus() { + // Simple check: if token exists, user is authenticated + if let token = TokenStorage.shared.getToken(), !token.isEmpty { + isAuthenticated = true + + // CRITICAL: Initialize lookups if user is already logged in + // Without this, lookups won't load on app launch for returning users + Task { + do { + _ = try await APILayer.shared.initializeLookups() + print("✅ Lookups initialized on app launch for authenticated user") + } catch { + print("❌ Failed to initialize lookups on app launch: \(error)") + } + } + } else { + isAuthenticated = false + } + } + + func login() { + isAuthenticated = true + } + + func logout() { + // Call shared ViewModel logout + sharedViewModel.logout() + + // Clear token from storage + TokenStorage.shared.clearToken() + + // Clear lookups data on logout via DataCache + DataCache.shared.clearLookups() + + // Clear all cached data + DataCache.shared.clearAll() + + // Update authentication state + isAuthenticated = false + + print("AuthenticationManager: Logged out - all state reset") + } +} + +/// Root view that always shows the main app, with login presented as a modal when needed +struct RootView: View { + + var body: some View { + MainTabView() + } +} diff --git a/iosApp/iosApp/iOSApp.swift b/iosApp/iosApp/iOSApp.swift index 07486b8..479ac09 100644 --- a/iosApp/iosApp/iOSApp.swift +++ b/iosApp/iosApp/iOSApp.swift @@ -13,7 +13,7 @@ struct iOSApp: App { var body: some Scene { WindowGroup { - LoginView(resetToken: $deepLinkResetToken) + RootView() .onOpenURL { url in handleDeepLink(url: url) } @@ -41,4 +41,4 @@ struct iOSApp: App { print("No token found in deep link") } } -} \ No newline at end of file +}