From 65765703c898c907398922825d2f2f7a1158c88f Mon Sep 17 00:00:00 2001 From: Miltiadis Vasilakis Date: Mon, 2 Sep 2024 15:47:04 +0300 Subject: [PATCH] Fix annotation color in dark appearance (#1004) * Fix annotation color when added in dark appearance * Add annotation color correction migration --- Zotero/Models/Database/Database.swift | 42 +++++++++++++++++-- .../ViewModels/PDFReaderActionHandler.swift | 9 +--- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/Zotero/Models/Database/Database.swift b/Zotero/Models/Database/Database.swift index 1b2acd10b..cad39b78c 100644 --- a/Zotero/Models/Database/Database.swift +++ b/Zotero/Models/Database/Database.swift @@ -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( @@ -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, + 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)?[index]["value"] = newColor + (newObject["fields"] as? List)?[index]["changed"] = true + + let itemChange = RItemChanges.fields + let newChanges = List() + newChanges.append(RObjectChange.create(changes: itemChange)) + if let oldChanges = oldObject["changes"] as? List { + 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 } } @@ -138,9 +174,7 @@ struct Database { newChanges.append(RObjectChange.create(changes: itemChange)) if let oldChanges = oldObject["changes"] as? List { 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 diff --git a/Zotero/Scenes/Detail/PDF/ViewModels/PDFReaderActionHandler.swift b/Zotero/Scenes/Detail/PDF/ViewModels/PDFReaderActionHandler.swift index 0463b8de8..f8e6181b9 100644 --- a/Zotero/Scenes/Detail/PDF/ViewModels/PDFReaderActionHandler.swift +++ b/Zotero/Scenes/Detail/PDF/ViewModels/PDFReaderActionHandler.swift @@ -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 }