mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Add move(post:from:to:) method to WriteFreelyModel
This commit is contained in:
parent
b216b576c8
commit
44f03b59d5
@ -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 {
|
||||
|
@ -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: {
|
||||
|
Loading…
Reference in New Issue
Block a user