Persist auth across preview rerenders

This commit is contained in:
lucast committed 2026-10-07 02:14:28 +02:00
1 parent d6b35fe346
commit d9a996aa98
5 files changed
+163 -8

No files matched your search

+12 -6
View File
@@ -1,12 +1,15 @@
import SwiftUI
struct ContentView: View {
@StateObject private var session = AuthSessionStore()
@StateObject private var session: AuthSessionStore
private let restoresSession: Bool
@MainActor init(session: AuthSessionStore? = nil, restoresSession: Bool = true) {
_session = StateObject(wrappedValue: session ?? AuthSessionStore())
_session = StateObject(wrappedValue: session ?? AuthSessionStore.shared)
self.restoresSession = restoresSession
#if DEBUG
print("[ContentView] initialized; injectedSession=\(session != nil), restoresSession=\(restoresSession)")
#endif
}
var body: some View {
@@ -26,6 +29,12 @@ struct ContentView: View {
}
.task {
guard restoresSession else { return }
#if DEBUG
print("[ContentView] session restore task started")
defer {
print("[ContentView] session restore task ended; cancelled=\(Task.isCancelled)")
}
#endif
await session.restoreSession()
}
}
@@ -33,9 +42,6 @@ struct ContentView: View {
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(
session: AuthSessionStore(initialState: .signedOut),
restoresSession: false
)
ContentView()
}
}