yattee/Model/Accounts/Account.swift

84 lines
2.0 KiB
Swift
Raw Normal View History

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
var app: VideosApp?
let instanceID: String?
2021-10-20 22:21:50 +00:00
var name: String?
let url: String
var username: String
var password: String?
2021-10-20 22:21:50 +00:00
let anonymous: Bool
let country: String?
let region: String?
2021-10-20 22:21:50 +00:00
2021-10-27 21:11:38 +00:00
init(
id: String? = nil,
app: VideosApp? = nil,
2021-10-27 21:11:38 +00:00
instanceID: String? = nil,
name: String? = nil,
url: String? = nil,
username: String? = nil,
password: String? = nil,
anonymous: Bool = false,
country: String? = nil,
region: String? = nil
2021-10-27 21:11:38 +00:00
) {
2021-10-20 22:21:50 +00:00
self.anonymous = anonymous
self.id = id ?? (anonymous ? "anonymous-\(instanceID ?? url ?? UUID().uuidString)" : UUID().uuidString)
self.instanceID = instanceID
2021-10-20 22:21:50 +00:00
self.name = name
self.url = url ?? ""
self.username = username ?? ""
self.password = password ?? ""
self.country = country
self.region = region
self.app = app ?? instance.app
}
var token: String? {
KeychainModel.shared.getAccountKey(self, "token")
}
var credentials: (String?, String?) {
AccountsModel.getCredentials(self)
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 } ?? Instance(app: app ?? .invidious, name: url, apiURL: url)
}
var isPublic: Bool {
instanceID.isNil
2021-10-20 22:21:50 +00:00
}
var shortUsername: String {
let (username, _) = credentials
guard let username = username,
username.count > 10
else {
return username ?? ""
2021-10-20 22:21:50 +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 {
guard let name = name, !name.isEmpty else {
return shortUsername
}
return name
2021-10-20 22:21:50 +00:00
}
func hash(into hasher: inout Hasher) {
hasher.combine(username)
2021-10-20 22:21:50 +00:00
}
}