// ContentView.swift import SwiftUI import UIKit struct ContentView: View { @EnvironmentObject private var auth: AuthManager @EnvironmentObject private var sync: SyncManager @StateObject private var updateChecker = UpdateChecker.shared var body: some View { Group { if auth.isLoading && !auth.isAuthenticated { VStack(spacing: 16) { Image(systemName: "checkmark.seal.fill") .font(.system(size: 64)) .foregroundStyle(.blue) ProgressView("Loading…") } } else if auth.isAuthenticated { DashboardView() } else { LoginView() } } .animation(.easeInOut(duration: 0.3), value: auth.isAuthenticated) .task { // 1. Restore session first — sets isAuthenticated and loads tokens. await AuthManager.shared.restoreSession() // 2. Only start network monitoring AFTER auth is resolved. // startMonitoring() fires triggerSync() immediately on connectivity, // so it must not run before tokens are in place. SyncManager.shared.startMonitoring() // 3. If already online and authenticated, do an initial sync. if SyncManager.shared.isOnline && AuthManager.shared.isAuthenticated { await SyncManager.shared.triggerSync() } // 4. Check the App Store for a newer version. Independent of auth — // runs even on the login screen so unauthenticated devices still // get prompted. Throttled internally to once per 24h; silent on // any failure (offline, parse error, etc.) so it never disrupts // normal app use. await updateChecker.checkForUpdate() } .alert("Update Available", isPresented: $updateChecker.updateAvailable) { Button("Update") { if let url = updateChecker.appStoreURL { UIApplication.shared.open(url) } } Button("Later", role: .cancel) { updateChecker.dismissForNow() } } message: { if let version = updateChecker.latestVersion { Text("Version \(version) is available on the App Store. Update now for the latest fixes and features.") } else { Text("A new version is available on the App Store.") } } } }