2021-11-05 18:18:36 +00:00
|
|
|
import Foundation
|
|
|
|
|
|
|
|
enum WFDefaults {
|
|
|
|
static let isLoggedIn = "isLoggedIn"
|
|
|
|
static let showAllPostsFlag = "showAllPostsFlag"
|
|
|
|
static let selectedCollectionURL = "selectedCollectionURL"
|
|
|
|
static let lastDraftURL = "lastDraftURL"
|
2021-11-19 20:41:54 +00:00
|
|
|
static let colorSchemeIntegerKey = "colorSchemeIntegerKey"
|
|
|
|
static let defaultFontIntegerKey = "defaultFontIntegerKey"
|
|
|
|
static let usernameStringKey = "usernameStringKey"
|
|
|
|
static let serverStringKey = "serverStringKey"
|
2021-11-05 18:18:36 +00:00
|
|
|
#if os(macOS)
|
|
|
|
static let automaticallyChecksForUpdates = "automaticallyChecksForUpdates"
|
|
|
|
static let subscribeToBetaUpdates = "subscribeToBetaUpdates"
|
|
|
|
#endif
|
2022-07-28 11:47:39 +00:00
|
|
|
static let didHaveFatalError = "didHaveFatalError"
|
|
|
|
static let fatalErrorDescription = "fatalErrorDescription"
|
2021-11-05 18:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension UserDefaults {
|
|
|
|
|
|
|
|
private static let appGroupName: String = "group.com.abunchtell.writefreely"
|
|
|
|
private static let didMigrateDefaultsToAppGroup: String = "didMigrateDefaultsToAppGroup"
|
|
|
|
private static let didRemoveStandardDefaults: String = "didRemoveStandardDefaults"
|
|
|
|
|
|
|
|
static var shared: UserDefaults {
|
|
|
|
if let groupDefaults = UserDefaults(suiteName: UserDefaults.appGroupName),
|
|
|
|
groupDefaults.bool(forKey: UserDefaults.didMigrateDefaultsToAppGroup) {
|
|
|
|
return groupDefaults
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
let groupDefaults = try UserDefaults.standard.migrateDefaultsToAppGroup()
|
|
|
|
return groupDefaults
|
|
|
|
} catch {
|
|
|
|
return UserDefaults.standard
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func migrateDefaultsToAppGroup() throws -> UserDefaults {
|
|
|
|
let userDefaults = UserDefaults.standard
|
|
|
|
let groupDefaults = UserDefaults(suiteName: UserDefaults.appGroupName)
|
|
|
|
|
|
|
|
if let groupDefaults = groupDefaults {
|
|
|
|
if groupDefaults.bool(forKey: UserDefaults.didMigrateDefaultsToAppGroup) {
|
|
|
|
return groupDefaults
|
|
|
|
}
|
|
|
|
|
|
|
|
for (key, value) in userDefaults.dictionaryRepresentation() {
|
|
|
|
groupDefaults.set(value, forKey: key)
|
|
|
|
}
|
|
|
|
groupDefaults.set(true, forKey: UserDefaults.didMigrateDefaultsToAppGroup)
|
|
|
|
return groupDefaults
|
|
|
|
} else {
|
2022-07-27 13:56:32 +00:00
|
|
|
throw UserDefaultsError.couldNotMigrateStandardDefaults
|
2021-11-05 18:18:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|