From 1dc9903e390a23a47f0badccf83414bd08a1ac86 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 15 Jun 2022 18:37:53 -0400 Subject: [PATCH] Move LocalData to app group --- Reader/AppDelegate.swift | 2 ++ Reader/LocalData.swift | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Reader/AppDelegate.swift b/Reader/AppDelegate.swift index 218d4cb..b120f82 100644 --- a/Reader/AppDelegate.swift +++ b/Reader/AppDelegate.swift @@ -20,6 +20,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate { #endif func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { + LocalData.migrateIfNecessary() + swizzleWKWebView() Preferences.shared.objectWillChange diff --git a/Reader/LocalData.swift b/Reader/LocalData.swift index 3069580..545e52b 100644 --- a/Reader/LocalData.swift +++ b/Reader/LocalData.swift @@ -16,9 +16,11 @@ struct LocalData { private static let encoder = JSONEncoder() private static let decoder = JSONDecoder() + private static var defaults = UserDefaults(suiteName: "group.net.shadowfacts.Reader")! + static var accounts: [Account] { get { - guard let data = UserDefaults.standard.data(forKey: "accounts"), + guard let data = defaults.data(forKey: "accounts"), let accounts = try? decoder.decode([Account].self, from: data) else { return [] } @@ -26,16 +28,16 @@ struct LocalData { } set { let data = try! encoder.encode(newValue) - UserDefaults.standard.set(data, forKey: "accounts") + defaults.set(data, forKey: "accounts") } } static var mostRecentAccountID: Data? { get { - return UserDefaults.standard.data(forKey: "mostRecentAccountID") + return defaults.data(forKey: "mostRecentAccountID") } set { - UserDefaults.standard.set(newValue, forKey: "mostRecentAccountID") + defaults.set(newValue, forKey: "mostRecentAccountID") } } @@ -50,6 +52,17 @@ struct LocalData { return accounts.first(where: { $0.id == id }) } + static func migrateIfNecessary() { + if let accounts = UserDefaults.standard.object(forKey: "accounts") { + UserDefaults.standard.removeObject(forKey: "accounts") + defaults.set(accounts, forKey: "accounts") + } + if let mostRecentAccountID = UserDefaults.standard.object(forKey: "mostRecentAccountID") { + UserDefaults.standard.removeObject(forKey: "mostRecentAccountID") + defaults.set(mostRecentAccountID, forKey: "mostRecentAccountID") + } + } + struct Account: Codable { let id: Data let instanceURL: URL