From 70227a7fa1a33e62ba9388c25dbf957cc986cf36 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 15 Apr 2024 10:37:02 -0400 Subject: [PATCH] Add MigratablePreferenceKey protocol --- .../TuskerPreferences/Keys/AdvancedKeys.swift | 4 ++-- .../TuskerPreferences/Keys/AppearanceKeys.swift | 14 +++++++------- .../TuskerPreferences/Keys/BehaviorKeys.swift | 8 ++++---- .../TuskerPreferences/Keys/CommonKeys.swift | 4 ++-- .../TuskerPreferences/Keys/ComposingKeys.swift | 6 +++--- .../Keys/DigitalWellnessKeys.swift | 2 +- .../Legacy/PreferenceStore+Migrate.swift | 4 ++-- .../Sources/TuskerPreferences/PreferenceKey.swift | 10 ++++++++++ 8 files changed, 31 insertions(+), 21 deletions(-) diff --git a/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/AdvancedKeys.swift b/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/AdvancedKeys.swift index a8608d3059..25a80815f2 100644 --- a/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/AdvancedKeys.swift +++ b/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/AdvancedKeys.swift @@ -8,10 +8,10 @@ import Foundation import Pachyderm -struct StatusContentTypeKey: PreferenceKey { +struct StatusContentTypeKey: MigratablePreferenceKey { static var defaultValue: StatusContentType { .plain } } -struct FeatureFlagsKey: PreferenceKey { +struct FeatureFlagsKey: MigratablePreferenceKey { static var defaultValue: Set { [] } } diff --git a/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/AppearanceKeys.swift b/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/AppearanceKeys.swift index a535fcd2cc..c8ab1921ab 100644 --- a/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/AppearanceKeys.swift +++ b/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/AppearanceKeys.swift @@ -8,31 +8,31 @@ import Foundation import UIKit -struct ThemeKey: PreferenceKey { +struct ThemeKey: MigratablePreferenceKey { static var defaultValue: Theme { .unspecified } } -struct AccentColorKey: PreferenceKey { +struct AccentColorKey: MigratablePreferenceKey { static var defaultValue: AccentColor { .default } } -struct AvatarStyleKey: PreferenceKey { +struct AvatarStyleKey: MigratablePreferenceKey { static var defaultValue: AvatarStyle { .roundRect } } -struct LeadingSwipeActionsKey: PreferenceKey { +struct LeadingSwipeActionsKey: MigratablePreferenceKey { static var defaultValue: [StatusSwipeAction] { [.favorite, .reblog] } } -struct TrailingSwipeActionsKey: PreferenceKey { +struct TrailingSwipeActionsKey: MigratablePreferenceKey { static var defaultValue: [StatusSwipeAction] { [.reply, .share] } } -struct WidescreenNavigationModeKey: PreferenceKey { +struct WidescreenNavigationModeKey: MigratablePreferenceKey { static var defaultValue: WidescreenNavigationMode { .splitScreen } } -struct AttachmentBlurModeKey: PreferenceKey { +struct AttachmentBlurModeKey: MigratablePreferenceKey { static var defaultValue: AttachmentBlurMode { .useStatusSetting } static func didSet(in store: PreferenceStore, newValue: AttachmentBlurMode) { diff --git a/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/BehaviorKeys.swift b/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/BehaviorKeys.swift index 17ac09fa89..24717e17fd 100644 --- a/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/BehaviorKeys.swift +++ b/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/BehaviorKeys.swift @@ -7,11 +7,11 @@ import Foundation -struct OppositeCollapseKeywordsKey: PreferenceKey { +struct OppositeCollapseKeywordsKey: MigratablePreferenceKey { static var defaultValue: [String] { [] } } -struct ConfirmReblogKey: PreferenceKey { +struct ConfirmReblogKey: MigratablePreferenceKey { static var defaultValue: Bool { #if os(visionOS) true @@ -21,11 +21,11 @@ struct ConfirmReblogKey: PreferenceKey { } } -struct TimelineSyncModeKey: PreferenceKey { +struct TimelineSyncModeKey: MigratablePreferenceKey { static var defaultValue: TimelineSyncMode { .icloud } } -struct InAppSafariKey: PreferenceKey { +struct InAppSafariKey: MigratablePreferenceKey { static var defaultValue: Bool { #if targetEnvironment(macCatalyst) || os(visionOS) false diff --git a/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/CommonKeys.swift b/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/CommonKeys.swift index 1bf8a1adff..778d191fae 100644 --- a/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/CommonKeys.swift +++ b/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/CommonKeys.swift @@ -7,10 +7,10 @@ import Foundation -struct TrueKey: PreferenceKey { +struct TrueKey: MigratablePreferenceKey { static var defaultValue: Bool { true } } -struct FalseKey: PreferenceKey { +struct FalseKey: MigratablePreferenceKey { static var defaultValue: Bool { false } } diff --git a/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/ComposingKeys.swift b/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/ComposingKeys.swift index 736c9ef85d..39869ec740 100644 --- a/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/ComposingKeys.swift +++ b/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/ComposingKeys.swift @@ -7,14 +7,14 @@ import Foundation -struct PostVisibilityKey: PreferenceKey { +struct PostVisibilityKey: MigratablePreferenceKey { static var defaultValue: PostVisibility { .serverDefault } } -struct ReplyVisibilityKey: PreferenceKey { +struct ReplyVisibilityKey: MigratablePreferenceKey { static var defaultValue: ReplyVisibility { .sameAsPost } } -struct ContentWarningCopyModeKey: PreferenceKey { +struct ContentWarningCopyModeKey: MigratablePreferenceKey { static var defaultValue: ContentWarningCopyMode { .asIs } } diff --git a/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/DigitalWellnessKeys.swift b/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/DigitalWellnessKeys.swift index c1ae4086a4..e4cb891d16 100644 --- a/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/DigitalWellnessKeys.swift +++ b/Packages/TuskerPreferences/Sources/TuskerPreferences/Keys/DigitalWellnessKeys.swift @@ -7,6 +7,6 @@ import Foundation -struct NotificationsModeKey: PreferenceKey { +struct NotificationsModeKey: MigratablePreferenceKey { static var defaultValue: NotificationsMode { .allNotifications } } diff --git a/Packages/TuskerPreferences/Sources/TuskerPreferences/Legacy/PreferenceStore+Migrate.swift b/Packages/TuskerPreferences/Sources/TuskerPreferences/Legacy/PreferenceStore+Migrate.swift index a5ea15bd18..3db2b744ca 100644 --- a/Packages/TuskerPreferences/Sources/TuskerPreferences/Legacy/PreferenceStore+Migrate.swift +++ b/Packages/TuskerPreferences/Sources/TuskerPreferences/Legacy/PreferenceStore+Migrate.swift @@ -80,13 +80,13 @@ private protocol MigrationProtocol { func migrate(from legacy: LegacyPreferences, to store: PreferenceStore) } -private struct Migration: MigrationProtocol where Key.Value: Equatable { +private struct Migration: MigrationProtocol where Key.Value: Equatable { let from: KeyPath let to: KeyPath> func migrate(from legacy: LegacyPreferences, to store: PreferenceStore) { let value = legacy[keyPath: from] - if value != Key.defaultValue { + if Key.shouldMigrate(oldValue: value) { Preference.set(enclosingInstance: store, storage: to.appending(path: \.preference), newValue: value) } } diff --git a/Packages/TuskerPreferences/Sources/TuskerPreferences/PreferenceKey.swift b/Packages/TuskerPreferences/Sources/TuskerPreferences/PreferenceKey.swift index 4a153c5a5e..f0a96f080f 100644 --- a/Packages/TuskerPreferences/Sources/TuskerPreferences/PreferenceKey.swift +++ b/Packages/TuskerPreferences/Sources/TuskerPreferences/PreferenceKey.swift @@ -18,3 +18,13 @@ public protocol PreferenceKey { extension PreferenceKey { static func didSet(in store: PreferenceStore, newValue: Value) {} } + +protocol MigratablePreferenceKey: PreferenceKey where Value: Equatable { + static func shouldMigrate(oldValue: Value) -> Bool +} + +extension MigratablePreferenceKey { + static func shouldMigrate(oldValue: Value) -> Bool { + oldValue != defaultValue + } +}