Add labels to highlights

This commit is contained in:
Jackson Harper
2022-11-28 16:19:00 +08:00
parent 179d791a73
commit aef72f00f0
6 changed files with 47 additions and 5 deletions

View File

@ -13,6 +13,7 @@
<attribute name="shortId" attributeType="String"/>
<attribute name="suffix" optional="YES" attributeType="String"/>
<attribute name="updatedAt" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<relationship name="labels" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="LinkedItemLabel" inverseName="highlight" inverseEntity="LinkedItemLabel"/>
<relationship name="linkedItem" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="LinkedItem" inverseName="highlights" inverseEntity="LinkedItem"/>
<uniquenessConstraints>
<uniquenessConstraint>
@ -67,6 +68,7 @@
<attribute name="labelDescription" optional="YES" attributeType="String"/>
<attribute name="name" attributeType="String"/>
<attribute name="serverSyncStatus" attributeType="Integer 64" defaultValueString="NO" usesScalarValueType="YES"/>
<relationship name="highlight" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Highlight" inverseName="labels" inverseEntity="Highlight"/>
<relationship name="linkedItems" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="LinkedItem" inverseName="labels" inverseEntity="LinkedItem"/>
<uniquenessConstraints>
<uniquenessConstraint>

View File

@ -21,7 +21,8 @@ extension DataService {
annotation: annotation,
createdAt: nil,
updatedAt: nil,
createdByMe: true
createdByMe: true,
labels: []
)
internalHighlight.persist(context: backgroundContext, associatedItemID: articleId)

View File

@ -23,7 +23,8 @@ extension DataService {
annotation: nil,
createdAt: nil,
updatedAt: nil,
createdByMe: true
createdByMe: true,
labels: []
)
internalHighlight.persist(

View File

@ -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) ?? []
)
}

View File

@ -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<Models.Highlight> = 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)
)
}

View File

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