diff --git a/apple/OmnivoreKit/Sources/App/Views/Highlights/HighlightsListCard.swift b/apple/OmnivoreKit/Sources/App/Views/Highlights/HighlightsListCard.swift index fd4e3fd12..f488db303 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Highlights/HighlightsListCard.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Highlights/HighlightsListCard.swift @@ -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 } ) diff --git a/apple/OmnivoreKit/Sources/App/Views/Highlights/HighlightsListView.swift b/apple/OmnivoreKit/Sources/App/Views/Highlights/HighlightsListView.swift index 157a998d4..b3c0e848a 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Highlights/HighlightsListView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Highlights/HighlightsListView.swift @@ -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) } } } diff --git a/apple/OmnivoreKit/Sources/App/Views/Highlights/HighlightsListViewModel.swift b/apple/OmnivoreKit/Sources/App/Views/Highlights/HighlightsListViewModel.swift index a407be8a2..791d85c29 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Highlights/HighlightsListViewModel.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Highlights/HighlightsListViewModel.swift @@ -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" - } -} diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift index f74763fcf..85cb7c15b 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift @@ -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) { diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift index b13acc07a..a005c3808 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift @@ -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()) diff --git a/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift b/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift index e96ba857c..e8785777e 100644 --- a/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift +++ b/apple/OmnivoreKit/Sources/Models/DataModels/FeedItem.swift @@ -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 [