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
|
2022-08-25 23:36:46 +00:00
|
|
|
var app: VideosApp?
|
2022-07-01 21:28:32 +00:00
|
|
|
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
|
2022-08-25 23:36:46 +00:00
|
|
|
var username: String
|
|
|
|
var password: String?
|
2021-10-20 22:21:50 +00:00
|
|
|
let anonymous: Bool
|
2022-07-01 21:28:32 +00:00
|
|
|
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,
|
2022-07-01 21:28:32 +00:00
|
|
|
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,
|
2021-11-14 23:06:01 +00:00
|
|
|
username: String? = nil,
|
|
|
|
password: String? = nil,
|
2022-07-01 21:28:32 +00:00
|
|
|
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)
|
2022-07-01 21:28:32 +00:00
|
|
|
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 ?? ""
|
2021-11-14 23:06:01 +00:00
|
|
|
self.username = username ?? ""
|
|
|
|
self.password = password ?? ""
|
2022-07-01 21:28:32 +00:00
|
|
|
self.country = country
|
|
|
|
self.region = region
|
2022-08-25 23:36:46 +00:00
|
|
|
self.app = app ?? instance.app
|
|
|
|
}
|
|
|
|
|
2022-12-09 00:15:19 +00:00
|
|
|
var url: URL! {
|
|
|
|
URL(string: urlString)
|
|
|
|
}
|
|
|
|
|
2022-08-25 23:36:46 +00:00
|
|
|
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)
|
2022-07-01 21:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var isPublic: Bool {
|
|
|
|
instanceID.isNil
|
2021-10-20 22:21:50 +00:00
|
|
|
}
|
|
|
|
|
2023-07-25 11:08:21 +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) {
|
2021-11-14 23:06:01 +00:00
|
|
|
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
|
|
|
}
|