mirror of
https://github.com/yattee/yattee.git
synced 2025-08-06 10:44:06 +00:00
Setting default account
This commit is contained in:
@@ -19,6 +19,10 @@ struct Instance: Defaults.Serializable, Hashable, Identifiable {
|
||||
self.sid = sid
|
||||
}
|
||||
|
||||
var instance: Instance {
|
||||
Defaults[.instances].first { $0.id == instanceID }!
|
||||
}
|
||||
|
||||
var anonymizedSID: String {
|
||||
guard sid.count > 3 else {
|
||||
return ""
|
||||
@@ -35,42 +39,42 @@ struct Instance: Defaults.Serializable, Hashable, Identifiable {
|
||||
func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(sid)
|
||||
}
|
||||
}
|
||||
|
||||
struct AccountsBridge: Defaults.Bridge {
|
||||
typealias Value = Account
|
||||
typealias Serializable = [String: String]
|
||||
struct AccountsBridge: Defaults.Bridge {
|
||||
typealias Value = Account
|
||||
typealias Serializable = [String: String]
|
||||
|
||||
func serialize(_ value: Value?) -> Serializable? {
|
||||
guard let value = value else {
|
||||
return nil
|
||||
func serialize(_ value: Value?) -> Serializable? {
|
||||
guard let value = value else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return [
|
||||
"id": value.id.uuidString,
|
||||
"instanceID": value.instanceID.uuidString,
|
||||
"name": value.name ?? "",
|
||||
"url": value.url,
|
||||
"sid": value.sid
|
||||
]
|
||||
}
|
||||
|
||||
return [
|
||||
"id": value.id.uuidString,
|
||||
"instanceID": value.instanceID.uuidString,
|
||||
"name": value.name ?? "",
|
||||
"url": value.url,
|
||||
"sid": value.sid
|
||||
]
|
||||
}
|
||||
func deserialize(_ object: Serializable?) -> Value? {
|
||||
guard
|
||||
let object = object,
|
||||
let id = object["id"],
|
||||
let instanceID = object["instanceID"],
|
||||
let url = object["url"],
|
||||
let sid = object["sid"]
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
|
||||
func deserialize(_ object: Serializable?) -> Value? {
|
||||
guard
|
||||
let object = object,
|
||||
let id = object["id"],
|
||||
let instanceID = object["instanceID"],
|
||||
let url = object["url"],
|
||||
let sid = object["sid"]
|
||||
else {
|
||||
return nil
|
||||
let uuid = UUID(uuidString: id)
|
||||
let instanceUUID = UUID(uuidString: instanceID)!
|
||||
let name = object["name"] ?? ""
|
||||
|
||||
return Account(id: uuid, instanceID: instanceUUID, name: name, url: url, sid: sid)
|
||||
}
|
||||
|
||||
let uuid = UUID(uuidString: id)
|
||||
let instanceUUID = UUID(uuidString: instanceID)!
|
||||
let name = object["name"] ?? ""
|
||||
|
||||
return Account(id: uuid, instanceID: instanceUUID, name: name, url: url, sid: sid)
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -2,8 +2,13 @@ import Defaults
|
||||
import Foundation
|
||||
|
||||
final class InstancesModel: ObservableObject {
|
||||
var defaultAccount: Instance.Account! {
|
||||
Defaults[.accounts].first
|
||||
@Published var defaultAccount: Instance.Account?
|
||||
|
||||
init() {
|
||||
if let id = Defaults[.defaultAccountID] {
|
||||
let uuid = UUID(uuidString: id)
|
||||
defaultAccount = Defaults[.accounts].first { $0.id == uuid }
|
||||
}
|
||||
}
|
||||
|
||||
func find(_ id: Instance.ID?) -> Instance? {
|
||||
@@ -26,8 +31,10 @@ final class InstancesModel: ObservableObject {
|
||||
}
|
||||
|
||||
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) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,4 +50,13 @@ final class InstancesModel: ObservableObject {
|
||||
Defaults[.accounts].remove(at: accountIndex)
|
||||
}
|
||||
}
|
||||
|
||||
func resetDefaultAccount() {
|
||||
setDefaultAccount(nil)
|
||||
}
|
||||
|
||||
func setDefaultAccount(_ account: Instance.Account?) {
|
||||
Defaults[.defaultAccountID] = account?.id.uuidString
|
||||
defaultAccount = account
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user