From f60ba672e77d46611d453e39d9b1bcadb53b2212 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Fri, 7 Apr 2023 21:00:01 +0800 Subject: [PATCH] Add highlight notes from the notebook --- .../Sources/App/Views/Highlights/NotebookView.swift | 1 + .../App/Views/Highlights/NotebookViewModel.swift | 12 +++++++++++- .../DataService/Mutations/CreateHighlight.swift | 6 +++--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/Highlights/NotebookView.swift b/apple/OmnivoreKit/Sources/App/Views/Highlights/NotebookView.swift index b7c25ec18..acffece50 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Highlights/NotebookView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Highlights/NotebookView.swift @@ -46,6 +46,7 @@ }.alert("Are you sure you want to delete the note?", isPresented: $showConfirmNoteDelete) { Button("Remove Item", role: .destructive) { + viewModel.deleteNote(dataService: dataService) showConfirmNoteDelete = false } Button(LocalText.cancelGeneric, role: .cancel) { diff --git a/apple/OmnivoreKit/Sources/App/Views/Highlights/NotebookViewModel.swift b/apple/OmnivoreKit/Sources/App/Views/Highlights/NotebookViewModel.swift index 3cd6fd695..759ee5d83 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Highlights/NotebookViewModel.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Highlights/NotebookViewModel.swift @@ -55,13 +55,23 @@ struct NoteItemParams: Identifiable { if let linkedItem = dataService.viewContext.object(with: itemObjectID) as? LinkedItem { noteItem = NoteItemParams(highlightID: highlightId, annotation: annotation) - let highlight = dataService.createNote(shortId: shortId, highlightID: highlightId, articleId: linkedItem.unwrappedID, annotation: annotation) + let highlight = dataService.createNote(shortId: shortId, + highlightID: highlightId, + articleId: linkedItem.unwrappedID, + annotation: annotation) } else { // } } } + func deleteNote(dataService: DataService) { + if let highlightID = noteItem?.highlightID { + dataService.deleteHighlight(highlightID: highlightID) + noteItem = nil + } + } + func deleteHighlight(highlightID: String, dataService: DataService) { dataService.deleteHighlight(highlightID: highlightID) highlightItems.removeAll { $0.highlightID == highlightID } diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateHighlight.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateHighlight.swift index fbbe087ab..fb0632672 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateHighlight.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateHighlight.swift @@ -91,10 +91,10 @@ public extension DataService { articleId: articleId, highlightPositionAnchorIndex: OptionalArgument(highlight.positionAnchorIndex), highlightPositionPercent: OptionalArgument(highlight.positionPercent), id: highlight.id, - patch: OptionalArgument(highlight.patch), - quote: OptionalArgument(highlight.quote), + patch: OptionalArgument(highlight.patch.isEmpty ? nil : highlight.patch), + quote: OptionalArgument(highlight.quote.isEmpty ? nil : highlight.quote), shortId: highlight.shortId, - type: OptionalArgument(Enums.HighlightType.highlight) + type: OptionalArgument(highlight.type == "NOTE" ? Enums.HighlightType.note : Enums.HighlightType.highlight) ), selection: selection )