Files
honeyDueKMP/iosApp/iosApp/iOSApp.swift
Trey t 70d46da14a Add smart device token caching for push notification registration
Cache device token in UserDefaults and only register with backend when
token changes. Also registers when app returns from background if token
differs from cached value, reducing unnecessary API calls.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-04 20:18:08 -06:00

70 lines
2.5 KiB
Swift

import SwiftUI
import ComposeApp
@main
struct iOSApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@StateObject private var themeManager = ThemeManager.shared
@Environment(\.scenePhase) private var scenePhase
@State private var deepLinkResetToken: String?
init() {
// Initialize DataManager with platform-specific managers
// This must be done before any other operations that access DataManager
DataManager.shared.initialize(
tokenMgr: TokenManager.Companion.shared.getInstance(),
themeMgr: ThemeStorageManager.Companion.shared.getInstance(),
persistenceMgr: PersistenceManager()
)
// Initialize TokenStorage once at app startup (legacy support)
TokenStorage.shared.initialize(manager: TokenManager.Companion.shared.getInstance())
// Initialize lookups at app start (public endpoints, no auth required)
// This fetches /static_data/ and /upgrade-triggers/ immediately
Task {
print("🚀 Initializing lookups at app start...")
_ = try? await APILayer.shared.initializeLookups()
print("✅ Lookups initialized")
}
}
var body: some Scene {
WindowGroup {
RootView()
.environmentObject(themeManager)
.onOpenURL { url in
handleDeepLink(url: url)
}
.onChange(of: scenePhase) { newPhase in
if newPhase == .active {
// Check and register device token when app becomes active
PushNotificationManager.shared.checkAndRegisterDeviceIfNeeded()
}
}
}
}
// MARK: - Deep Link Handling
private func handleDeepLink(url: URL) {
print("Deep link received: \(url)")
// Handle casera://reset-password?token=xxx
guard url.scheme == "casera",
url.host == "reset-password" else {
print("Unrecognized deep link scheme or host")
return
}
// Parse token from query parameters
if let components = URLComponents(url: url, resolvingAgainstBaseURL: true),
let queryItems = components.queryItems,
let token = queryItems.first(where: { $0.name == "token" })?.value {
print("Reset token extracted: \(token)")
deepLinkResetToken = token
} else {
print("No token found in deep link")
}
}
}