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:
parent
f60c70f32a
commit
81d4cad030
2 changed files with 19 additions and 10 deletions
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -94,18 +94,22 @@ struct BusinessListView: View {
|
|||
let list = try await APIClient.shared.listBusinesses(token: token)
|
||||
businesses = list
|
||||
|
||||
// Auto-navigate if only one business (like Android)
|
||||
if list.count == 1, let only = list.first {
|
||||
appState.selectBusiness(only)
|
||||
return
|
||||
}
|
||||
// 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)
|
||||
return
|
||||
}
|
||||
|
||||
// Auto-navigate to last used business
|
||||
if let lastId = AppPrefs.lastBusinessId,
|
||||
let last = list.first(where: { $0.id == lastId }) {
|
||||
appState.selectBusiness(last)
|
||||
return
|
||||
// Auto-navigate to last used business
|
||||
if let lastId = AppPrefs.lastBusinessId,
|
||||
let last = list.first(where: { $0.id == lastId }) {
|
||||
appState.selectBusiness(last)
|
||||
return
|
||||
}
|
||||
}
|
||||
appState.skipAutoNav = false
|
||||
} catch let e as APIError where e.errorDescription == APIError.unauthorized.errorDescription {
|
||||
appState.logout()
|
||||
} catch {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue