Delete local post via swipe

This commit is contained in:
Angelo Stavrow 2020-09-17 10:34:48 -04:00
parent d7a6668917
commit edec5e9087
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
2 changed files with 27 additions and 11 deletions

View File

@ -2,6 +2,7 @@ import SwiftUI
struct PostListFilteredView: View {
@EnvironmentObject var model: WriteFreelyModel
var fetchRequest: FetchRequest<WFAPost>
init(filter: String?, showAllPosts: Bool) {
@ -28,23 +29,33 @@ struct PostListFilteredView: View {
}
var body: some View {
List(fetchRequest.wrappedValue, id: \.self) { post in
NavigationLink(
destination: PostEditorView(post: post),
tag: post,
selection: $model.selectedPost
) {
PostCellView(post: post)
}
List {
ForEach(fetchRequest.wrappedValue, id: \.self) { post in
NavigationLink(
destination: PostEditorView(post: post),
tag: post,
selection: $model.selectedPost
) {
PostCellView(post: post)
}
}.onDelete(perform: { indexSet in
for index in indexSet {
let post = fetchRequest.wrappedValue[index]
if post.status == PostStatus.local.rawValue {
delete(post)
}
}
})
}
}
func delete(_ post: WFAPost) {
model.posts.remove(post)
}
}
struct PostListFilteredView_Previews: PreviewProvider {
static var previews: some View {
let context = LocalStorageManager.persistentContainer.viewContext
return PostListFilteredView(filter: nil, showAllPosts: false)
.environment(\.managedObjectContext, context)
}
}

View File

@ -22,6 +22,11 @@ class PostListModel: ObservableObject {
}
}
func remove(_ post: WFAPost) {
LocalStorageManager.persistentContainer.viewContext.delete(post)
LocalStorageManager().saveContext()
}
func purgeAllPosts() {
userPosts = []
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "WFAPost")