added multi accounting #4
Build Unsigned iOS ipa / Build Unsigned iOS IPA (push) Canceled after 0s
Build Unsigned macOS App / Build macOS ZIP (push) Canceled after 0s
iOS CI / Build and Test SwiftUI App (push) Canceled after 0s

This commit is contained in:
wt committed 2026-09-27 22:59:20 +07:00
1 parent 95a1c62b54
commit ca1a69e61f
5 files changed
+211 -34

No files matched your search

@@ -3,6 +3,7 @@ import Foundation
final class AccountPreferences {
private let defaults: UserDefaults
private let accountKey = "luma.active-account.v1"
private let accountsKey = "luma.accounts.v1"
private let encryptionSettingsKey = "luma.encryption-settings.v1"
private let chatStateSettingsKey = "luma.chat-state-settings.v1"
@@ -15,6 +16,39 @@ final class AccountPreferences {
return try? JSONDecoder().decode(AccountConfiguration.self, from: data)
}
/// Returns every configured account. The original single-account key is
/// migrated lazily so existing installations keep working.
func loadAccounts() -> [AccountConfiguration] {
if let data = defaults.data(forKey: accountsKey),
let accounts = try? JSONDecoder().decode([AccountConfiguration].self, from: data) {
return accounts
}
guard let account = load() else { return [] }
return [account]
}
func saveAccounts(_ accounts: [AccountConfiguration]) throws {
let normalized = accounts.reduce(into: [String: AccountConfiguration]()) { result, account in
result[account.normalizedJID] = account
}.values.sorted { $0.normalizedJID < $1.normalizedJID }
defaults.set(try JSONEncoder().encode(normalized), forKey: accountsKey)
if let active = load(), normalized.contains(where: { $0.normalizedJID == active.normalizedJID }) {
try save(active)
} else if let first = normalized.first {
try save(first)
} else {
clear()
}
}
func add(_ account: AccountConfiguration) throws {
try saveAccounts(loadAccounts() + [account])
}
func remove(jid: String) throws {
try saveAccounts(loadAccounts().filter { $0.normalizedJID != jid.lowercased() })
}
func save(_ account: AccountConfiguration) throws {
let data = try JSONEncoder().encode(account)
defaults.set(data, forKey: accountKey)