mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Delete local post via swipe
This commit is contained in:
parent
d7a6668917
commit
edec5e9087
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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")
|
||||
|
Loading…
Reference in New Issue
Block a user