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

76 lines
2.0 KiB
Swift
Raw Normal View History

2021-09-25 13:48:22 +05:30
import Defaults
import Foundation
struct Instance: Defaults.Serializable, Hashable, Identifiable {
static var bridge = InstancesBridge()
2021-10-21 03:51:50 +05:30
let app: VideosApp
let id: String
2021-09-25 13:48:22 +05:30
let name: String
2022-12-09 05:45:19 +05:30
let apiURLString: String
2021-10-28 02:41:38 +05:30
var frontendURL: String?
var proxiesVideos: Bool
2021-09-25 13:48:22 +05:30
2022-12-09 05:45:19 +05:30
init(app: VideosApp, id: String? = nil, name: String? = nil, apiURLString: String, frontendURL: String? = nil, proxiesVideos: Bool = false) {
2021-10-17 04:18:58 +05:30
self.app = app
self.id = id ?? UUID().uuidString
2022-12-09 05:45:19 +05:30
self.name = name ?? app.rawValue
self.apiURLString = apiURLString
2021-10-28 02:41:38 +05:30
self.frontendURL = frontendURL
self.proxiesVideos = proxiesVideos
2021-09-25 13:48:22 +05:30
}
2022-12-09 05:45:19 +05:30
var apiURL: URL! {
URL(string: apiURLString)
}
var anonymous: VideosAPI! {
2021-10-21 03:51:50 +05:30
switch app {
case .invidious:
return InvidiousAPI(account: anonymousAccount)
case .piped:
return PipedAPI(account: anonymousAccount)
2022-12-09 05:45:19 +05:30
case .peerTube:
return PeerTubeAPI(account: anonymousAccount)
case .local:
return nil
2021-10-21 03:51:50 +05:30
}
}
2021-09-25 13:48:22 +05:30
var description: String {
2021-10-17 04:18:58 +05:30
"\(app.name) - \(shortDescription)"
}
var longDescription: String {
2022-12-13 00:16:45 +05:30
name.isEmpty ? "\(app.name) - \(apiURLString)" : "\(app.name) - \(name) (\(apiURLString))"
2021-09-25 13:48:22 +05:30
}
var shortDescription: String {
2022-12-09 05:45:19 +05:30
name.isEmpty ? apiURLString : name
2021-09-25 13:48:22 +05:30
}
var anonymousAccount: Account {
2022-12-09 05:45:19 +05:30
Account(instanceID: id, name: "Anonymous".localized(), urlString: apiURLString, anonymous: true)
2021-09-25 13:48:22 +05:30
}
2021-10-27 04:29:59 +05:30
var urlComponents: URLComponents {
2022-12-09 05:45:19 +05:30
URLComponents(url: apiURL, resolvingAgainstBaseURL: false)!
2021-10-27 04:29:59 +05:30
}
2021-10-28 22:44:55 +05:30
var frontendHost: String? {
2022-12-09 05:45:19 +05:30
guard let url = app == .invidious ? apiURLString : frontendURL else {
2021-10-28 22:44:55 +05:30
return nil
}
return URLComponents(string: url)?.host
2021-10-27 04:29:59 +05:30
}
2021-09-25 13:48:22 +05:30
func hash(into hasher: inout Hasher) {
2021-10-28 02:41:38 +05:30
hasher.combine(apiURL)
2021-09-25 13:48:22 +05:30
}
2024-02-02 04:24:16 +05:30
var accounts: [Account] {
AccountsModel.shared.all.filter { $0.instanceID == id }
}
2021-09-25 13:48:22 +05:30
}