fix: back button bounces user right back into selected business

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>
This commit is contained in:
Schwifty 2026-03-22 21:47:45 +00:00
parent f60c70f32a
commit 81d4cad030
2 changed files with 19 additions and 10 deletions

View file

@ -14,6 +14,9 @@ final class AppState: ObservableObject {
@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() {
@ -36,6 +39,8 @@ final class AppState: ObservableObject {
}
func backToBusinessList() {
AppPrefs.lastBusinessId = nil
skipAutoNav = true
currentScreen = .businessList
}

View file

@ -94,6 +94,8 @@ struct BusinessListView: View {
let list = try await APIClient.shared.listBusinesses(token: token)
businesses = list
// Skip auto-navigation if user explicitly tapped Back
if !appState.skipAutoNav {
// Auto-navigate if only one business (like Android)
if list.count == 1, let only = list.first {
appState.selectBusiness(only)
@ -106,6 +108,8 @@ struct BusinessListView: View {
appState.selectBusiness(last)
return
}
}
appState.skipAutoNav = false
} catch let e as APIError where e.errorDescription == APIError.unauthorized.errorDescription {
appState.logout()
} catch {