Skip to content

Commit

Permalink
Fix annotation color in dark appearance (zotero#1004)
Browse files Browse the repository at this point in the history
* Fix annotation color when added in dark appearance

* Add annotation color correction migration
  • Loading branch information
mvasilak committed Sep 3, 2024
1 parent 8a485da commit 6576570
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
42 changes: 38 additions & 4 deletions Zotero/Models/Database/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import RealmSwift
import Network

struct Database {
private static let schemaVersion: UInt64 = 45
private static let schemaVersion: UInt64 = 46

static func mainConfiguration(url: URL, fileStorage: FileStorage) -> Realm.Configuration {
var config = Realm.Configuration(
Expand Down Expand Up @@ -86,6 +86,42 @@ struct Database {
if schemaVersion < 45 {
extractAnnotationTypeFromItems(migration: migration)
}
if schemaVersion < 46 {
correctAnnotationColors(migration: migration)
}
}
}

private static func correctAnnotationColors(migration: Migration) {
let colorVariationMap = AnnotationsConfig.colorVariationMap
migration.enumerateObjects(ofType: RItem.className()) { oldObject, newObject in
guard let oldObject,
let newObject,
oldObject["rawType"] as? String == ItemTypes.annotation,
let fields = oldObject["fields"] as? List<MigrationObject>,
let index = fields.firstIndex(where: { $0["key"] as? String == FieldKeys.Item.Annotation.color }),
let color = fields[index]["value"] as? String,
let newColor = AnnotationsConfig.colorVariationMap[color],
color != newColor
else { return }

(newObject["fields"] as? List<MigrationObject>)?[index]["value"] = newColor
(newObject["fields"] as? List<MigrationObject>)?[index]["changed"] = true

let itemChange = RItemChanges.fields
let newChanges = List<RObjectChange>()
newChanges.append(RObjectChange.create(changes: itemChange))
if let oldChanges = oldObject["changes"] as? List<MigrationObject> {
for oldChange in oldChanges {
if let oldIdentifier = oldChange["identifier"] as? String, let oldRawChanges = oldChange["rawChanges"] as? Int16, oldRawChanges != itemChange.rawValue {
let existingChange = RObjectChange()
existingChange.identifier = oldIdentifier
existingChange.rawChanges = oldRawChanges
newChanges.append(existingChange)
}
}
}
newObject["changes"] = newChanges
}
}

Expand Down Expand Up @@ -138,9 +174,7 @@ struct Database {
newChanges.append(RObjectChange.create(changes: itemChange))
if let oldChanges = oldObject["changes"] as? List<MigrationObject> {
for oldChange in oldChanges {
if let oldIdentifier = oldChange["identifier"] as? String,
let oldRawChanges = oldChange["rawChanges"] as? Int16,
oldRawChanges != itemChange.rawValue {
if let oldIdentifier = oldChange["identifier"] as? String, let oldRawChanges = oldChange["rawChanges"] as? Int16, oldRawChanges != itemChange.rawValue {
let existingChange = RObjectChange()
existingChange.identifier = oldIdentifier
existingChange.rawChanges = oldRawChanges
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1415,21 +1415,16 @@ final class PDFReaderActionHandler: ViewModelActionHandler, BackgroundDbProcessi

guard !finalAnnotations.isEmpty else { return }
let documentAnnotations: [PDFDocumentAnnotation] = finalAnnotations.compactMap { annotation in
var color: UIColor? = annotation.color
if color == nil, let tool = tool(from: annotation), let activeColor = viewModel.state.toolColors[tool] {
color = activeColor
}
guard let color else { return nil }
let documentAnnotation = AnnotationConverter.annotation(
from: annotation,
color: color.hexString,
color: annotation.baseColor,
library: viewModel.state.library,
username: viewModel.state.username,
displayName: viewModel.state.displayName,
boundingBoxConverter: boundingBoxConverter
)
guard let documentAnnotation else { return nil }
// Only create preview for annotations that will be
// Only create preview for annotations that will be added in the database.
annotationPreviewController.store(for: annotation, parentKey: viewModel.state.key, libraryId: viewModel.state.library.identifier, isDark: (viewModel.state.interfaceStyle == .dark))
return documentAnnotation
}
Expand Down

0 comments on commit 6576570

Please sign in to comment.