Page MenuHomeMusing Studio

No OneTemporary

diff --git a/Shared/PostList/PostListFilteredView.swift b/Shared/PostList/PostListFilteredView.swift
index f89f081..aceb999 100644
--- a/Shared/PostList/PostListFilteredView.swift
+++ b/Shared/PostList/PostListFilteredView.swift
@@ -1,50 +1,61 @@
import SwiftUI
struct PostListFilteredView: View {
@EnvironmentObject var model: WriteFreelyModel
+
var fetchRequest: FetchRequest<WFAPost>
init(filter: String?, showAllPosts: Bool) {
if showAllPosts {
fetchRequest = FetchRequest<WFAPost>(
entity: WFAPost.entity(),
sortDescriptors: [NSSortDescriptor(key: "createdDate", ascending: false)]
)
} else {
if let filter = filter {
fetchRequest = FetchRequest<WFAPost>(
entity: WFAPost.entity(),
sortDescriptors: [NSSortDescriptor(key: "createdDate", ascending: false)],
predicate: NSPredicate(format: "collectionAlias == %@", filter)
)
} else {
fetchRequest = FetchRequest<WFAPost>(
entity: WFAPost.entity(),
sortDescriptors: [NSSortDescriptor(key: "createdDate", ascending: false)],
predicate: NSPredicate(format: "collectionAlias == nil")
)
}
}
}
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)
}
}
diff --git a/Shared/PostList/PostListModel.swift b/Shared/PostList/PostListModel.swift
index aab1ebd..9dea70a 100644
--- a/Shared/PostList/PostListModel.swift
+++ b/Shared/PostList/PostListModel.swift
@@ -1,36 +1,41 @@
import SwiftUI
import CoreData
class PostListModel: ObservableObject {
@Published var userPosts = [WFAPost]()
init() {
loadCachedPosts()
}
func loadCachedPosts() {
let request = WFAPost.createFetchRequest()
let sort = NSSortDescriptor(key: "createdDate", ascending: false)
request.sortDescriptors = [sort]
userPosts = []
do {
let cachedPosts = try LocalStorageManager.persistentContainer.viewContext.fetch(request)
userPosts.append(contentsOf: cachedPosts)
} catch {
print("Error: Failed to fetch cached posts.")
}
}
+ func remove(_ post: WFAPost) {
+ LocalStorageManager.persistentContainer.viewContext.delete(post)
+ LocalStorageManager().saveContext()
+ }
+
func purgeAllPosts() {
userPosts = []
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "WFAPost")
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
do {
try LocalStorageManager.persistentContainer.viewContext.executeAndMergeChanges(using: deleteRequest)
} catch {
print("Error: Failed to purge cached posts.")
}
}
}

File Metadata

Mime Type
text/x-diff
Expires
Fri, Jan 31, 2:55 PM (10 h, 5 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3145811

Event Timeline