Add single-post updateFromServer API to WriteFreelyModel

This commit is contained in:
Angelo Stavrow 2020-08-31 16:38:35 -04:00
parent b21cef6a54
commit 16b16dbd52
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE

View File

@ -100,6 +100,15 @@ extension WriteFreelyModel {
)
}
}
func updateFromServer(post: Post) {
guard let loggedInClient = client else { return }
guard let postId = post.wfPost.postId else { return }
DispatchQueue.main.async {
self.selectedPost = post
}
loggedInClient.getPost(byId: postId, completion: updateFromServerHandler)
}
}
private extension WriteFreelyModel {
@ -208,9 +217,6 @@ private extension WriteFreelyModel {
post = Post(wfPost: fetchedPost)
}
fetchedPostsArray.append(post)
// DispatchQueue.main.async {
// self.store.add(post)
// }
}
DispatchQueue.main.async {
self.store.updateStore(with: fetchedPostsArray)
@ -234,6 +240,18 @@ private extension WriteFreelyModel {
print(error)
}
}
func updateFromServerHandler(result: Result<WFPost, Error>) {
do {
let fetchedPost = try result.get()
DispatchQueue.main.async {
guard let selectedPost = self.selectedPost else { return }
self.store.replace(post: selectedPost, with: fetchedPost)
}
} catch {
print(error)
}
}
}
private extension WriteFreelyModel {