listless

A simple list app for Apple platforms
Log | Files | Refs | README | LICENSE

ItemListView+Undo.swift (2457B)


      1 import SwiftUI
      2 
      3 extension ItemListView {
      4 
      5     func deleteItemWithUndo(itemID: UUID) {
      6         deleteItem(itemID: itemID)
      7         showUndoToast(message: "Item deleted")
      8     }
      9 
     10     func deleteSelectedItemWithUndo() -> KeyPress.Result {
     11         guard focusedField == .scrollView else {
     12             return .ignored
     13         }
     14         guard let currentID = fState.selectedItemID else {
     15             return .handled
     16         }
     17         guard allItemsInDisplayOrder.contains(where: { $0.id == currentID }) else {
     18             return .handled
     19         }
     20         deleteItemWithUndo(itemID: currentID)
     21         return .handled
     22     }
     23 
     24     func deleteAllItemsWithUndo() {
     25         let ids = items.map(\.id)
     26         guard !ids.isEmpty else { return }
     27         let count = ids.count
     28         managedObjectContext.undoManager?.beginUndoGrouping()
     29         do {
     30             try store.deleteMultiple(itemIDs: ids)
     31         } catch {
     32             presentStoreError(error)
     33             managedObjectContext.undoManager?.endUndoGrouping()
     34             return
     35         }
     36         managedObjectContext.undoManager?.endUndoGrouping()
     37         fState.pruneDeletedItems(displayOrder: allItemsInDisplayOrder.map(\.id))
     38         let noun = count == 1 ? "item" : "items"
     39         showUndoToast(message: "\(count) \(noun) deleted")
     40     }
     41 
     42     func clearCompletedItemsWithUndo() {
     43         let ids = completedItems.map(\.id)
     44         guard !ids.isEmpty else { return }
     45         let count = ids.count
     46         managedObjectContext.undoManager?.beginUndoGrouping()
     47         do {
     48             try store.deleteMultiple(itemIDs: ids)
     49         } catch {
     50             presentStoreError(error)
     51             managedObjectContext.undoManager?.endUndoGrouping()
     52             return
     53         }
     54         managedObjectContext.undoManager?.endUndoGrouping()
     55         fState.pruneDeletedItems(displayOrder: allItemsInDisplayOrder.map(\.id))
     56         let noun = count == 1 ? "item" : "items"
     57         showUndoToast(message: "\(count) \(noun) cleared")
     58     }
     59 
     60     func showUndoToast(message: String) {
     61         withAnimation {
     62             iState.undoToast = UndoToastData(id: UUID(), message: message)
     63         }
     64     }
     65 
     66     func performUndo() {
     67         managedObjectContext.undoManager?.undo()
     68         do {
     69             try store.save()
     70         } catch {
     71             presentStoreError(error)
     72         }
     73         dismissUndoToast()
     74     }
     75 
     76     func dismissUndoToast() {
     77         withAnimation {
     78             iState.undoToast = nil
     79         }
     80     }
     81 }