1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 06:40:32 +05:30
yattee/Model/Accounts/Account.swift

58 lines
1.4 KiB
Swift
Raw Normal View History

2021-10-21 03:51:50 +05:30
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
let username: String
let password: String?
var token: String?
2021-10-21 03:51:50 +05:30
let anonymous: Bool
2021-10-28 02:41:38 +05:30
init(
id: String? = nil,
instanceID: String? = nil,
name: String? = nil,
url: String? = nil,
username: String? = nil,
password: String? = nil,
token: String? = nil,
2021-10-28 02:41:38 +05:30
anonymous: Bool = false
) {
2021-10-21 03:51:50 +05:30
self.anonymous = anonymous
self.id = id ?? (anonymous ? "anonymous-\(instanceID!)" : UUID().uuidString)
self.instanceID = instanceID ?? UUID().uuidString
self.name = name
self.url = url ?? ""
self.username = username ?? ""
self.token = token
self.password = password ?? ""
2021-10-21 03:51:50 +05:30
}
2021-11-13 02:16:15 +05:30
var instance: Instance! {
Defaults[.instances].first { $0.id == instanceID }
2021-10-21 03:51:50 +05:30
}
var shortUsername: String {
guard username.count > 10 else {
return username
2021-10-21 03:51:50 +05:30
}
let index = username.index(username.startIndex, offsetBy: 11)
return String(username[..<index])
2021-10-21 03:51:50 +05:30
}
var description: String {
(name != nil && name!.isEmpty) ? shortUsername : name!
2021-10-21 03:51:50 +05:30
}
func hash(into hasher: inout Hasher) {
hasher.combine(username)
2021-10-21 03:51:50 +05:30
}
}