Add move(post:from:to:) method to WriteFreelyModel

This commit is contained in:
Angelo Stavrow 2020-10-08 12:05:10 -04:00
parent b216b576c8
commit 44f03b59d5
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
2 changed files with 39 additions and 6 deletions

View File

@ -174,6 +174,22 @@ extension WriteFreelyModel {
loggedInClient.getPost(byId: postId, completion: updateFromServerHandler)
}
}
func move(post: WFAPost, from oldCollection: WFACollection?, to newCollection: WFACollection?) {
guard let loggedInClient = client,
let postId = post.postId else { return }
if let newCollectionAlias = newCollection?.alias {
post.collectionAlias = newCollectionAlias
loggedInClient.movePost(postId: postId, to: newCollectionAlias, completion: movePostHandler)
} else {
// Moving a post to Drafts is not yet supported by the Swift package.
DispatchQueue.main.async {
post.collectionAlias = oldCollection?.alias
self.selectedPost = nil
}
}
}
}
private extension WriteFreelyModel {
@ -379,6 +395,23 @@ private extension WriteFreelyModel {
print(error)
}
}
func movePostHandler(result: Result<Bool, Error>) {
do {
let succeeded = try result.get()
if succeeded {
DispatchQueue.main.async {
LocalStorageManager().saveContext()
self.posts.loadCachedPosts()
}
}
} catch {
DispatchQueue.main.async {
LocalStorageManager.persistentContainer.viewContext.rollback()
}
print(error)
}
}
}
private extension WriteFreelyModel {

View File

@ -195,12 +195,12 @@ struct PostEditorView: View {
}
}
})
.onChange(of: selectedCollection, perform: { newValue in
if post.collectionAlias != newValue?.alias {
post.status = PostStatus.edited.rawValue
post.collectionAlias = newValue?.alias
model.posts.loadCachedPosts()
LocalStorageManager().saveContext()
.onChange(of: selectedCollection, perform: { [selectedCollection] newCollection in
if post.collectionAlias == newCollection?.alias {
return
} else {
post.collectionAlias = newCollection?.alias
model.move(post: post, from: selectedCollection, to: newCollection)
}
})
.onAppear(perform: {