Merge pull request #167 from writeas/fix-post-status-bug

Fix post-status bug on publishing changes
This commit is contained in:
Angelo Stavrow 2021-02-01 10:12:27 -05:00 committed by GitHub
commit 849c093748
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 141 additions and 120 deletions

View File

@ -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)
}

View File

@ -112,7 +112,8 @@ struct PostListView: View {
)
.toolbar {
ToolbarItemGroup(placement: .primaryAction) {
ActivePostToolbarView()
if model.selectedPost != nil {
ActivePostToolbarView(activePost: model.selectedPost!)
.alert(isPresented: $model.isPresentingNetworkErrorAlert, content: {
Alert(
title: Text("Connection Error"),
@ -127,6 +128,7 @@ struct PostListView: View {
})
}
}
}
.navigationTitle(
model.showAllPosts ? "All Posts" : model.selectedCollection?.title ?? (
model.account.server == "https://write.as" ? "Anonymous" : "Drafts"

View File

@ -2,6 +2,7 @@ import SwiftUI
struct ActivePostToolbarView: View {
@EnvironmentObject var model: WriteFreelyModel
@ObservedObject var activePost: WFAPost
@State private var isPresentingSharingServicePicker: Bool = false
@State private var selectedCollection: WFACollection?
@ -11,7 +12,6 @@ struct ActivePostToolbarView: View {
) var collections: FetchedResults<WFACollection>
var body: some View {
if let activePost = model.selectedPost {
HStack {
if model.account.isLoggedIn &&
activePost.status != PostStatus.local.rawValue &&
@ -64,7 +64,7 @@ struct ActivePostToolbarView: View {
}, label: {
Label("Publish…", systemImage: "paperplane")
})
.disabled(activePost.body.isEmpty)
.disabled(model.selectedPost?.body.isEmpty ?? true)
.help("Publish the post to the web.\(model.account.isLoggedIn ? "" : " You must be logged in to do this.")") // swiftlint:disable:this line_length
} else {
HStack(spacing: 4) {
@ -102,9 +102,6 @@ struct ActivePostToolbarView: View {
}
}
})
} else {
EmptyView()
}
}
private func createPostUrl() -> [Any] {
@ -117,6 +114,9 @@ struct ActivePostToolbarView: View {
}
private func publishPost(_ post: WFAPost) {
if post != model.selectedPost {
return
}
DispatchQueue.main.async {
LocalStorageManager().saveContext()
model.publish(post: post)