When tapping Back from ScanView, BusinessListView remounts and .task fires loadBusinesses() which sees AppPrefs.lastBusinessId still set — immediately re-navigating into the same business. Fix: clear lastBusinessId on back, and add skipAutoNav flag to also prevent single-business auto-select from firing when user explicitly navigated back. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
53 lines
1.4 KiB
Swift
53 lines
1.4 KiB
Swift
import SwiftUI
|
|
|
|
/// Central app state — drives navigation between Login → Business Select → Scan
|
|
@MainActor
|
|
final class AppState: ObservableObject {
|
|
|
|
enum Screen {
|
|
case login
|
|
case businessList
|
|
case scan(business: Business)
|
|
}
|
|
|
|
@Published var currentScreen: Screen = .login
|
|
@Published var token: String?
|
|
@Published var userId: String?
|
|
|
|
/// When true, skip auto-navigation in BusinessListView (user explicitly went back)
|
|
var skipAutoNav = false
|
|
|
|
init() {
|
|
// Restore saved session
|
|
if let saved = SecureStorage.loadSession() {
|
|
self.token = saved.token
|
|
self.userId = saved.userId
|
|
self.currentScreen = .businessList
|
|
}
|
|
}
|
|
|
|
func didLogin(token: String, userId: String) {
|
|
self.token = token
|
|
self.userId = userId
|
|
SecureStorage.saveSession(token: token, userId: userId)
|
|
currentScreen = .businessList
|
|
}
|
|
|
|
func selectBusiness(_ business: Business) {
|
|
AppPrefs.lastBusinessId = business.id
|
|
currentScreen = .scan(business: business)
|
|
}
|
|
|
|
func backToBusinessList() {
|
|
AppPrefs.lastBusinessId = nil
|
|
skipAutoNav = true
|
|
currentScreen = .businessList
|
|
}
|
|
|
|
func logout() {
|
|
token = nil
|
|
userId = nil
|
|
SecureStorage.clearSession()
|
|
currentScreen = .login
|
|
}
|
|
}
|