mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Merge pull request #25 from writeas/implement-publishing
Implement publishing
This commit is contained in:
commit
b01ea3c29e
@ -39,6 +39,7 @@ class Post: Identifiable, ObservableObject {
|
||||
status: .published,
|
||||
collection: collection
|
||||
)
|
||||
self.wfPost = wfPost
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -32,6 +32,7 @@ struct PostEditorView: View {
|
||||
}
|
||||
ToolbarItem(placement: .primaryAction) {
|
||||
Button(action: {
|
||||
model.publish(post: post)
|
||||
post.status = .published
|
||||
}, label: {
|
||||
Image(systemName: "paperplane")
|
||||
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user