2021-10-20 22:21:50 +00:00
|
|
|
import Defaults
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
struct Account: Defaults.Serializable, Hashable, Identifiable {
|
|
|
|
static var bridge = AccountsBridge()
|
|
|
|
|
|
|
|
let id: String
|
|
|
|
let instanceID: String
|
|
|
|
var name: String?
|
|
|
|
let url: String
|
2021-11-14 23:06:01 +00:00
|
|
|
let username: String
|
|
|
|
let password: String?
|
|
|
|
var token: String?
|
2021-10-20 22:21:50 +00:00
|
|
|
let anonymous: Bool
|
|
|
|
|
2021-10-27 21:11:38 +00:00
|
|
|
init(
|
|
|
|
id: String? = nil,
|
|
|
|
instanceID: String? = nil,
|
|
|
|
name: String? = nil,
|
|
|
|
url: String? = nil,
|
2021-11-14 23:06:01 +00:00
|
|
|
username: String? = nil,
|
|
|
|
password: String? = nil,
|
|
|
|
token: String? = nil,
|
2021-10-27 21:11:38 +00:00
|
|
|
anonymous: Bool = false
|
|
|
|
) {
|
2021-10-20 22:21:50 +00:00
|
|
|
self.anonymous = anonymous
|
|
|
|
|
|
|
|
self.id = id ?? (anonymous ? "anonymous-\(instanceID!)" : UUID().uuidString)
|
|
|
|
self.instanceID = instanceID ?? UUID().uuidString
|
|
|
|
self.name = name
|
|
|
|
self.url = url ?? ""
|
2021-11-14 23:06:01 +00:00
|
|
|
self.username = username ?? ""
|
|
|
|
self.token = token
|
|
|
|
self.password = password ?? ""
|
2021-10-20 22:21:50 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 20:46:15 +00:00
|
|
|
var instance: Instance! {
|
|
|
|
Defaults[.instances].first { $0.id == instanceID }
|
2021-10-20 22:21:50 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 23:06:01 +00:00
|
|
|
var shortUsername: String {
|
|
|
|
guard username.count > 10 else {
|
|
|
|
return username
|
2021-10-20 22:21:50 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 23:06:01 +00:00
|
|
|
let index = username.index(username.startIndex, offsetBy: 11)
|
|
|
|
return String(username[..<index])
|
2021-10-20 22:21:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var description: String {
|
2021-11-14 23:06:01 +00:00
|
|
|
(name != nil && name!.isEmpty) ? shortUsername : name!
|
2021-10-20 22:21:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func hash(into hasher: inout Hasher) {
|
2021-11-14 23:06:01 +00:00
|
|
|
hasher.combine(username)
|
2021-10-20 22:21:50 +00:00
|
|
|
}
|
|
|
|
}
|