yattee/Model/Accounts/Account.swift

88 lines
2.1 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?
2022-12-11 22:15:56 +00:00
var name: String
2022-12-09 00:15:19 +00:00
let urlString: 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,
2022-12-09 00:15:19 +00:00
urlString: 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
2022-12-09 00:15:19 +00:00
self.id = id ?? (anonymous ? "anonymous-\(instanceID ?? urlString ?? UUID().uuidString)" : UUID().uuidString)
self.instanceID = instanceID
2022-12-11 22:15:56 +00:00
self.name = name ?? ""
2022-12-09 00:15:19 +00:00
self.urlString = urlString ?? ""
self.username = username ?? ""
self.password = password ?? ""
self.country = country
self.region = region
self.app = app ?? instance.app
}
2022-12-09 00:15:19 +00:00
var url: URL! {
URL(string: urlString)
}
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! {
2023-06-07 20:37:37 +00:00
InstancesModel.shared.find(instanceID) ?? Instance(app: app ?? .invidious, name: urlString, apiURLString: urlString)
}
var isPublic: Bool {
instanceID.isNil
2021-10-20 22:21:50 +00:00
}
var isPublicAddedToCustom: Bool {
InstancesModel.shared.findByURLString(urlString) != nil
}
2021-10-20 22:21:50 +00:00
var description: String {
2022-12-12 00:18:29 +00:00
guard !isPublic else {
return name
}
2023-04-22 15:14:22 +00:00
let (username, _) = credentials
return username ?? name
2021-10-20 22:21:50 +00:00
}
2022-12-11 22:15:56 +00:00
var urlHost: String {
URLComponents(url: url, resolvingAgainstBaseURL: false)?.host ?? ""
}
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
}
2022-12-10 02:01:59 +00:00
var feedCacheKey: String {
"feed-\(id)"
}
2021-10-20 22:21:50 +00:00
}