From 42a3b2fe87377b2832ca305fd6b3cf673bae7e3a Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Fri, 7 Apr 2023 18:12:10 +0800 Subject: [PATCH] Start to implement document note deletion --- .../App/Views/Highlights/NotebookView.swift | 14 ++++++++ .../Views/Highlights/NotebookViewModel.swift | 12 ++++++- .../Mutations/CreateHighlight.swift | 35 +++++++++++++++++-- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/Highlights/NotebookView.swift b/apple/OmnivoreKit/Sources/App/Views/Highlights/NotebookView.swift index 417bd161e..b7c25ec18 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Highlights/NotebookView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Highlights/NotebookView.swift @@ -20,6 +20,7 @@ @Binding var hasHighlightMutations: Bool @State var setLabelsHighlight: Highlight? @State var showShareView: Bool = false + @State var showConfirmNoteDelete = false var emptyView: some View { Text(LocalText.highlightCardNoHighlightsOnPage) @@ -42,6 +43,14 @@ } }.formSheet(isPresented: $showShareView) { ShareSheet(activityItems: [viewModel.highlightsAsMarkdown()]) + }.alert("Are you sure you want to delete the note?", + isPresented: $showConfirmNoteDelete) { + Button("Remove Item", role: .destructive) { + showConfirmNoteDelete = false + } + Button(LocalText.cancelGeneric, role: .cancel) { + showConfirmNoteDelete = false + } } #else .toolbar { @@ -139,6 +148,7 @@ annotation: $noteAnnotation, onSave: { viewModel.updateNoteAnnotation( + itemObjectID: itemObjectID, annotation: noteAnnotation, dataService: dataService ) @@ -168,6 +178,10 @@ action: { showShareView = true }, label: { Label(LocalText.exportGeneric, systemImage: "square.and.arrow.up") } ) + Button( + action: { showConfirmNoteDelete = true }, + label: { Label("Delete Document Note", systemImage: "trash") } + ).padding() }, label: { Image(systemName: "ellipsis") diff --git a/apple/OmnivoreKit/Sources/App/Views/Highlights/NotebookViewModel.swift b/apple/OmnivoreKit/Sources/App/Views/Highlights/NotebookViewModel.swift index 1dd96bfd5..3cd6fd695 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Highlights/NotebookViewModel.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Highlights/NotebookViewModel.swift @@ -45,10 +45,20 @@ struct NoteItemParams: Identifiable { } } - func updateNoteAnnotation(annotation: String, dataService: DataService) { + func updateNoteAnnotation(itemObjectID: NSManagedObjectID, annotation: String, dataService: DataService) { if let noteItem = self.noteItem { dataService.updateHighlightAttributes(highlightID: noteItem.highlightID, annotation: annotation) self.noteItem = NoteItemParams(highlightID: noteItem.highlightID, annotation: annotation) + } else { + let highlightId = UUID().uuidString.lowercased() + let shortId = NanoID.generate(alphabet: NanoID.Alphabet.urlSafe.rawValue, size: 8) + + 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) + } else { + // + } } } diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateHighlight.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateHighlight.swift index 90bfda833..fbbe087ab 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateHighlight.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateHighlight.swift @@ -3,9 +3,9 @@ import Foundation import Models import SwiftGraphQL -extension DataService { +public extension DataService { // swiftlint:disable:next function_parameter_count - public func createHighlight( + func createHighlight( shortId: String, highlightID: String, quote: String, @@ -40,7 +40,36 @@ extension DataService { return internalHighlight.encoded() } - func syncHighlightCreation(highlight: InternalHighlight, articleId: String) { + func createNote( + shortId: String, + highlightID: String, + articleId: String, + annotation: String + ) -> [String: Any]? { + let internalHighlight = InternalHighlight( + id: highlightID, + type: "NOTE", + shortId: shortId, + quote: "", + prefix: nil, suffix: nil, + patch: "", + annotation: annotation, + createdAt: nil, + updatedAt: nil, + createdByMe: true, + createdBy: nil, + positionPercent: nil, + positionAnchorIndex: nil, + labels: [] + ) + + internalHighlight.persist(context: backgroundContext, associatedItemID: articleId) + syncHighlightCreation(highlight: internalHighlight, articleId: articleId) + + return internalHighlight.encoded() + } + + internal func syncHighlightCreation(highlight: InternalHighlight, articleId: String) { enum MutationResult { case saved(highlight: InternalHighlight) case error(errorCode: Enums.CreateHighlightErrorCode)