Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix annotation color in dark appearance #1004

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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