Move LocalData to app group

This commit is contained in:
Shadowfacts 2022-06-15 18:37:53 -04:00
parent b3522a76e1
commit 1dc9903e39
2 changed files with 19 additions and 4 deletions

View File

@ -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

View File

@ -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