mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Differentiate between updating old post / publishing new post in handler
This commit is contained in:
parent
f589d42794
commit
5cdce15808
@ -37,6 +37,7 @@ class WriteFreelyModel: ObservableObject {
|
||||
private let defaults = UserDefaults.standard
|
||||
private let monitor = NWPathMonitor()
|
||||
private let queue = DispatchQueue(label: "NetworkMonitor")
|
||||
private var postToUpdate: WFAPost?
|
||||
|
||||
init() {
|
||||
DispatchQueue.main.async {
|
||||
@ -145,6 +146,8 @@ extension WriteFreelyModel {
|
||||
}
|
||||
|
||||
func publish(post: WFAPost) {
|
||||
postToUpdate = nil
|
||||
|
||||
if !hasNetworkConnection {
|
||||
DispatchQueue.main.async { self.isPresentingNetworkErrorAlert = true }
|
||||
return
|
||||
@ -173,10 +176,8 @@ extension WriteFreelyModel {
|
||||
|
||||
if let existingPostId = post.postId {
|
||||
// This is an existing post.
|
||||
postToUpdate = post
|
||||
wfPost.postId = post.postId
|
||||
wfPost.slug = post.slug
|
||||
wfPost.updatedDate = post.updatedDate
|
||||
wfPost.collectionAlias = post.collectionAlias
|
||||
|
||||
loggedInClient.updatePost(
|
||||
postId: existingPostId,
|
||||
@ -407,6 +408,23 @@ private extension WriteFreelyModel {
|
||||
// See: https://github.com/writeas/writefreely-swift/issues/20
|
||||
do {
|
||||
let fetchedPost = try result.get()
|
||||
// If this is an updated post, check it against postToUpdate.
|
||||
if let updatingPost = self.postToUpdate {
|
||||
updatingPost.appearance = fetchedPost.appearance
|
||||
updatingPost.body = fetchedPost.body
|
||||
updatingPost.createdDate = fetchedPost.createdDate
|
||||
updatingPost.language = fetchedPost.language
|
||||
updatingPost.postId = fetchedPost.postId
|
||||
updatingPost.rtl = fetchedPost.rtl ?? false
|
||||
updatingPost.slug = fetchedPost.slug
|
||||
updatingPost.status = PostStatus.published.rawValue
|
||||
updatingPost.title = fetchedPost.title ?? ""
|
||||
updatingPost.updatedDate = fetchedPost.updatedDate
|
||||
DispatchQueue.main.async {
|
||||
LocalStorageManager().saveContext()
|
||||
}
|
||||
} else {
|
||||
// Otherwise if it's a newly-published post, find it in the local store.
|
||||
let request = WFAPost.createFetchRequest()
|
||||
let matchBodyPredicate = NSPredicate(format: "body == %@", fetchedPost.body)
|
||||
if let fetchedPostTitle = fetchedPost.title {
|
||||
@ -439,6 +457,7 @@ private extension WriteFreelyModel {
|
||||
} catch {
|
||||
print("Error: Failed to fetch cached posts")
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
print(error)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user