From ec55801effa95628cb5468d62e0d22871dd5e12f Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Wed, 12 Jul 2023 17:07:34 +0800 Subject: [PATCH] Better undelete handling --- .../App/Views/Home/HomeFeedViewIOS.swift | 67 ++++++++++--------- .../App/Views/Home/HomeFeedViewModel.swift | 18 +++-- 2 files changed, 48 insertions(+), 37 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift index c8b32a58e..965b1bf40 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift @@ -318,39 +318,46 @@ struct AnimatingCellHeight: AnimatableModifier { func menuItems(for item: LinkedItem) -> some View { Group { - Button( - action: { viewModel.itemUnderTitleEdit = item }, - label: { Label("Edit Info", systemImage: "info.circle") } - ) - Button( - action: { viewModel.itemUnderLabelEdit = item }, - label: { Label(item.labels?.count == 0 ? "Add Labels" : "Edit Labels", systemImage: "tag") } - ) - Button(action: { - withAnimation(.linear(duration: 0.4)) { - viewModel.setLinkArchived( - dataService: dataService, - objectID: item.objectID, - archived: !item.isArchived + if item.state != "DELETED" { + Button( + action: { viewModel.itemUnderTitleEdit = item }, + label: { Label("Edit Info", systemImage: "info.circle") } + ) + Button( + action: { viewModel.itemUnderLabelEdit = item }, + label: { Label(item.labels?.count == 0 ? "Add Labels" : "Edit Labels", systemImage: "tag") } + ) + Button(action: { + withAnimation(.linear(duration: 0.4)) { + viewModel.setLinkArchived( + dataService: dataService, + objectID: item.objectID, + archived: !item.isArchived + ) + } + }, label: { + Label( + item.isArchived ? "Unarchive" : "Archive", + systemImage: item.isArchived ? "tray.and.arrow.down.fill" : "archivebox" + ) + }) + Button("Remove Item", role: .destructive) { + viewModel.removeLink(dataService: dataService, objectID: item.objectID) + } + if let author = item.author { + Button( + action: { + viewModel.searchTerm = "author:\"\(author)\"" + }, + label: { + Label(String("More by \(author)"), systemImage: "person") + } ) } - }, label: { - Label( - item.isArchived ? "Unarchive" : "Archive", - systemImage: item.isArchived ? "tray.and.arrow.down.fill" : "archivebox" - ) - }) - Button("Remove Item", role: .destructive) { - viewModel.removeLink(dataService: dataService, objectID: item.objectID) - } - if let author = item.author { + } else { Button( - action: { - viewModel.searchTerm = "author:\"\(author)\"" - }, - label: { - Label(String("More by \(author)"), systemImage: "person") - } + action: { viewModel.undeleteItem(dataService: dataService, itemID: item.unwrappedID) }, + label: { Label("Undelete", systemImage: "trash.slash") } ) } } diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift index 3325576f9..613900d6c 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift @@ -325,17 +325,21 @@ import Views dataService.removeLink(objectID: objectID) Snackbar.show(message: "Link deleted", undoAction: { - Task { - if await dataService.undeleteItem(itemID: itemID) { - Snackbar.show(message: "Link undeleted") - } else { - Snackbar.show(message: "Error. Check trash to recover.") - } - } + self.undeleteItem(dataService: dataService, itemID: itemID) }) } } + func undeleteItem(dataService: DataService, itemID: String) { + Task { + if await dataService.undeleteItem(itemID: itemID) { + Snackbar.show(message: "Link undeleted") + } else { + Snackbar.show(message: "Error. Check trash to recover.") + } + } + } + func getOrCreateLabel(dataService: DataService, named: String, color: String) -> LinkedItemLabel? { if let label = LinkedItemLabel.named(named, inContext: dataService.viewContext) { return label