persist annotation changes from highlights view
This commit is contained in:
@ -7,7 +7,8 @@ struct HighlightsListCard: View {
|
||||
@State var annotation = String()
|
||||
@State var showAnnotationModal = false
|
||||
|
||||
let highlight: Highlight
|
||||
let highlightParams: HighlightListItemParams
|
||||
let onSaveAnnotation: (String) -> Void
|
||||
|
||||
var contextMenuView: some View {
|
||||
Group {
|
||||
@ -35,10 +36,10 @@ struct HighlightsListCard: View {
|
||||
Spacer()
|
||||
}
|
||||
|
||||
Text(highlight.annotation ?? "")
|
||||
Text(highlightParams.annotation)
|
||||
}
|
||||
.onTapGesture {
|
||||
annotation = highlight.annotation ?? ""
|
||||
annotation = highlightParams.annotation
|
||||
showAnnotationModal = true
|
||||
}
|
||||
}
|
||||
@ -55,7 +56,7 @@ struct HighlightsListCard: View {
|
||||
Spacer()
|
||||
}
|
||||
.onTapGesture {
|
||||
annotation = highlight.annotation ?? ""
|
||||
annotation = highlightParams.annotation
|
||||
showAnnotationModal = true
|
||||
}
|
||||
}
|
||||
@ -65,7 +66,7 @@ struct HighlightsListCard: View {
|
||||
HStack {
|
||||
Image(systemName: "highlighter")
|
||||
|
||||
Text(highlight.highlightCardTitle)
|
||||
Text(highlightParams.title)
|
||||
.font(.appHeadline)
|
||||
.foregroundColor(.appGrayTextContrast)
|
||||
.lineLimit(1)
|
||||
@ -91,11 +92,11 @@ struct HighlightsListCard: View {
|
||||
.overlay(Color.appYellow48)
|
||||
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
Text(highlight.quote ?? "")
|
||||
Text(highlightParams.quote)
|
||||
|
||||
Divider()
|
||||
|
||||
if highlight.annotation == nil || highlight.annotation?.isEmpty == true {
|
||||
if highlightParams.annotation.isEmpty {
|
||||
addNoteSection
|
||||
} else {
|
||||
noteSection
|
||||
@ -108,11 +109,10 @@ struct HighlightsListCard: View {
|
||||
HighlightAnnotationSheet(
|
||||
annotation: $annotation,
|
||||
onSave: {
|
||||
print("new annotation = \(annotation)")
|
||||
onSaveAnnotation(annotation)
|
||||
showAnnotationModal = false
|
||||
},
|
||||
onCancel: {
|
||||
print("cancel annotation")
|
||||
showAnnotationModal = false
|
||||
}
|
||||
)
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import CoreData
|
||||
import Models
|
||||
import Services
|
||||
import SwiftUI
|
||||
@ -8,13 +9,19 @@ struct HighlightsListView: View {
|
||||
@Environment(\.presentationMode) private var presentationMode
|
||||
@StateObject var viewModel = HighlightsListViewModel()
|
||||
|
||||
let item: LinkedItem
|
||||
let itemObjectID: NSManagedObjectID
|
||||
|
||||
var innerBody: some View {
|
||||
List {
|
||||
Section {
|
||||
ForEach(viewModel.highlights, id: \.self) { highlight in
|
||||
HighlightsListCard(highlight: highlight)
|
||||
ForEach(viewModel.highlightItems) { highlightParams in
|
||||
HighlightsListCard(highlightParams: highlightParams) { newAnnotation in
|
||||
viewModel.updateAnnotation(
|
||||
highlightID: highlightParams.highlightID,
|
||||
annotation: newAnnotation,
|
||||
dataService: dataService
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -55,7 +62,7 @@ struct HighlightsListView: View {
|
||||
#endif
|
||||
}
|
||||
.task {
|
||||
viewModel.load(item: item)
|
||||
viewModel.load(itemObjectID: itemObjectID, dataService: dataService)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,20 +4,50 @@ import Services
|
||||
import SwiftUI
|
||||
import Views
|
||||
|
||||
@MainActor final class HighlightsListViewModel: ObservableObject {
|
||||
@Published var highlights = [Highlight]()
|
||||
struct HighlightListItemParams: Identifiable {
|
||||
let id = UUID()
|
||||
let highlightID: String
|
||||
let title: String
|
||||
let annotation: String
|
||||
let quote: String
|
||||
}
|
||||
|
||||
func load(item: LinkedItem) {
|
||||
@MainActor final class HighlightsListViewModel: ObservableObject {
|
||||
@Published var highlightItems = [HighlightListItemParams]()
|
||||
|
||||
func load(itemObjectID: NSManagedObjectID, dataService: DataService) {
|
||||
if let linkedItem = dataService.viewContext.object(with: itemObjectID) as? LinkedItem {
|
||||
loadHighlights(item: linkedItem)
|
||||
}
|
||||
}
|
||||
|
||||
func updateAnnotation(highlightID: String, annotation: String, dataService: DataService) {
|
||||
dataService.updateHighlightAttributes(highlightID: highlightID, annotation: annotation)
|
||||
|
||||
if let index = highlightItems.firstIndex(where: { $0.highlightID == highlightID }) {
|
||||
highlightItems[index] = HighlightListItemParams(
|
||||
highlightID: highlightID,
|
||||
title: highlightItems[index].title,
|
||||
annotation: annotation,
|
||||
quote: highlightItems[index].quote
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private func loadHighlights(item: LinkedItem) {
|
||||
let unsortedHighlights = item.highlights.asArray(of: Highlight.self)
|
||||
|
||||
highlights = unsortedHighlights.sorted {
|
||||
let highlights = unsortedHighlights.sorted {
|
||||
($0.createdAt ?? Date()) < ($1.createdAt ?? Date())
|
||||
}
|
||||
|
||||
highlightItems = highlights.map {
|
||||
HighlightListItemParams(
|
||||
highlightID: $0.unwrappedID,
|
||||
title: "Highlight",
|
||||
annotation: $0.annotation ?? "",
|
||||
quote: $0.quote ?? ""
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Highlight {
|
||||
var highlightCardTitle: String {
|
||||
"Highlight"
|
||||
}
|
||||
}
|
||||
|
||||
@ -63,7 +63,7 @@ import Views
|
||||
LinkedItemTitleEditView(item: item)
|
||||
}
|
||||
.sheet(item: $viewModel.itemForHighlightsView) { item in
|
||||
HighlightsListView(item: item)
|
||||
HighlightsListView(itemObjectID: item.objectID)
|
||||
}
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .barTrailing) {
|
||||
|
||||
@ -204,7 +204,7 @@ struct WebReaderContainerView: View {
|
||||
LinkedItemTitleEditView(item: item)
|
||||
}
|
||||
.sheet(isPresented: $showHighlightsView) {
|
||||
HighlightsListView(item: item)
|
||||
HighlightsListView(itemObjectID: item.objectID)
|
||||
}
|
||||
#if os(macOS)
|
||||
.buttonStyle(PlainButtonStyle())
|
||||
|
||||
@ -87,6 +87,12 @@ public extension LinkedItem {
|
||||
}
|
||||
}
|
||||
|
||||
var sortedHighlights: [Highlight] {
|
||||
highlights.asArray(of: Highlight.self).sorted {
|
||||
($0.createdAt ?? Date()) < ($1.createdAt ?? Date())
|
||||
}
|
||||
}
|
||||
|
||||
var labelsJSONString: String {
|
||||
let labels = self.labels.asArray(of: LinkedItemLabel.self).map { label in
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user