Add MigratablePreferenceKey protocol

This commit is contained in:
Shadowfacts 2024-04-15 10:37:02 -04:00
parent cb5488dcaa
commit 70227a7fa1
8 changed files with 31 additions and 21 deletions

View File

@ -8,10 +8,10 @@
import Foundation import Foundation
import Pachyderm import Pachyderm
struct StatusContentTypeKey: PreferenceKey { struct StatusContentTypeKey: MigratablePreferenceKey {
static var defaultValue: StatusContentType { .plain } static var defaultValue: StatusContentType { .plain }
} }
struct FeatureFlagsKey: PreferenceKey { struct FeatureFlagsKey: MigratablePreferenceKey {
static var defaultValue: Set<FeatureFlag> { [] } static var defaultValue: Set<FeatureFlag> { [] }
} }

View File

@ -8,31 +8,31 @@
import Foundation import Foundation
import UIKit import UIKit
struct ThemeKey: PreferenceKey { struct ThemeKey: MigratablePreferenceKey {
static var defaultValue: Theme { .unspecified } static var defaultValue: Theme { .unspecified }
} }
struct AccentColorKey: PreferenceKey { struct AccentColorKey: MigratablePreferenceKey {
static var defaultValue: AccentColor { .default } static var defaultValue: AccentColor { .default }
} }
struct AvatarStyleKey: PreferenceKey { struct AvatarStyleKey: MigratablePreferenceKey {
static var defaultValue: AvatarStyle { .roundRect } static var defaultValue: AvatarStyle { .roundRect }
} }
struct LeadingSwipeActionsKey: PreferenceKey { struct LeadingSwipeActionsKey: MigratablePreferenceKey {
static var defaultValue: [StatusSwipeAction] { [.favorite, .reblog] } static var defaultValue: [StatusSwipeAction] { [.favorite, .reblog] }
} }
struct TrailingSwipeActionsKey: PreferenceKey { struct TrailingSwipeActionsKey: MigratablePreferenceKey {
static var defaultValue: [StatusSwipeAction] { [.reply, .share] } static var defaultValue: [StatusSwipeAction] { [.reply, .share] }
} }
struct WidescreenNavigationModeKey: PreferenceKey { struct WidescreenNavigationModeKey: MigratablePreferenceKey {
static var defaultValue: WidescreenNavigationMode { .splitScreen } static var defaultValue: WidescreenNavigationMode { .splitScreen }
} }
struct AttachmentBlurModeKey: PreferenceKey { struct AttachmentBlurModeKey: MigratablePreferenceKey {
static var defaultValue: AttachmentBlurMode { .useStatusSetting } static var defaultValue: AttachmentBlurMode { .useStatusSetting }
static func didSet(in store: PreferenceStore, newValue: AttachmentBlurMode) { static func didSet(in store: PreferenceStore, newValue: AttachmentBlurMode) {

View File

@ -7,11 +7,11 @@
import Foundation import Foundation
struct OppositeCollapseKeywordsKey: PreferenceKey { struct OppositeCollapseKeywordsKey: MigratablePreferenceKey {
static var defaultValue: [String] { [] } static var defaultValue: [String] { [] }
} }
struct ConfirmReblogKey: PreferenceKey { struct ConfirmReblogKey: MigratablePreferenceKey {
static var defaultValue: Bool { static var defaultValue: Bool {
#if os(visionOS) #if os(visionOS)
true true
@ -21,11 +21,11 @@ struct ConfirmReblogKey: PreferenceKey {
} }
} }
struct TimelineSyncModeKey: PreferenceKey { struct TimelineSyncModeKey: MigratablePreferenceKey {
static var defaultValue: TimelineSyncMode { .icloud } static var defaultValue: TimelineSyncMode { .icloud }
} }
struct InAppSafariKey: PreferenceKey { struct InAppSafariKey: MigratablePreferenceKey {
static var defaultValue: Bool { static var defaultValue: Bool {
#if targetEnvironment(macCatalyst) || os(visionOS) #if targetEnvironment(macCatalyst) || os(visionOS)
false false

View File

@ -7,10 +7,10 @@
import Foundation import Foundation
struct TrueKey: PreferenceKey { struct TrueKey: MigratablePreferenceKey {
static var defaultValue: Bool { true } static var defaultValue: Bool { true }
} }
struct FalseKey: PreferenceKey { struct FalseKey: MigratablePreferenceKey {
static var defaultValue: Bool { false } static var defaultValue: Bool { false }
} }

View File

@ -7,14 +7,14 @@
import Foundation import Foundation
struct PostVisibilityKey: PreferenceKey { struct PostVisibilityKey: MigratablePreferenceKey {
static var defaultValue: PostVisibility { .serverDefault } static var defaultValue: PostVisibility { .serverDefault }
} }
struct ReplyVisibilityKey: PreferenceKey { struct ReplyVisibilityKey: MigratablePreferenceKey {
static var defaultValue: ReplyVisibility { .sameAsPost } static var defaultValue: ReplyVisibility { .sameAsPost }
} }
struct ContentWarningCopyModeKey: PreferenceKey { struct ContentWarningCopyModeKey: MigratablePreferenceKey {
static var defaultValue: ContentWarningCopyMode { .asIs } static var defaultValue: ContentWarningCopyMode { .asIs }
} }

View File

@ -7,6 +7,6 @@
import Foundation import Foundation
struct NotificationsModeKey: PreferenceKey { struct NotificationsModeKey: MigratablePreferenceKey {
static var defaultValue: NotificationsMode { .allNotifications } static var defaultValue: NotificationsMode { .allNotifications }
} }

View File

@ -80,13 +80,13 @@ private protocol MigrationProtocol {
func migrate(from legacy: LegacyPreferences, to store: PreferenceStore) func migrate(from legacy: LegacyPreferences, to store: PreferenceStore)
} }
private struct Migration<Key: PreferenceKey>: MigrationProtocol where Key.Value: Equatable { private struct Migration<Key: MigratablePreferenceKey>: MigrationProtocol where Key.Value: Equatable {
let from: KeyPath<LegacyPreferences, Key.Value> let from: KeyPath<LegacyPreferences, Key.Value>
let to: KeyPath<PreferenceStore, PreferencePublisher<Key>> let to: KeyPath<PreferenceStore, PreferencePublisher<Key>>
func migrate(from legacy: LegacyPreferences, to store: PreferenceStore) { func migrate(from legacy: LegacyPreferences, to store: PreferenceStore) {
let value = legacy[keyPath: from] 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) Preference.set(enclosingInstance: store, storage: to.appending(path: \.preference), newValue: value)
} }
} }

View File

@ -18,3 +18,13 @@ public protocol PreferenceKey {
extension PreferenceKey { extension PreferenceKey {
static func didSet(in store: PreferenceStore, newValue: Value) {} 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
}
}