From 0167ecc652627650220943924dad9269ec4c1c0a Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Thu, 28 Mar 2024 21:09:22 +0800 Subject: [PATCH] Fix swipe to archive not triggering a list update --- .../App/Views/Home/HomeFeedViewModel.swift | 3 +- .../App/Views/RemoveLibraryItemAction.swift | 52 +++++++++++++++++-- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift index 27b42b75b..815e85a54 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift @@ -250,8 +250,7 @@ enum LoadingBarStyle { } func setLinkArchived(dataService: DataService, objectID: NSManagedObjectID, archived: Bool) { - dataService.archiveLink(objectID: objectID, archived: archived) - snackbar(archived ? "Link archived" : "Link unarchived") + archiveLibraryItemAction(dataService: dataService, objectID: objectID, archived: archived) } func removeLibraryItem(dataService: DataService, objectID: NSManagedObjectID) { diff --git a/apple/OmnivoreKit/Sources/App/Views/RemoveLibraryItemAction.swift b/apple/OmnivoreKit/Sources/App/Views/RemoveLibraryItemAction.swift index c728509ee..ceaa39cd0 100644 --- a/apple/OmnivoreKit/Sources/App/Views/RemoveLibraryItemAction.swift +++ b/apple/OmnivoreKit/Sources/App/Views/RemoveLibraryItemAction.swift @@ -7,18 +7,20 @@ import Utils import Views func removeLibraryItemAction(dataService: DataService, objectID: NSManagedObjectID) { + var localPdf: String? = nil + dataService.viewContext.performAndWait { if let item = dataService.viewContext.object(with: objectID) as? Models.LibraryItem { item.state = "DELETED" try? dataService.viewContext.save() - - // Delete local PDF file if it exists - if let localPdf = item.localPDF, let localPdfURL = PDFUtils.localPdfURL(filename: localPdf) { - try? FileManager.default.removeItem(at: localPdfURL) - } + localPdf = item.localPDF } } + if let localPdf = localPdf, let localPdfURL = PDFUtils.localPdfURL(filename: localPdf) { + try? FileManager.default.removeItem(at: localPdfURL) + } + let syncTask = Task.detached(priority: .background) { do { try await Task.sleep(nanoseconds: 4_000_000_000) @@ -44,3 +46,43 @@ func removeLibraryItemAction(dataService: DataService, objectID: NSManagedObject } }, dismissAfter: 2000) } + +func archiveLibraryItemAction(dataService: DataService, objectID: NSManagedObjectID, archived: Bool) { + var localPdf: String? = nil + dataService.viewContext.performAndWait { + if let item = dataService.viewContext.object(with: objectID) as? Models.LibraryItem { + item.isArchived = archived + try? dataService.viewContext.save() + localPdf = item.localPDF + } + } + + // Delete local PDF file if it exists + if let localPdf = localPdf, let localPdfURL = PDFUtils.localPdfURL(filename: localPdf) { + try? FileManager.default.removeItem(at: localPdfURL) + } + + let syncTask = Task.detached(priority: .background) { + do { + try await Task.sleep(nanoseconds: 4_000_000_000) + let canceled = Task.isCancelled + if !canceled { + dataService.archiveLink(objectID: objectID, archived: archived) + } + } catch { + print("error running task: ", error) + } + print("checking if task is canceled: ", Task.isCancelled) + } + + Snackbar.show(message: "Item archived", undoAction: { + print("canceling task", syncTask) + syncTask.cancel() + dataService.viewContext.performAndWait { + if let item = dataService.viewContext.object(with: objectID) as? Models.LibraryItem { + item.state = "SUCCEEDED" + try? dataService.viewContext.save() + } + } + }, dismissAfter: 2000) +}