@@ -10,6 +10,7 @@ class WriteFreelyModel: ObservableObject {
10
10
@Published var store = PostStore ( )
11
11
@Published var collections = CollectionListModel ( with: [ ] )
12
12
@Published var isLoggingIn : Bool = false
13
+ @Published var selectedPost : Post ?
13
14
14
15
private var client : WFClient ?
15
16
private let defaults = UserDefaults . standard
@@ -26,6 +27,25 @@ class WriteFreelyModel: ObservableObject {
26
27
27
28
DispatchQueue . main. async {
28
29
self . account. restoreState ( )
30
+ if self . account. isLoggedIn {
31
+ guard let serverURL = URL ( string: self . account. server) else {
32
+ print ( " Server URL not found " )
33
+ return
34
+ }
35
+ guard let token = self . fetchTokenFromKeychain (
36
+ username: self . account. username,
37
+ server: self . account. server
38
+ ) else {
39
+ print ( " Could not fetch token from Keychain " )
40
+ return
41
+ }
42
+ self . account. login ( WFUser ( token: token, username: self . account. username) )
43
+ self . client = WFClient ( for: serverURL)
44
+ self . client? . user = self . account. user
45
+ self . collections. clearUserCollection ( )
46
+ self . fetchUserCollections ( )
47
+ self . fetchUserPosts ( )
48
+ }
29
49
}
30
50
}
31
51
}
@@ -80,6 +100,15 @@ extension WriteFreelyModel {
80
100
)
81
101
}
82
102
}
103
+
104
+ func updateFromServer( post: Post ) {
105
+ guard let loggedInClient = client else { return }
106
+ guard let postId = post. wfPost. postId else { return }
107
+ DispatchQueue . main. async {
108
+ self . selectedPost = post
109
+ }
110
+ loggedInClient. getPost ( byId: postId, completion: updateFromServerHandler)
111
+ }
83
112
}
84
113
85
114
private extension WriteFreelyModel {
@@ -121,7 +150,7 @@ private extension WriteFreelyModel {
121
150
DispatchQueue . main. async {
122
151
self . account. logout ( )
123
152
self . collections. clearUserCollection ( )
124
- self . store. purge ( )
153
+ self . store. purgeAllPosts ( )
125
154
}
126
155
} catch {
127
156
print ( " Something went wrong purging the token from the Keychain. " )
@@ -136,7 +165,7 @@ private extension WriteFreelyModel {
136
165
DispatchQueue . main. async {
137
166
self . account. logout ( )
138
167
self . collections. clearUserCollection ( )
139
- self . store. purge ( )
168
+ self . store. purgeAllPosts ( )
140
169
}
141
170
} catch {
142
171
print ( " Something went wrong purging the token from the Keychain. " )
@@ -176,6 +205,7 @@ private extension WriteFreelyModel {
176
205
func fetchUserPostsHandler( result: Result < [ WFPost ] , Error > ) {
177
206
do {
178
207
let fetchedPosts = try result. get ( )
208
+ var fetchedPostsArray : [ Post ] = [ ]
179
209
for fetchedPost in fetchedPosts {
180
210
var post : Post
181
211
if let matchingAlias = fetchedPost. collectionAlias {
@@ -186,9 +216,10 @@ private extension WriteFreelyModel {
186
216
} else {
187
217
post = Post ( wfPost: fetchedPost)
188
218
}
189
- DispatchQueue . main. async {
190
- self . store. add ( post)
191
- }
219
+ fetchedPostsArray. append ( post)
220
+ }
221
+ DispatchQueue . main. async {
222
+ self . store. updateStore ( with: fetchedPostsArray)
192
223
}
193
224
} catch {
194
225
print ( error)
@@ -209,6 +240,18 @@ private extension WriteFreelyModel {
209
240
print ( error)
210
241
}
211
242
}
243
+
244
+ func updateFromServerHandler( result: Result < WFPost , Error > ) {
245
+ do {
246
+ let fetchedPost = try result. get ( )
247
+ DispatchQueue . main. async {
248
+ guard let selectedPost = self . selectedPost else { return }
249
+ self . store. replace ( post: selectedPost, with: fetchedPost)
250
+ }
251
+ } catch {
252
+ print ( error)
253
+ }
254
+ }
212
255
}
213
256
214
257
private extension WriteFreelyModel {
0 commit comments