diff --git a/Shared/PostList/PostListFilteredView.swift b/Shared/PostList/PostListFilteredView.swift index cf09a4e..84b8c3b 100644 --- a/Shared/PostList/PostListFilteredView.swift +++ b/Shared/PostList/PostListFilteredView.swift @@ -1,61 +1,90 @@ import SwiftUI struct PostListFilteredView: View { @EnvironmentObject var model: WriteFreelyModel var fetchRequest: FetchRequest init(filter: String?, showAllPosts: Bool) { if showAllPosts { fetchRequest = FetchRequest( entity: WFAPost.entity(), sortDescriptors: [NSSortDescriptor(key: "createdDate", ascending: false)] ) } else { if let filter = filter { fetchRequest = FetchRequest( entity: WFAPost.entity(), sortDescriptors: [NSSortDescriptor(key: "createdDate", ascending: false)], predicate: NSPredicate(format: "collectionAlias == %@", filter) ) } else { fetchRequest = FetchRequest( entity: WFAPost.entity(), sortDescriptors: [NSSortDescriptor(key: "createdDate", ascending: false)], predicate: NSPredicate(format: "collectionAlias == nil") ) } } } var body: some View { + #if os(iOS) List { ForEach(fetchRequest.wrappedValue, id: \.self) { post in NavigationLink( destination: PostEditorView(post: post), tag: post, selection: $model.selectedPost ) { PostCellView(post: post) } .deleteDisabled(post.status != PostStatus.local.rawValue) } .onDelete(perform: { indexSet in for index in indexSet { let post = fetchRequest.wrappedValue[index] delete(post) } }) } + #else + List { + ForEach(fetchRequest.wrappedValue, id: \.self) { post in + NavigationLink( + destination: PostEditorView(post: post), + tag: post, + selection: $model.selectedPost + ) { + PostCellView(post: post) + } + .deleteDisabled(post.status != PostStatus.local.rawValue) + } + .onDelete(perform: { indexSet in + for index in indexSet { + let post = fetchRequest.wrappedValue[index] + delete(post) + } + }) + } + .onDeleteCommand(perform: { + guard let selectedPost = model.selectedPost else { return } + if selectedPost.status == PostStatus.local.rawValue { + delete(selectedPost) + } + }) + #endif } func delete(_ post: WFAPost) { - model.posts.remove(post) + withAnimation { + model.posts.remove(post) + } } } struct PostListFilteredView_Previews: PreviewProvider { static var previews: some View { return PostListFilteredView(filter: nil, showAllPosts: false) } }