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

92 lines
2.2 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
var app: VideosApp?
let instanceID: String?
2021-10-21 03:51:50 +05:30
var name: String?
2022-12-09 05:45:19 +05:30
let urlString: String
var username: String
var password: String?
2021-10-21 03:51:50 +05:30
let anonymous: Bool
let country: String?
let region: String?
2021-10-21 03:51:50 +05:30
2021-10-28 02:41:38 +05:30
init(
id: String? = nil,
app: VideosApp? = nil,
2021-10-28 02:41:38 +05:30
instanceID: String? = nil,
name: String? = nil,
2022-12-09 05:45:19 +05:30
urlString: String? = nil,
username: String? = nil,
password: String? = nil,
anonymous: Bool = false,
country: String? = nil,
region: String? = nil
2021-10-28 02:41:38 +05:30
) {
2021-10-21 03:51:50 +05:30
self.anonymous = anonymous
2022-12-09 05:45:19 +05:30
self.id = id ?? (anonymous ? "anonymous-\(instanceID ?? urlString ?? UUID().uuidString)" : UUID().uuidString)
self.instanceID = instanceID
2021-10-21 03:51:50 +05:30
self.name = name
2022-12-09 05:45:19 +05:30
self.urlString = urlString ?? ""
self.username = username ?? ""
self.password = password ?? ""
self.country = country
self.region = region
self.app = app ?? instance.app
}
2022-12-09 05:45:19 +05:30
var url: URL! {
URL(string: urlString)
}
var token: String? {
KeychainModel.shared.getAccountKey(self, "token")
}
var credentials: (String?, String?) {
AccountsModel.getCredentials(self)
2021-10-21 03:51:50 +05:30
}
2021-11-13 02:16:15 +05:30
var instance: Instance! {
2022-12-09 05:45:19 +05:30
Defaults[.instances].first { $0.id == instanceID } ?? Instance(app: app ?? .invidious, name: urlString, apiURLString: urlString)
}
var isPublic: Bool {
instanceID.isNil
2021-10-21 03:51:50 +05:30
}
var shortUsername: String {
let (username, _) = credentials
2022-09-28 19:57:01 +05:30
guard let username,
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 {
2022-09-28 19:57:01 +05:30
guard let name, !name.isEmpty else {
return shortUsername
}
return 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
}
2022-12-10 07:31:59 +05:30
var feedCacheKey: String {
"feed-\(id)"
}
2021-10-21 03:51:50 +05:30
}