Start to implement document note deletion

This commit is contained in:
Jackson Harper 2023-04-07 18:12:10 +08:00
parent 40d0c52c2e
commit 42a3b2fe87
3 changed files with 57 additions and 4 deletions

View file

@ -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")

View file

@ -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 {
//
}
}
}

View file

@ -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)