39 lines
1.4 KiB
Swift
39 lines
1.4 KiB
Swift
// ContentView.swift
|
|
|
|
import SwiftUI
|
|
|
|
struct ContentView: View {
|
|
@EnvironmentObject private var auth: AuthManager
|
|
@EnvironmentObject private var sync: SyncManager
|
|
|
|
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()
|
|
}
|
|
}
|
|
}
|
|
}
|