2021-09-25 08:18:22 +00:00
|
|
|
import Defaults
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
struct Instance: Defaults.Serializable, Hashable, Identifiable {
|
|
|
|
struct Account: Defaults.Serializable, Hashable, Identifiable {
|
|
|
|
static var bridge = AccountsBridge()
|
2021-09-28 21:47:48 +00:00
|
|
|
static var empty = Account(instanceID: UUID(), name: "Signed Out", url: "", sid: "")
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-09-26 20:39:27 +00:00
|
|
|
let id: UUID
|
|
|
|
let instanceID: UUID
|
2021-09-25 08:18:22 +00:00
|
|
|
var name: String?
|
|
|
|
let url: String
|
|
|
|
let sid: String
|
|
|
|
|
2021-09-26 20:39:27 +00:00
|
|
|
init(id: UUID? = nil, instanceID: UUID, name: String? = nil, url: String, sid: String) {
|
2021-09-25 08:18:22 +00:00
|
|
|
self.id = id ?? UUID()
|
2021-09-26 20:39:27 +00:00
|
|
|
self.instanceID = instanceID
|
2021-09-25 08:18:22 +00:00
|
|
|
self.name = name
|
|
|
|
self.url = url
|
|
|
|
self.sid = sid
|
|
|
|
}
|
|
|
|
|
2021-09-26 22:03:33 +00:00
|
|
|
var instance: Instance {
|
|
|
|
Defaults[.instances].first { $0.id == instanceID }!
|
|
|
|
}
|
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
var anonymizedSID: String {
|
|
|
|
guard sid.count > 3 else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
let index = sid.index(sid.startIndex, offsetBy: 4)
|
|
|
|
return String(sid[..<index])
|
|
|
|
}
|
|
|
|
|
|
|
|
var description: String {
|
|
|
|
(name != nil && name!.isEmpty) ? "Unnamed (\(anonymizedSID))" : name!
|
|
|
|
}
|
|
|
|
|
2021-09-28 21:47:48 +00:00
|
|
|
var isEmpty: Bool {
|
|
|
|
self == Account.empty
|
|
|
|
}
|
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
func hash(into hasher: inout Hasher) {
|
|
|
|
hasher.combine(sid)
|
|
|
|
}
|
|
|
|
|
2021-09-26 22:03:33 +00:00
|
|
|
struct AccountsBridge: Defaults.Bridge {
|
|
|
|
typealias Value = Account
|
|
|
|
typealias Serializable = [String: String]
|
|
|
|
|
|
|
|
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
|
|
|
|
]
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 22:03:33 +00:00
|
|
|
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)
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static var bridge = InstancesBridge()
|
|
|
|
|
2021-09-26 20:39:27 +00:00
|
|
|
let id: UUID
|
2021-09-25 08:18:22 +00:00
|
|
|
let name: String
|
|
|
|
let url: String
|
|
|
|
|
2021-09-26 20:39:27 +00:00
|
|
|
init(id: UUID? = nil, name: String, url: String) {
|
2021-09-25 08:18:22 +00:00
|
|
|
self.id = id ?? UUID()
|
|
|
|
self.name = name
|
|
|
|
self.url = url
|
|
|
|
}
|
|
|
|
|
|
|
|
var description: String {
|
|
|
|
name.isEmpty ? url : "\(name) (\(url))"
|
|
|
|
}
|
|
|
|
|
|
|
|
var shortDescription: String {
|
|
|
|
name.isEmpty ? url : name
|
|
|
|
}
|
|
|
|
|
|
|
|
var anonymousAccount: Account {
|
2021-09-26 20:39:27 +00:00
|
|
|
Account(instanceID: id, name: "Anonymous", url: url, sid: "")
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct InstancesBridge: Defaults.Bridge {
|
|
|
|
typealias Value = Instance
|
|
|
|
typealias Serializable = [String: String]
|
|
|
|
|
|
|
|
func serialize(_ value: Value?) -> Serializable? {
|
|
|
|
guard let value = value else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2021-09-26 20:39:27 +00:00
|
|
|
"id": value.id.uuidString,
|
2021-09-25 08:18:22 +00:00
|
|
|
"name": value.name,
|
2021-09-26 20:39:27 +00:00
|
|
|
"url": value.url
|
2021-09-25 08:18:22 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
func deserialize(_ object: Serializable?) -> Value? {
|
|
|
|
guard
|
|
|
|
let object = object,
|
|
|
|
let id = object["id"],
|
|
|
|
let url = object["url"]
|
|
|
|
else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let uuid = UUID(uuidString: id)
|
2021-09-26 20:39:27 +00:00
|
|
|
let name = object["name"] ?? ""
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-09-26 20:39:27 +00:00
|
|
|
return Instance(id: uuid, name: name, url: url)
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func hash(into hasher: inout Hasher) {
|
|
|
|
hasher.combine(url)
|
|
|
|
}
|
|
|
|
}
|