mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-03 02:42:54 +00:00
Migrate iOS UserDefaults from suite to standard. Fixes #5466.
This commit is contained in:
1 parent
2357a5ad61
commit
b45fcbc7d0
4 files changed
+91
-17
No files matched your search
@@ -52,6 +52,7 @@
|
||||
843E2F1D2CF2B8D300ED170F /* RSWeb in Frameworks */ = {isa = PBXBuildFile; productRef = 843E2F1C2CF2B8D300ED170F /* RSWeb */; };
|
||||
84719F4C2EE01A1100EEF332 /* Tests/NetNewsWire-iOSTests/ActivityItemSourceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84719F4B2EE01A1100EEF332 /* Tests/NetNewsWire-iOSTests/ActivityItemSourceTests.swift */; };
|
||||
84719F4E2EE01A1100EEF332 /* Tests/NetNewsWire-iOSTests/MultilineUILabelSizerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84719F4D2EE01A1100EEF332 /* Tests/NetNewsWire-iOSTests/MultilineUILabelSizerTests.swift */; };
|
||||
84719F502EE01A1100EEF332 /* Tests/NetNewsWire-iOSTests/AppDefaultsMigrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84719F4F2EE01A1100EEF332 /* Tests/NetNewsWire-iOSTests/AppDefaultsMigrationTests.swift */; };
|
||||
848E84CD2DB749440023F3BA /* RSCore in Frameworks */ = {isa = PBXBuildFile; productRef = 848E84CC2DB749440023F3BA /* RSCore */; };
|
||||
848E84CE2DB749440023F3BA /* RSCore in Embed Frameworks */ = {isa = PBXBuildFile; productRef = 848E84CC2DB749440023F3BA /* RSCore */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
|
||||
848E84D02DB749440023F3BA /* RSCoreResources in Frameworks */ = {isa = PBXBuildFile; productRef = 848E84CF2DB749440023F3BA /* RSCoreResources */; };
|
||||
@@ -287,6 +288,7 @@
|
||||
845B14A51FC2299E0013CF92 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
|
||||
84719F4B2EE01A1100EEF332 /* Tests/NetNewsWire-iOSTests/ActivityItemSourceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Tests/NetNewsWire-iOSTests/ActivityItemSourceTests.swift"; sourceTree = SOURCE_ROOT; };
|
||||
84719F4D2EE01A1100EEF332 /* Tests/NetNewsWire-iOSTests/MultilineUILabelSizerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Tests/NetNewsWire-iOSTests/MultilineUILabelSizerTests.swift"; sourceTree = SOURCE_ROOT; };
|
||||
84719F4F2EE01A1100EEF332 /* Tests/NetNewsWire-iOSTests/AppDefaultsMigrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Tests/NetNewsWire-iOSTests/AppDefaultsMigrationTests.swift"; sourceTree = SOURCE_ROOT; };
|
||||
848CEF1E2DB74B56002B35B5 /* NetNewsWire-iOS.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = "NetNewsWire-iOS.xctestplan"; sourceTree = "<group>"; };
|
||||
848E84C92DB743FF0023F3BA /* NetNewsWire-CI.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = "NetNewsWire-CI.xctestplan"; sourceTree = "<group>"; };
|
||||
848E84CA2DB743FF0023F3BA /* NetNewsWire.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = NetNewsWire.xctestplan; sourceTree = "<group>"; };
|
||||
@@ -349,6 +351,7 @@
|
||||
membershipExceptions = (
|
||||
"NetNewsWire-iOSTests/ActivityItemSourceTests.swift",
|
||||
"NetNewsWire-iOSTests/MultilineUILabelSizerTests.swift",
|
||||
"NetNewsWire-iOSTests/AppDefaultsMigrationTests.swift",
|
||||
);
|
||||
target = 849C64701ED37A5D003D8FC0 /* NetNewsWireTests */;
|
||||
};
|
||||
@@ -571,6 +574,7 @@
|
||||
children = (
|
||||
84719F4B2EE01A1100EEF332 /* Tests/NetNewsWire-iOSTests/ActivityItemSourceTests.swift */,
|
||||
84719F4D2EE01A1100EEF332 /* Tests/NetNewsWire-iOSTests/MultilineUILabelSizerTests.swift */,
|
||||
84719F4F2EE01A1100EEF332 /* Tests/NetNewsWire-iOSTests/AppDefaultsMigrationTests.swift */,
|
||||
);
|
||||
name = "Recovered References";
|
||||
sourceTree = "<group>";
|
||||
@@ -1167,6 +1171,7 @@
|
||||
files = (
|
||||
84719F4C2EE01A1100EEF332 /* Tests/NetNewsWire-iOSTests/ActivityItemSourceTests.swift in Sources */,
|
||||
84719F4E2EE01A1100EEF332 /* Tests/NetNewsWire-iOSTests/MultilineUILabelSizerTests.swift in Sources */,
|
||||
84719F502EE01A1100EEF332 /* Tests/NetNewsWire-iOSTests/AppDefaultsMigrationTests.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
//
|
||||
// AppDefaultsMigrationTests.swift
|
||||
// NetNewsWire-iOSTests
|
||||
//
|
||||
// Created by Brent Simmons on 10/22/26.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Testing
|
||||
@testable import NetNewsWire
|
||||
|
||||
@Suite struct AppDefaultsMigrationTests {
|
||||
|
||||
// Test-only keys, since the host app has already registered defaults for the real keys.
|
||||
private static let paletteKey = "testMigrationUserInterfaceColorPalette"
|
||||
private static let lastRefreshKey = "testMigrationLastRefresh"
|
||||
|
||||
@Test func migrateCopiesValuesWithoutOverwriting() throws {
|
||||
let sourceSuiteName = UUID().uuidString
|
||||
let destinationSuiteName = UUID().uuidString
|
||||
let source = try #require(UserDefaults(suiteName: sourceSuiteName))
|
||||
let destination = try #require(UserDefaults(suiteName: destinationSuiteName))
|
||||
defer {
|
||||
source.removePersistentDomain(forName: sourceSuiteName)
|
||||
destination.removePersistentDomain(forName: destinationSuiteName)
|
||||
}
|
||||
|
||||
let oldRefreshDate = Date(timeIntervalSince1970: 1_700_000_000)
|
||||
let newRefreshDate = Date(timeIntervalSince1970: 1_800_000_000)
|
||||
source.set(2, forKey: Self.paletteKey)
|
||||
source.set(oldRefreshDate, forKey: Self.lastRefreshKey)
|
||||
destination.set(newRefreshDate, forKey: Self.lastRefreshKey)
|
||||
|
||||
AppDefaults.migrate(from: source, to: destination, keys: [Self.paletteKey, Self.lastRefreshKey])
|
||||
|
||||
#expect(destination.integer(forKey: Self.paletteKey) == 2)
|
||||
#expect(destination.object(forKey: Self.lastRefreshKey) as? Date == newRefreshDate)
|
||||
}
|
||||
}
|
||||
+45
-16
@@ -43,12 +43,6 @@ final class AppDefaults: Sendable {
|
||||
|
||||
private init() {}
|
||||
|
||||
nonisolated(unsafe) static let store: UserDefaults = {
|
||||
let appIdentifierPrefix = Bundle.main.object(forInfoDictionaryKey: "AppIdentifierPrefix") as! String
|
||||
let suiteName = "\(appIdentifierPrefix)group.\(Bundle.main.bundleIdentifier!)"
|
||||
return UserDefaults.init(suiteName: suiteName)!
|
||||
}()
|
||||
|
||||
struct Key {
|
||||
static let userInterfaceColorPalette = "userInterfaceColorPalette"
|
||||
static let lastImageCacheFlushDate = "lastImageCacheFlushDate"
|
||||
@@ -81,6 +75,10 @@ final class AppDefaults: Sendable {
|
||||
static let splitViewPreferredDisplayMode = "splitViewPreferredDisplayMode"
|
||||
static let timelineWidth = "timelineWidth"
|
||||
static let sidebarWidth = "sidebarWidth"
|
||||
static let didMigrateAppGroupDefaults = "didMigrateAppGroupDefaults"
|
||||
|
||||
// Keys that used to be stored in the app-group suite.
|
||||
static let appGroupKeys = [userInterfaceColorPalette, lastImageCacheFlushDate, firstRunDate, timelineGroupByFeed, refreshClearsReadArticles, timelineNumberOfLines, timelineIconDimension, timelineSortDirection, articleFullscreenAvailable, articleFullscreenEnabled, confirmMarkAllAsRead, articleContentJavascriptEnabled, splitViewPreferredDisplayMode, lastRefresh]
|
||||
}
|
||||
|
||||
let isDeveloperBuild: Bool = {
|
||||
@@ -91,7 +89,7 @@ final class AppDefaults: Sendable {
|
||||
}()
|
||||
|
||||
let isFirstRun: Bool = {
|
||||
if AppDefaults.store.object(forKey: Key.firstRunDate) is Date {
|
||||
if UserDefaults.standard.object(forKey: Key.firstRunDate) is Date {
|
||||
return false
|
||||
}
|
||||
firstRunDate = Date()
|
||||
@@ -281,11 +279,11 @@ final class AppDefaults: Sendable {
|
||||
|
||||
var timelineIconSize: IconSize {
|
||||
get {
|
||||
let rawValue = AppDefaults.store.integer(forKey: Key.timelineIconDimension)
|
||||
let rawValue = UserDefaults.standard.integer(forKey: Key.timelineIconDimension)
|
||||
return IconSize(rawValue: rawValue) ?? IconSize.medium
|
||||
}
|
||||
set {
|
||||
AppDefaults.store.set(newValue.rawValue, forKey: Key.timelineIconDimension)
|
||||
UserDefaults.standard.set(newValue.rawValue, forKey: Key.timelineIconDimension)
|
||||
NotificationCenter.default.post(name: .timelineIconSizeDidChange, object: nil)
|
||||
}
|
||||
}
|
||||
@@ -420,6 +418,9 @@ final class AppDefaults: Sendable {
|
||||
}
|
||||
|
||||
@MainActor static func registerDefaults() {
|
||||
// Must run before register(defaults:) so registered values aren’t copied as explicit ones.
|
||||
migrateAppGroupDefaultsIfNeeded()
|
||||
|
||||
let defaults: [String: Any] = [Key.userInterfaceColorPalette: UserInterfaceColorPalette.automatic.rawValue,
|
||||
Key.timelineGroupByFeed: false,
|
||||
Key.refreshClearsReadArticles: false,
|
||||
@@ -432,7 +433,16 @@ final class AppDefaults: Sendable {
|
||||
Key.articleContentJavascriptEnabled: true,
|
||||
Key.currentThemeName: Self.defaultThemeName,
|
||||
Key.splitViewPreferredDisplayMode: UISplitViewController.DisplayMode.oneBesideSecondary.rawValue]
|
||||
AppDefaults.store.register(defaults: defaults)
|
||||
UserDefaults.standard.register(defaults: defaults)
|
||||
}
|
||||
|
||||
/// Copies each key’s value from source to destination, unless destination already has a value.
|
||||
static func migrate(from source: UserDefaults, to destination: UserDefaults, keys: [String]) {
|
||||
for key in keys {
|
||||
if let value = source.object(forKey: key), destination.object(forKey: key) == nil {
|
||||
destination.set(value, forKey: key)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -456,27 +466,46 @@ private extension AppDefaults {
|
||||
}
|
||||
|
||||
static func bool(for key: String) -> Bool {
|
||||
return AppDefaults.store.bool(forKey: key)
|
||||
return UserDefaults.standard.bool(forKey: key)
|
||||
}
|
||||
|
||||
static func setBool(for key: String, _ flag: Bool) {
|
||||
AppDefaults.store.set(flag, forKey: key)
|
||||
UserDefaults.standard.set(flag, forKey: key)
|
||||
}
|
||||
|
||||
static func int(for key: String) -> Int {
|
||||
return AppDefaults.store.integer(forKey: key)
|
||||
return UserDefaults.standard.integer(forKey: key)
|
||||
}
|
||||
|
||||
static func setInt(for key: String, _ x: Int) {
|
||||
AppDefaults.store.set(x, forKey: key)
|
||||
UserDefaults.standard.set(x, forKey: key)
|
||||
}
|
||||
|
||||
static func date(for key: String) -> Date? {
|
||||
return AppDefaults.store.object(forKey: key) as? Date
|
||||
return UserDefaults.standard.object(forKey: key) as? Date
|
||||
}
|
||||
|
||||
static func setDate(for key: String, _ date: Date?) {
|
||||
AppDefaults.store.set(date, forKey: key)
|
||||
UserDefaults.standard.set(date, forKey: key)
|
||||
}
|
||||
|
||||
/// Moves settings from the app-group suite to UserDefaults.standard, then deletes the suite.
|
||||
/// <https://github.com/Ranchero-Software/NetNewsWire/issues/5466>
|
||||
static func migrateAppGroupDefaultsIfNeeded() {
|
||||
guard !UserDefaults.standard.bool(forKey: Key.didMigrateAppGroupDefaults) else {
|
||||
return
|
||||
}
|
||||
UserDefaults.standard.set(true, forKey: Key.didMigrateAppGroupDefaults)
|
||||
|
||||
guard let appIdentifierPrefix = Bundle.main.object(forInfoDictionaryKey: "AppIdentifierPrefix") as? String, let bundleIdentifier = Bundle.main.bundleIdentifier else {
|
||||
return
|
||||
}
|
||||
let suiteName = "\(appIdentifierPrefix)group.\(bundleIdentifier)"
|
||||
guard let appGroupDefaults = UserDefaults(suiteName: suiteName) else {
|
||||
return
|
||||
}
|
||||
migrate(from: appGroupDefaults, to: UserDefaults.standard, keys: Key.appGroupKeys)
|
||||
appGroupDefaults.removePersistentDomain(forName: suiteName)
|
||||
}
|
||||
|
||||
static func sortDirection(for key: String) -> ComparisonResult {
|
||||
|
||||
@@ -59,13 +59,14 @@ import Images
|
||||
}
|
||||
|
||||
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
|
||||
AppDefaults.registerDefaults()
|
||||
|
||||
FaviconGenerator.templateImage = Assets.Images.faviconTemplate
|
||||
|
||||
WebViewConfiguration.resolveBrowserUserAgent()
|
||||
Task {
|
||||
await WebViewConfiguration.compileContentBlockingRules()
|
||||
}
|
||||
AppDefaults.registerDefaults()
|
||||
|
||||
let isFirstRun = AppDefaults.shared.isFirstRun
|
||||
if isFirstRun {
|
||||
|
||||
Reference in new issue
Block a user