Differentiate between updating old post / publishing new post in handler

This commit is contained in:
Angelo Stavrow 2021-01-27 16:12:40 -05:00
parent f589d42794
commit 5cdce15808
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE

View File

@ -37,6 +37,7 @@ class WriteFreelyModel: ObservableObject {
private let defaults = UserDefaults.standard private let defaults = UserDefaults.standard
private let monitor = NWPathMonitor() private let monitor = NWPathMonitor()
private let queue = DispatchQueue(label: "NetworkMonitor") private let queue = DispatchQueue(label: "NetworkMonitor")
private var postToUpdate: WFAPost?
init() { init() {
DispatchQueue.main.async { DispatchQueue.main.async {
@ -145,6 +146,8 @@ extension WriteFreelyModel {
} }
func publish(post: WFAPost) { func publish(post: WFAPost) {
postToUpdate = nil
if !hasNetworkConnection { if !hasNetworkConnection {
DispatchQueue.main.async { self.isPresentingNetworkErrorAlert = true } DispatchQueue.main.async { self.isPresentingNetworkErrorAlert = true }
return return
@ -173,10 +176,8 @@ extension WriteFreelyModel {
if let existingPostId = post.postId { if let existingPostId = post.postId {
// This is an existing post. // This is an existing post.
postToUpdate = post
wfPost.postId = post.postId wfPost.postId = post.postId
wfPost.slug = post.slug
wfPost.updatedDate = post.updatedDate
wfPost.collectionAlias = post.collectionAlias
loggedInClient.updatePost( loggedInClient.updatePost(
postId: existingPostId, postId: existingPostId,
@ -407,37 +408,55 @@ private extension WriteFreelyModel {
// See: https://github.com/writeas/writefreely-swift/issues/20 // See: https://github.com/writeas/writefreely-swift/issues/20
do { do {
let fetchedPost = try result.get() let fetchedPost = try result.get()
let request = WFAPost.createFetchRequest() // If this is an updated post, check it against postToUpdate.
let matchBodyPredicate = NSPredicate(format: "body == %@", fetchedPost.body) if let updatingPost = self.postToUpdate {
if let fetchedPostTitle = fetchedPost.title { updatingPost.appearance = fetchedPost.appearance
let matchTitlePredicate = NSPredicate(format: "title == %@", fetchedPostTitle) updatingPost.body = fetchedPost.body
request.predicate = NSCompoundPredicate( updatingPost.createdDate = fetchedPost.createdDate
andPredicateWithSubpredicates: [ updatingPost.language = fetchedPost.language
matchTitlePredicate, updatingPost.postId = fetchedPost.postId
matchBodyPredicate updatingPost.rtl = fetchedPost.rtl ?? false
] updatingPost.slug = fetchedPost.slug
) updatingPost.status = PostStatus.published.rawValue
} else { updatingPost.title = fetchedPost.title ?? ""
request.predicate = matchBodyPredicate updatingPost.updatedDate = fetchedPost.updatedDate
}
do {
let cachedPostsResults = try LocalStorageManager.persistentContainer.viewContext.fetch(request)
guard let cachedPost = cachedPostsResults.first else { return }
cachedPost.appearance = fetchedPost.appearance
cachedPost.body = fetchedPost.body
cachedPost.createdDate = fetchedPost.createdDate
cachedPost.language = fetchedPost.language
cachedPost.postId = fetchedPost.postId
cachedPost.rtl = fetchedPost.rtl ?? false
cachedPost.slug = fetchedPost.slug
cachedPost.status = PostStatus.published.rawValue
cachedPost.title = fetchedPost.title ?? ""
cachedPost.updatedDate = fetchedPost.updatedDate
DispatchQueue.main.async { DispatchQueue.main.async {
LocalStorageManager().saveContext() LocalStorageManager().saveContext()
} }
} catch { } else {
print("Error: Failed to fetch cached posts") // 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 {
let matchTitlePredicate = NSPredicate(format: "title == %@", fetchedPostTitle)
request.predicate = NSCompoundPredicate(
andPredicateWithSubpredicates: [
matchTitlePredicate,
matchBodyPredicate
]
)
} else {
request.predicate = matchBodyPredicate
}
do {
let cachedPostsResults = try LocalStorageManager.persistentContainer.viewContext.fetch(request)
guard let cachedPost = cachedPostsResults.first else { return }
cachedPost.appearance = fetchedPost.appearance
cachedPost.body = fetchedPost.body
cachedPost.createdDate = fetchedPost.createdDate
cachedPost.language = fetchedPost.language
cachedPost.postId = fetchedPost.postId
cachedPost.rtl = fetchedPost.rtl ?? false
cachedPost.slug = fetchedPost.slug
cachedPost.status = PostStatus.published.rawValue
cachedPost.title = fetchedPost.title ?? ""
cachedPost.updatedDate = fetchedPost.updatedDate
DispatchQueue.main.async {
LocalStorageManager().saveContext()
}
} catch {
print("Error: Failed to fetch cached posts")
}
} }
} catch { } catch {
print(error) print(error)