mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-05 11:58:32 +00:00
Initial implementation of AccountSettings. (#6275)
* Make the UserPreference macro also generate an accessor for the key name. * Initial implementation of AccountSettings. - Add an AccountUserDefaults storage with a userID prefix on the key. - Migrate the 2 values reset by AppSettings.resetSessionSpecificSettings to AccountSettings. * Rename UserSettings.volatile() to UserSettings.mock() Given it now has a userID parameter with a default value..
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
//
|
||||
// Copyright 2026 Element Creations Ltd.
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
// Please see LICENSE files in the repository root for full details.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
/// A store that scopes all of its values to a single account.
|
||||
///
|
||||
/// Each value keeps its own key in the parent store, prefixed with the user ID so that writes
|
||||
/// remain atomic across the app and its extensions.
|
||||
final nonisolated class AccountUserDefaults: UserDefaultsProtocol, @unchecked Sendable {
|
||||
private let keyPrefix: String
|
||||
private let store: UserDefaultsProtocol
|
||||
|
||||
init(userID: String, store: UserDefaultsProtocol) {
|
||||
keyPrefix = "account[\(userID)]."
|
||||
self.store = store
|
||||
}
|
||||
|
||||
// MARK: Storage
|
||||
|
||||
func data(forKey key: String) -> Data? {
|
||||
store.data(forKey: keyPrefix + key)
|
||||
}
|
||||
|
||||
func object(forKey key: String) -> Any? {
|
||||
store.object(forKey: keyPrefix + key)
|
||||
}
|
||||
|
||||
func removeObject(forKey key: String) {
|
||||
store.removeObject(forKey: keyPrefix + key)
|
||||
}
|
||||
|
||||
func set(_ value: Any?, forKey key: String) {
|
||||
store.set(value, forKey: keyPrefix + key)
|
||||
}
|
||||
|
||||
// MARK: - Management
|
||||
|
||||
func migrateAppSettingsValue(forKey key: String) {
|
||||
guard let value = store.object(forKey: key) else {
|
||||
MXLog.info("Ignoring \(key), not set.")
|
||||
return
|
||||
}
|
||||
|
||||
store.set(value, forKey: keyPrefix + key)
|
||||
store.removeObject(forKey: key)
|
||||
}
|
||||
|
||||
func reset() {
|
||||
for key in dictionaryRepresentation().keys {
|
||||
removeObject(forKey: key)
|
||||
}
|
||||
}
|
||||
|
||||
func dictionaryRepresentation() -> [String: Any] {
|
||||
var representation = [String: Any]()
|
||||
for (key, value) in store.dictionaryRepresentation() where key.hasPrefix(keyPrefix) {
|
||||
representation[String(key.dropFirst(keyPrefix.count))] = value
|
||||
}
|
||||
return representation
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
//
|
||||
// Copyright 2026 Element Creations Ltd.
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
// Please see LICENSE files in the repository root for full details.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
final nonisolated class TrackedUserDefaults: UserDefaults, UserDefaultsProtocol {
|
||||
private let lock = NSLock()
|
||||
|
||||
private nonisolated(unsafe) var _trackedSuites: [String] = []
|
||||
private(set) var trackedSuites: [String] {
|
||||
get { lock.withLock { _trackedSuites } }
|
||||
set { lock.withLock { _trackedSuites = newValue } }
|
||||
}
|
||||
|
||||
override init?(suiteName suitename: String?) {
|
||||
if let suitename {
|
||||
lock.lock()
|
||||
_trackedSuites = [suitename]
|
||||
lock.unlock()
|
||||
}
|
||||
|
||||
super.init(suiteName: suitename)
|
||||
}
|
||||
|
||||
override func addSuite(named suiteName: String) {
|
||||
trackedSuites.append(suiteName)
|
||||
super.addSuite(named: suiteName)
|
||||
}
|
||||
|
||||
override func removeSuite(named suiteName: String) {
|
||||
trackedSuites.removeAll { $0 == suiteName }
|
||||
super.removeSuite(named: suiteName)
|
||||
}
|
||||
|
||||
func resetRegisteredDomains() {
|
||||
lock.withLock {
|
||||
for suite in _trackedSuites {
|
||||
removePersistentDomain(forName: suite)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func reset() {
|
||||
resetRegisteredDomains()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
//
|
||||
// Copyright 2025 Element Creations Ltd.
|
||||
// Copyright 2022-2025 New Vector Ltd.
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
// Please see LICENSE files in the repository root for full details.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
nonisolated protocol UserDefaultsProtocol: AnyObject {
|
||||
func data(forKey key: String) -> Data?
|
||||
func object(forKey key: String) -> Any?
|
||||
func removeObject(forKey key: String)
|
||||
func set(_ value: Any?, forKey key: String)
|
||||
|
||||
func reset()
|
||||
func dictionaryRepresentation() -> [String: Any]
|
||||
}
|
||||
|
||||
// MARK: - Codable handling
|
||||
|
||||
nonisolated extension UserDefaultsProtocol {
|
||||
/// Reads and writes a `Codable` value stored under `key`, used by the `@UserPreference` macro.
|
||||
///
|
||||
/// When `Value` conforms to `PlistRepresentable` the Codable encode/decode phase is skipped and
|
||||
/// the value is stored natively in the plist.
|
||||
subscript<Value: Codable>(key: String) -> Value? {
|
||||
get {
|
||||
if Value.self is PlistRepresentable.Type {
|
||||
object(forKey: key) as? Value
|
||||
} else {
|
||||
data(forKey: key).flatMap { try? JSONDecoder().decode(Value.self, from: $0) }
|
||||
}
|
||||
}
|
||||
set {
|
||||
// Detects correctly double optionals like this: String?? = .some(nil)
|
||||
if newValue.isNil {
|
||||
removeObject(forKey: key)
|
||||
} else if Value.self is PlistRepresentable.Type {
|
||||
set(newValue, forKey: key)
|
||||
} else {
|
||||
set(try? JSONEncoder().encode(newValue), forKey: key)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private nonisolated protocol Nullable {
|
||||
var isNil: Bool { get }
|
||||
}
|
||||
|
||||
nonisolated extension Optional: Nullable {
|
||||
var isNil: Bool {
|
||||
switch self {
|
||||
case .none:
|
||||
return true
|
||||
case .some(let nullable as Nullable):
|
||||
return nullable.isNil
|
||||
case .some:
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - PlistRepresentable
|
||||
|
||||
/// A protocol to mark types as being plist compliant.
|
||||
/// The `UserDefaultsProtocol` storage subscript uses this to avoid encoding/decoding plist compliant values with Codable.
|
||||
nonisolated protocol PlistRepresentable { }
|
||||
|
||||
extension Bool: PlistRepresentable { }
|
||||
extension String: PlistRepresentable { }
|
||||
extension Int: PlistRepresentable { }
|
||||
extension Float: PlistRepresentable { }
|
||||
extension Double: PlistRepresentable { }
|
||||
extension Date: PlistRepresentable { }
|
||||
extension Data: PlistRepresentable { }
|
||||
|
||||
extension Array: PlistRepresentable where Element: PlistRepresentable { }
|
||||
extension Dictionary: PlistRepresentable where Key == String, Value: PlistRepresentable { }
|
||||
extension Optional: PlistRepresentable where Wrapped: PlistRepresentable { }
|
||||
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// Copyright 2026 Element Creations Ltd.
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
// Please see LICENSE files in the repository root for full details.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
final nonisolated class VolatileUserDefaults: UserDefaultsProtocol, Sendable {
|
||||
private let lock = NSLock()
|
||||
|
||||
private nonisolated(unsafe) var _storage: [String: Any]
|
||||
private(set) var storage: [String: Any] {
|
||||
get { lock.withLock { _storage } }
|
||||
set { lock.withLock { _storage = newValue } }
|
||||
}
|
||||
|
||||
init(initialValues: [String: Any] = [:]) {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
|
||||
_storage = initialValues
|
||||
}
|
||||
|
||||
func data(forKey key: String) -> Data? {
|
||||
storage[key] as? Data
|
||||
}
|
||||
|
||||
func object(forKey key: String) -> Any? {
|
||||
storage[key]
|
||||
}
|
||||
|
||||
func removeObject(forKey key: String) {
|
||||
storage.removeValue(forKey: key)
|
||||
}
|
||||
|
||||
func set(_ value: Any?, forKey key: String) {
|
||||
storage[key] = value
|
||||
}
|
||||
|
||||
func reset() {
|
||||
storage = [:]
|
||||
}
|
||||
|
||||
func dictionaryRepresentation() -> [String: Any] {
|
||||
storage
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user