added multi accounting #4
This commit is contained in:
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)
|
||||
|
||||
Reference in new issue
Block a user