1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import Foundation
import SwiftUI
extension AppModel {
func sceneBecameActive() {
refreshExpiredModeration()
if !isRestoringSession, account != nil {
Task { await reloadContentPreferences() }
}
}
func restoreSession() async {
let session = moderationSession
defer {
isRestoringSession = false
signedInAvatar?.finishPreload()
}
do {
let restored = try await service.restoreAccount { [weak self] accountID in
guard let self, moderationSession == session, !isSigningIn, !isSigningOut else { return }
signedInAvatar?.preload(for: accountID)
}
guard let restored, session == moderationSession, !isSigningIn, !isSigningOut
else { return }
await signedInAvatar?.restore(for: restored)
guard session == moderationSession, !isSigningIn, !isSigningOut else { return }
account = restored
if !composerOnly {
await restoreFeedSelection()
guard session == moderationSession else { return }
}
await reloadContentPreferences()
guard session == moderationSession, contentPreferences != nil else { return }
if !composerOnly { await loadFeed() }
} catch {
guard session == moderationSession, !Task.isCancelled, !NetworkFailure.isCancellation(error) else { return }
errorMessage = NetworkFailure.message(for: error)
}
}
func signIn(handle: String, appPassword: String, authFactorToken: String? = nil) async {
guard !isSigningIn, !isSigningOut else { return }
cancelContentPreferencesReload()
moderationSession = UUID()
let session = moderationSession
followResults = [:]
followingActorIDs = []
activeFollowIDs = []
uncertainFollowIDs = []
contentPreferences = nil
invalidateContent()
moderationStates = [:]
moderationBusy = []
uncertainModerationIDs = [:]
uncertainReports = []
moderationError = nil
isSigningIn = true
errorMessage = nil
authFactorErrorMessage = nil
defer { isSigningIn = false }
do {
let signedIn = try await service.signIn(
handle: handle,
appPassword: appPassword,
authFactorToken: authFactorToken
)
guard session == moderationSession, !isSigningOut else { return }
signedInAvatar?.update(for: signedIn)
account = signedIn
await restoreFeedSelection()
guard session == moderationSession else { return }
requiresAuthFactorToken = false
await reloadContentPreferences()
guard session == moderationSession, contentPreferences != nil else { return }
await loadFeed()
} catch ServiceError.authFactorTokenRequired {
guard session == moderationSession else { return }
requiresAuthFactorToken = true
} catch ServiceError.authFactorTokenRejected {
guard session == moderationSession else { return }
requiresAuthFactorToken = true
authFactorErrorMessage = ServiceError.authFactorTokenRejected.localizedDescription
} catch {
guard session == moderationSession, !Task.isCancelled, !NetworkFailure.isCancellation(error) else { return }
errorMessage = NetworkFailure.message(for: error)
}
}
func cancelAuthFactorSignIn() {
requiresAuthFactorToken = false
authFactorErrorMessage = nil
errorMessage = nil
}
func signInWithDemo() async {
await signIn(handle: "demo.bsky.social", appPassword: "ui-test")
}
func signOut() async {
guard !isSigningOut else { return }
isSigningOut = true
cancelContentPreferencesReload()
defer { isSigningOut = false }
contentPreferences = nil
invalidateContent()
do {
try await service.signOut()
clearSignedOutAccount()
} catch {
if !NetworkFailure.isCancellation(error) { errorMessage = NetworkFailure.message(for: error) }
contentPreferencesError = "Could not load content preferences. Try again."
}
}
private func clearSignedOutAccount() {
contentGeneration = UUID()
sessionGeneration = UUID()
moderationSession = UUID()
signedInAvatar?.clear()
account = nil
selectedFeed = nil
padColumns = [.following, .notifications]
padStates = [:]
subscribedFeeds = []
isLoadingFeeds = false
feedsErrorMessage = nil
contentPreferences = nil
contentPreferencesError = nil
contentPreferencesRefreshError = nil
activePostActions = []
deletedPostIDs = []
activeFollowIDs = []
uncertainPostIDs = []
uncertainFollowIDs = []
uncertainDraftKeys = []
isPublishing = false
publishErrorMessage = nil
publishResultUnknown = false
posts = []
notifications = []
notificationPageStarts = []
notificationsCursor = nil
hasMoreNotifications = false
notificationsErrorMessage = nil
replyContexts = [:]
repostedBy = [:]
threadContexts = [:]
loadingPostIDs = []
failedPostIDs = []
postErrors = [:]
feedErrorMessage = nil
retryFeedPage = false
isLoadingPost = false
timelinePostIDs = []
cursor = nil
hasMorePosts = false
clearSignedOutInteractions()
}
private func clearSignedOutInteractions() {
followingActorIDs = []
followResults = [:]
moderationStates = [:]
moderationBusy = []
uncertainModerationIDs = [:]
uncertainReports = []
moderationError = nil
profiles = [:]
authorPosts = [:]
profileCursors = [:]
profileErrors = [:]
profileRepostedBy = [:]
loadingMoreProfileIDs = []
loadingProfileIDs = []
feedRequest = nil
moreFeedRequest = nil
postRequests = [:]
profileRequests = [:]
moreProfileRequests = [:]
notificationsRequest = nil
moreNotificationsRequest = nil
isLoadingNotifications = false
isLoadingMoreNotifications = false
resumeNotificationLoads()
failedNotificationsPage = false
isLoading = false
isLoadingMore = false
errorMessage = nil
}
}