mirror of
https://github.com/yattee/yattee.git
synced 2024-11-10 00:08:21 +00:00
70 lines
1.9 KiB
Swift
70 lines
1.9 KiB
Swift
import Defaults
|
|
import Foundation
|
|
|
|
final class InstancesModel: ObservableObject {
|
|
@Published var defaultAccount: Instance.Account?
|
|
|
|
init() {
|
|
guard let id = Defaults[.defaultAccountID],
|
|
let uuid = UUID(uuidString: id)
|
|
else {
|
|
return
|
|
}
|
|
|
|
defaultAccount = findAccount(uuid)
|
|
}
|
|
|
|
func find(_ id: Instance.ID?) -> Instance? {
|
|
guard id != nil else {
|
|
return nil
|
|
}
|
|
|
|
return Defaults[.instances].first { $0.id == id }
|
|
}
|
|
|
|
func accounts(_ id: Instance.ID?) -> [Instance.Account] {
|
|
Defaults[.accounts].filter { $0.instanceID == id }
|
|
}
|
|
|
|
func add(name: String, url: String) -> Instance {
|
|
let instance = Instance(name: name, url: url)
|
|
Defaults[.instances].append(instance)
|
|
|
|
return instance
|
|
}
|
|
|
|
func remove(_ instance: Instance) {
|
|
let accounts = accounts(instance.id)
|
|
if let index = Defaults[.instances].firstIndex(where: { $0.id == instance.id }) {
|
|
Defaults[.instances].remove(at: index)
|
|
accounts.forEach { removeAccount($0) }
|
|
}
|
|
}
|
|
|
|
func findAccount(_ id: Instance.Account.ID) -> Instance.Account? {
|
|
Defaults[.accounts].first { $0.id == id }
|
|
}
|
|
|
|
func addAccount(instance: Instance, name: String, sid: String) -> Instance.Account {
|
|
let account = Instance.Account(instanceID: instance.id, name: name, url: instance.url, sid: sid)
|
|
Defaults[.accounts].append(account)
|
|
|
|
return account
|
|
}
|
|
|
|
func removeAccount(_ account: Instance.Account) {
|
|
if let accountIndex = Defaults[.accounts].firstIndex(where: { $0.id == account.id }) {
|
|
Defaults[.accounts].remove(at: accountIndex)
|
|
}
|
|
}
|
|
|
|
func setDefaultAccount(_ account: Instance.Account?) {
|
|
Defaults[.defaultAccountID] = account?.id.uuidString
|
|
defaultAccount = account
|
|
}
|
|
|
|
func resetDefaultAccount() {
|
|
setDefaultAccount(nil)
|
|
}
|
|
}
|