@@ -15,6 +15,9 @@ struct IcySkyApp: App {
15
15
@State var auth : Auth = . init( )
16
16
@State var currentUser : CurrentUser ?
17
17
@State var router : Router = . init( )
18
+ @State var isLoadingInitialSession : Bool = true
19
+
20
+ @Environment ( \. scenePhase) var scenePhase
18
21
19
22
var body : some Scene {
20
23
WindowGroup {
@@ -45,17 +48,23 @@ struct IcySkyApp: App {
45
48
}
46
49
}
47
50
)
48
- . task {
49
- if await auth. refresh ( ) == nil {
51
+ . task ( id: auth. session) {
52
+ if let newSession = auth. session {
53
+ await refreshEnvWith ( session: newSession)
54
+ if router. presentedSheet == . auth {
55
+ router. presentedSheet = nil
56
+ }
57
+ } else if auth. session == nil && !isLoadingInitialSession {
50
58
router. presentedSheet = . auth
51
59
}
60
+ isLoadingInitialSession = false
52
61
}
53
- . onChange ( of : auth . session ) { old , new in
54
- if let newSession = new {
55
- refreshEnvWith ( session : newSession )
56
- router . presentedSheet = nil
57
- } else if old != nil && new == nil {
58
- router . presentedSheet = . auth
62
+ . task ( id : scenePhase ) {
63
+ if scenePhase == . active {
64
+ await auth . refresh ( )
65
+ if auth . session == nil {
66
+ router . presentedSheet = . auth
67
+ }
59
68
}
60
69
}
61
70
. overlay (
@@ -120,9 +129,9 @@ struct IcySkyApp: App {
120
129
}
121
130
}
122
131
123
- private func refreshEnvWith( session: UserSession ) {
132
+ private func refreshEnvWith( session: UserSession ) async {
124
133
let client = BSkyClient ( session: session, protoClient: ATProtoKit ( session: session) )
125
134
self . client = client
126
- self . currentUser = CurrentUser ( client: client)
135
+ self . currentUser = await CurrentUser ( client: client)
127
136
}
128
137
}
0 commit comments