mirror of
https://github.com/yattee/yattee.git
synced 2025-08-06 18:54:11 +00:00
Use separate defaults keys for instances and accounts
This commit is contained in:
@@ -5,13 +5,15 @@ struct Instance: Defaults.Serializable, Hashable, Identifiable {
|
||||
struct Account: Defaults.Serializable, Hashable, Identifiable {
|
||||
static var bridge = AccountsBridge()
|
||||
|
||||
let id: UUID?
|
||||
let id: UUID
|
||||
let instanceID: UUID
|
||||
var name: String?
|
||||
let url: String
|
||||
let sid: String
|
||||
|
||||
init(id: UUID? = nil, name: String? = nil, url: String, sid: String) {
|
||||
init(id: UUID? = nil, instanceID: UUID, name: String? = nil, url: String, sid: String) {
|
||||
self.id = id ?? UUID()
|
||||
self.instanceID = instanceID
|
||||
self.name = name
|
||||
self.url = url
|
||||
self.sid = sid
|
||||
@@ -45,7 +47,8 @@ struct Instance: Defaults.Serializable, Hashable, Identifiable {
|
||||
}
|
||||
|
||||
return [
|
||||
"id": value.id?.uuidString ?? "",
|
||||
"id": value.id.uuidString,
|
||||
"instanceID": value.instanceID.uuidString,
|
||||
"name": value.name ?? "",
|
||||
"url": value.url,
|
||||
"sid": value.sid
|
||||
@@ -55,30 +58,32 @@ struct Instance: Defaults.Serializable, Hashable, Identifiable {
|
||||
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(name: name, url: url, sid: sid)
|
||||
return Account(id: uuid, instanceID: instanceUUID, name: name, url: url, sid: sid)
|
||||
}
|
||||
}
|
||||
|
||||
static var bridge = InstancesBridge()
|
||||
|
||||
let id: UUID?
|
||||
let id: UUID
|
||||
let name: String
|
||||
let url: String
|
||||
var accounts = [Account]()
|
||||
|
||||
init(id: UUID? = nil, name: String, url: String, accounts: [Account] = []) {
|
||||
init(id: UUID? = nil, name: String, url: String) {
|
||||
self.id = id ?? UUID()
|
||||
self.name = name
|
||||
self.url = url
|
||||
self.accounts = accounts
|
||||
}
|
||||
|
||||
var description: String {
|
||||
@@ -90,7 +95,7 @@ struct Instance: Defaults.Serializable, Hashable, Identifiable {
|
||||
}
|
||||
|
||||
var anonymousAccount: Account {
|
||||
Account(name: "Anonymous", url: url, sid: "")
|
||||
Account(instanceID: id, name: "Anonymous", url: url, sid: "")
|
||||
}
|
||||
|
||||
struct InstancesBridge: Defaults.Bridge {
|
||||
@@ -103,10 +108,9 @@ struct Instance: Defaults.Serializable, Hashable, Identifiable {
|
||||
}
|
||||
|
||||
return [
|
||||
"id": value.id?.uuidString ?? "",
|
||||
"id": value.id.uuidString,
|
||||
"name": value.name,
|
||||
"url": value.url,
|
||||
"accounts": value.accounts.map { "\($0.id!):\($0.name ?? ""):\($0.sid)" }.joined(separator: ";")
|
||||
"url": value.url
|
||||
]
|
||||
}
|
||||
|
||||
@@ -119,24 +123,10 @@ struct Instance: Defaults.Serializable, Hashable, Identifiable {
|
||||
return nil
|
||||
}
|
||||
|
||||
let name = object["name"] ?? ""
|
||||
let accounts = object["accounts"] ?? ""
|
||||
let uuid = UUID(uuidString: id)
|
||||
let name = object["name"] ?? ""
|
||||
|
||||
var instance = Instance(id: uuid, name: name, url: url)
|
||||
|
||||
accounts.split(separator: ";").forEach { sid in
|
||||
let components = sid.components(separatedBy: ":")
|
||||
|
||||
let id = components[0]
|
||||
let name = components[1]
|
||||
let sid = components[2]
|
||||
|
||||
let uuid = UUID(uuidString: id)
|
||||
instance.accounts.append(Account(id: uuid, name: name, url: instance.url, sid: sid))
|
||||
}
|
||||
|
||||
return instance
|
||||
return Instance(id: uuid, name: name, url: url)
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -3,7 +3,7 @@ import Foundation
|
||||
|
||||
final class InstancesModel: ObservableObject {
|
||||
var defaultAccount: Instance.Account! {
|
||||
Defaults[.instances].first?.accounts.first
|
||||
Defaults[.accounts].first
|
||||
}
|
||||
|
||||
func find(_ id: Instance.ID?) -> Instance? {
|
||||
@@ -15,7 +15,7 @@ final class InstancesModel: ObservableObject {
|
||||
}
|
||||
|
||||
func accounts(_ id: Instance.ID?) -> [Instance.Account] {
|
||||
find(id)?.accounts ?? []
|
||||
Defaults[.accounts].filter { $0.instanceID == id }
|
||||
}
|
||||
|
||||
func add(name: String, url: String) -> Instance {
|
||||
@@ -32,20 +32,15 @@ final class InstancesModel: ObservableObject {
|
||||
}
|
||||
|
||||
func addAccount(instance: Instance, name: String, sid: String) -> Instance.Account {
|
||||
let account = Instance.Account(name: name, url: instance.url, sid: sid)
|
||||
|
||||
if let index = Defaults[.instances].firstIndex(where: { $0.id == instance.id }) {
|
||||
Defaults[.instances][index].accounts.append(account)
|
||||
}
|
||||
let account = Instance.Account(instanceID: instance.id, name: name, url: instance.url, sid: sid)
|
||||
Defaults[.accounts].append(account)
|
||||
|
||||
return account
|
||||
}
|
||||
|
||||
func removeAccount(instance: Instance, account: Instance.Account) {
|
||||
if let instanceIndex = Defaults[.instances].firstIndex(where: { $0.id == instance.id }) {
|
||||
if let accountIndex = Defaults[.instances][instanceIndex].accounts.firstIndex(where: { $0.id == account.id }) {
|
||||
Defaults[.instances][instanceIndex].accounts.remove(at: accountIndex)
|
||||
}
|
||||
func removeAccount(_ account: Instance.Account) {
|
||||
if let accountIndex = Defaults[.accounts].firstIndex(where: { $0.id == account.id }) {
|
||||
Defaults[.accounts].remove(at: accountIndex)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user