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:
Doug
2026-10-06 17:52:24 +01:00
committed by GitHub
parent acd18f3aa9
commit 93825c01ad
96 changed files with 623 additions and 248 deletions
@@ -467,14 +467,6 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
MXLog.info("The app was upgraded from \(oldVersion) to \(newVersion)")
if oldVersion < Version(1, 6, 0) {
MXLog.info("Migrating to v1.6.0, marking identity confirmation onboarding as ran.")
if !userSessionStore.userIDs.isEmpty {
appSettings.hasRunIdentityConfirmationOnboarding = true
appSettings.hasRunNotificationPermissionsOnboarding = true
}
}
if oldVersion < Version(1, 6, 7) {
Tracing.deleteLogFiles(in: Tracing.legacyLogsDirectory)
MXLog.info("Migrating to v1.6.7, log files have been wiped")
@@ -498,6 +490,12 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
await userSession.clientProxy.optimizeStores()
MXLog.info("Finished optimizing client stores.")
if oldVersion < Version(1, 6, 0) {
MXLog.info("Migrating to v1.6.0, marking identity confirmation onboarding as ran.")
userSession.userSettings.hasRunIdentityConfirmationOnboarding = true
userSession.userSettings.hasRunNotificationPermissionsOnboarding = true
}
if oldVersion < Version(25, 6, 0) {
MXLog.info("Migrating to version 25.06.0, migrating timeline media settings to account data.")
performSettingsToAccountDataMigration(userSession: userSession)
@@ -518,6 +516,12 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
appSettings.hasSignedInBefore = true
}
if oldVersion < Version(26, 10, 1) {
MXLog.info("Migrating to version 26.10.1, moving initial 'session specific' settings.")
userSession.userSettings.migrateAppSettingsValueToAccountSettings(\.hasRunIdentityConfirmationOnboardingKey)
userSession.userSettings.migrateAppSettingsValueToAccountSettings(\.searchBreadcrumbsKey)
}
userSessionMigrationsOldVersion = nil
}
@@ -863,7 +867,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
userSessionStore.logout(userSession: userSession)
tearDownUserSession()
appSettings.resetSessionSpecificSettings()
userSession.userSettings.resetSessionSpecificSettings()
appHooks.remoteSettingsHook.reset(appSettings)
// Reset analytics
@@ -0,0 +1,42 @@
//
// 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 Combine
import Foundation
import Macros
final nonisolated class AccountSettings: @unchecked Sendable {
/// UserDefaults to be used on reads and writes.
private let store: AccountUserDefaults
private let userID: String
// MARK: Session
@UserPreference(defaultValue: false)
var hasRunIdentityConfirmationOnboarding: Bool
// MARK: Search
/// The queries the user searched for and the rooms they opened from the results, most recent first.
@UserPreference(defaultValue: [SearchBreadcrumb]())
var searchBreadcrumbs: [SearchBreadcrumb]
init(userID: String, store: UserDefaultsProtocol) {
self.store = .init(userID: userID, store: store)
self.userID = userID
}
func migrateAppSettingsValue(_ keyPath: KeyPath<AccountSettings, UserPreferenceKey>) {
let storeKey = self[keyPath: keyPath].rawValue
store.migrateAppSettingsValue(forKey: storeKey)
}
func reset() {
MXLog.warning("Resetting AccountSettings for \(userID).")
store.reset()
}
}
@@ -69,12 +69,6 @@ final nonisolated class AppSettings: @unchecked Sendable {
store.reset()
}
func resetSessionSpecificSettings() {
MXLog.warning("Resetting the user session specific AppSettings.")
resetHasRunIdentityConfirmationOnboarding()
resetSearchBreadcrumbs()
}
// MARK: - Hooks
// swiftlint:disable:next function_parameter_count
@@ -338,9 +332,6 @@ final nonisolated class AppSettings: @unchecked Sendable {
@UserPreference(defaultValue: false)
var hasRunNotificationPermissionsOnboarding: Bool
@UserPreference(defaultValue: false)
var hasRunIdentityConfirmationOnboarding: Bool
@UserPreference(defaultValue: false)
var hasRequestedLocationAlwaysLocationAuthorization: Bool
@@ -363,12 +354,6 @@ final nonisolated class AppSettings: @unchecked Sendable {
@UserPreference(defaultValue: true)
var showAllRoomListActivity: Bool
// MARK: - Search Screen
/// The queries the user searched for and the rooms they opened from the results, most recent first.
@UserPreference(defaultValue: [SearchBreadcrumb]())
var searchBreadcrumbs: [SearchBreadcrumb]
// MARK: - Room Screen
@UserPreference(defaultValue: AppBuildType.current == .debug)
@@ -482,6 +467,10 @@ final nonisolated class AppSettings: @unchecked Sendable {
self.store = store
}
func userSettings(for userID: String) -> UserSettings {
UserSettings(appSettings: self, accountSettings: .init(userID: userID, store: store))
}
static func volatile() -> AppSettings {
AppSettings(store: VolatileUserDefaults())
}
@@ -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
}
}
@@ -15,6 +15,7 @@ nonisolated protocol UserDefaultsProtocol: AnyObject {
func set(_ value: Any?, forKey key: String)
func reset()
func dictionaryRepresentation() -> [String: Any]
}
// MARK: - Codable handling
@@ -42,4 +42,8 @@ final nonisolated class VolatileUserDefaults: UserDefaultsProtocol, Sendable {
func reset() {
storage = [:]
}
func dictionaryRepresentation() -> [String: Any] {
storage
}
}
@@ -6,22 +6,23 @@
//
import Foundation
import Macros
/// The settings for a signed in account, composed of the app wide settings and the settings
/// specific to that account.
@dynamicMemberLookup
final nonisolated class UserSettings: Sendable {
// TODO: @pixlwave Make this private after handling Advanced/Labs/Developer screen models.
let app: AppSettings
let account: AccountSettings
private let account: AccountSettings
init(appSettings: AppSettings, accountSettings: AccountSettings) {
self.app = appSettings
self.account = accountSettings
}
static func volatile() -> UserSettings {
let appSettings = AppSettings.volatile()
return UserSettings(appSettings: appSettings, accountSettings: .init())
static func mock(userID: String = "@me:matrix.org") -> UserSettings {
AppSettings.volatile().userSettings(for: userID)
}
subscript<Value>(dynamicMember keyPath: ReferenceWritableKeyPath<AppSettings, Value>) -> Value {
@@ -41,9 +42,12 @@ final nonisolated class UserSettings: Sendable {
subscript<Value>(dynamicMember keyPath: KeyPath<AccountSettings, Value>) -> Value {
account[keyPath: keyPath]
}
}
/// Settings specific to an account.
struct AccountSettings {
// Placeholder for now, implementation to follow with a dedicated user defaults store scoped to a specific user ID.
func migrateAppSettingsValueToAccountSettings(_ keyPath: KeyPath<AccountSettings, UserPreferenceKey>) {
account.migrateAppSettingsValue(keyPath)
}
func resetSessionSpecificSettings() {
account.reset()
}
}