Merge pull request #25 from writeas/implement-publishing

Implement publishing
This commit is contained in:
Angelo Stavrow 2020-08-27 13:16:15 -04:00 committed by GitHub
commit b01ea3c29e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 3 deletions

View File

@ -39,6 +39,7 @@ class Post: Identifiable, ObservableObject {
status: .published,
collection: collection
)
self.wfPost = wfPost
}
}

View File

@ -9,7 +9,6 @@ class WriteFreelyModel: ObservableObject {
@Published var preferences = PreferencesModel()
@Published var store = PostStore()
@Published var collections = CollectionListModel(with: [])
@Published var post: Post?
@Published var isLoggingIn: Bool = false
private var client: WFClient?
@ -63,6 +62,37 @@ extension WriteFreelyModel {
guard let loggedInClient = client else { return }
loggedInClient.getPosts(completion: fetchUserPostsHandler)
}
func publish(post: Post) {
guard let loggedInClient = client else { return }
if post.wfPost == nil {
// This is a new local draft.
post.wfPost = WFPost(
body: post.body,
title: post.title,
createdDate: post.createdDate
)
loggedInClient.createPost(
post: post.wfPost!, in: post.collection.wfCollection?.alias, completion: publishHandler
)
} else {
// This is an existing post.
// 1. Update Post.wfPost properties from (redundant) Post properties
// FIXME: https://github.com/writeas/writefreely-swiftui-multiplatform/issues/27
guard var publishedPost = post.wfPost else { return }
publishedPost.title = post.title
publishedPost.body = post.body
publishedPost.createdDate = post.createdDate
// 2. Update the post on the server and call the handler
loggedInClient.updatePost(
postId: publishedPost.postId!,
updatedPost: publishedPost,
completion: publishHandler
)
}
}
}
private extension WriteFreelyModel {
@ -177,6 +207,21 @@ private extension WriteFreelyModel {
print(error)
}
}
func publishHandler(result: Result<WFPost, Error>) {
do {
let wfPost = try result.get()
let foundPostIndex = store.posts.firstIndex(where: {
$0.title == wfPost.title && $0.body == wfPost.body
})
guard let index = foundPostIndex else { return }
DispatchQueue.main.async {
self.store.posts[index].wfPost = wfPost
}
} catch {
print(error)
}
}
}
private extension WriteFreelyModel {

View File

@ -32,6 +32,7 @@ struct PostEditorView: View {
}
ToolbarItem(placement: .primaryAction) {
Button(action: {
model.publish(post: post)
post.status = .published
}, label: {
Image(systemName: "paperplane")

View File

@ -7,12 +7,12 @@
<key>WriteFreely-MultiPlatform (iOS).xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>0</integer>
</dict>
<key>WriteFreely-MultiPlatform (macOS).xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
<integer>1</integer>
</dict>
</dict>
</dict>