Files
omnivore/apple/OmnivoreKit/Sources/App/Views/RemoveLibraryItemAction.swift
Jackson Harper 03766734e5 Pull out popup view and replace with Transmission snackbars
PopupView and Transimission can interfere with each other and
cause an issue with the root screen becoming black and the app
getting stuck.
2024-02-06 11:43:43 +08:00

47 lines
1.4 KiB
Swift

import CoreData
import Foundation
import Models
import Services
import Utils
import Views
func removeLibraryItemAction(dataService: DataService, objectID: NSManagedObjectID) {
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)
}
}
}
let syncTask = Task.detached(priority: .background) {
do {
try await Task.sleep(nanoseconds: 4_000_000_000)
let canceled = Task.isCancelled
if !canceled {
print("syncing link deletion")
dataService.removeLibraryItem(objectID: objectID, sync: true)
}
} catch {
print("error running task: ", error)
}
print("checking if task is canceled: ", Task.isCancelled)
}
Snackbar.show(message: "Item removed", 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)
}