From aef72f00f0fbfabd2ad6c5fdb27a7b4d3b2e6b6a Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Mon, 28 Nov 2022 16:19:00 +0800 Subject: [PATCH] Add labels to highlights --- .../CoreDataModel.xcdatamodel/contents | 2 ++ .../Mutations/CreateHighlight.swift | 3 ++- .../DataService/Mutations/MergeHighlight.swift | 3 ++- .../Selections/HighlightSelection.swift | 13 ++++++++++++- .../InternalModels/InternalHighlight.swift | 13 ++++++++++++- .../InternalLinkedItemLabel.swift | 18 +++++++++++++++++- 6 files changed, 47 insertions(+), 5 deletions(-) diff --git a/apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents b/apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents index cbd574d78..b46bf97e7 100644 --- a/apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents +++ b/apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents @@ -13,6 +13,7 @@ + @@ -67,6 +68,7 @@ + diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateHighlight.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateHighlight.swift index 5cd1ad36d..66878c579 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateHighlight.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/CreateHighlight.swift @@ -21,7 +21,8 @@ extension DataService { annotation: annotation, createdAt: nil, updatedAt: nil, - createdByMe: true + createdByMe: true, + labels: [] ) internalHighlight.persist(context: backgroundContext, associatedItemID: articleId) diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/MergeHighlight.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/MergeHighlight.swift index cb3e2c0d5..cdf78dc58 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/MergeHighlight.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/MergeHighlight.swift @@ -23,7 +23,8 @@ extension DataService { annotation: nil, createdAt: nil, updatedAt: nil, - createdByMe: true + createdByMe: true, + labels: [] ) internalHighlight.persist( diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Selections/HighlightSelection.swift b/apple/OmnivoreKit/Sources/Services/DataService/Selections/HighlightSelection.swift index 173dfe020..5deb56277 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Selections/HighlightSelection.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Selections/HighlightSelection.swift @@ -1,6 +1,16 @@ import Models import SwiftGraphQL +let highlightLabelSelection = Selection.Label { + InternalLinkedItemLabel( + id: try $0.id(), + name: try $0.name(), + color: try $0.color(), + createdAt: try $0.createdAt()?.value, + labelDescription: try $0.description() + ) +} + let highlightSelection = Selection.Highlight { InternalHighlight( id: try $0.id(), @@ -12,6 +22,7 @@ let highlightSelection = Selection.Highlight { annotation: try $0.annotation(), createdAt: try $0.createdAt().value, updatedAt: try $0.updatedAt().value, - createdByMe: try $0.createdByMe() + createdByMe: try $0.createdByMe(), + labels: try $0.labels(selection: highlightLabelSelection.list.nullable) ?? [] ) } diff --git a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalHighlight.swift b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalHighlight.swift index 450d5e759..e1640bcb8 100644 --- a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalHighlight.swift +++ b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalHighlight.swift @@ -13,6 +13,7 @@ struct InternalHighlight: Encodable { let createdAt: Date? let updatedAt: Date? let createdByMe: Bool + var labels: [InternalLinkedItemLabel] func asManagedObject(context: NSManagedObjectContext) -> Highlight { let fetchRequest: NSFetchRequest = Highlight.fetchRequest() @@ -33,6 +34,15 @@ struct InternalHighlight: Encodable { highlight.createdAt = createdAt highlight.updatedAt = updatedAt highlight.createdByMe = createdByMe + + if let existingLabels = highlight.labels { + highlight.removeFromLabels(existingLabels) + } + + for label in labels { + highlight.addToLabels(label.asManagedObject(inContext: context)) + } + return highlight } @@ -47,7 +57,8 @@ struct InternalHighlight: Encodable { annotation: highlight.annotation, createdAt: highlight.createdAt, updatedAt: highlight.updatedAt, - createdByMe: highlight.createdByMe + createdByMe: highlight.createdByMe, + labels: InternalLinkedItemLabel.make(highlight.labels) ) } diff --git a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalLinkedItemLabel.swift b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalLinkedItemLabel.swift index d6707c7b1..301f341a0 100644 --- a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalLinkedItemLabel.swift +++ b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalLinkedItemLabel.swift @@ -2,7 +2,7 @@ import CoreData import Foundation import Models -struct InternalLinkedItemLabel { +struct InternalLinkedItemLabel: Encodable { let id: String let name: String let color: String @@ -38,6 +38,22 @@ struct InternalLinkedItemLabel { label.labelDescription = labelDescription return label } + + static func make(_ labels: NSSet?) -> [InternalLinkedItemLabel] { + labels? + .compactMap { label in + if let label = label as? LinkedItemLabel { + return InternalLinkedItemLabel( + id: label.id ?? "", + name: label.name ?? "", + color: label.color ?? "", + createdAt: label.createdAt, + labelDescription: label.labelDescription + ) + } + return nil + } ?? [] + } } extension LinkedItemLabel {