2021-09-25 08:18:22 +00:00
|
|
|
import Defaults
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
final class InstancesModel: ObservableObject {
|
2022-11-24 20:36:05 +00:00
|
|
|
static var shared = InstancesModel()
|
|
|
|
|
|
|
|
var all: [Instance] {
|
2021-10-16 22:48:58 +00:00
|
|
|
Defaults[.instances]
|
|
|
|
}
|
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
var forPlayer: Instance? {
|
2021-12-19 16:56:47 +00:00
|
|
|
guard let id = Defaults[.playerInstanceID] else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-22 13:08:33 +00:00
|
|
|
return Self.shared.find(id)
|
2021-12-19 16:56:47 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
var lastUsed: Instance? {
|
2021-10-19 21:27:04 +00:00
|
|
|
guard let id = Defaults[.lastInstanceID] else {
|
|
|
|
return nil
|
2021-09-26 22:03:33 +00:00
|
|
|
}
|
2021-09-29 10:14:43 +00:00
|
|
|
|
2023-04-22 13:08:33 +00:00
|
|
|
return Self.shared.find(id)
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
func find(_ id: Instance.ID?) -> Instance? {
|
2021-09-25 08:18:22 +00:00
|
|
|
guard id != nil else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return Defaults[.instances].first { $0.id == id }
|
|
|
|
}
|
|
|
|
|
2023-07-25 11:08:21 +00:00
|
|
|
func findByURLString(_ urlString: String?) -> Instance? {
|
|
|
|
guard let urlString else { return nil }
|
|
|
|
|
|
|
|
return Defaults[.instances].first { $0.apiURLString == urlString }
|
|
|
|
}
|
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
func accounts(_ id: Instance.ID?) -> [Account] {
|
2021-09-26 20:39:27 +00:00
|
|
|
Defaults[.accounts].filter { $0.instanceID == id }
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
func add(app: VideosApp, name: String, url: String) -> Instance {
|
2021-11-07 21:39:28 +00:00
|
|
|
let instance = Instance(
|
2022-12-09 00:15:19 +00:00
|
|
|
app: app, id: UUID().uuidString, name: name, apiURLString: standardizedURL(url)
|
2021-11-07 21:39:28 +00:00
|
|
|
)
|
2021-09-25 08:18:22 +00:00
|
|
|
Defaults[.instances].append(instance)
|
|
|
|
|
|
|
|
return instance
|
|
|
|
}
|
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
func setFrontendURL(_ instance: Instance, _ url: String) {
|
2021-10-27 21:11:38 +00:00
|
|
|
if let index = Defaults[.instances].firstIndex(where: { $0.id == instance.id }) {
|
|
|
|
var instance = Defaults[.instances][index]
|
2021-11-07 21:39:28 +00:00
|
|
|
instance.frontendURL = standardizedURL(url)
|
2021-10-27 21:11:38 +00:00
|
|
|
|
|
|
|
Defaults[.instances][index] = instance
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
func setProxiesVideos(_ instance: Instance, _ proxiesVideos: Bool) {
|
2022-07-21 21:52:09 +00:00
|
|
|
guard let index = Defaults[.instances].firstIndex(where: { $0.id == instance.id }) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var instance = Defaults[.instances][index]
|
|
|
|
instance.proxiesVideos = proxiesVideos
|
|
|
|
|
|
|
|
Defaults[.instances][index] = instance
|
|
|
|
}
|
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
func remove(_ instance: Instance) {
|
|
|
|
let accounts = accounts(instance.id)
|
2021-09-25 08:18:22 +00:00
|
|
|
if let index = Defaults[.instances].firstIndex(where: { $0.id == instance.id }) {
|
|
|
|
Defaults[.instances].remove(at: index)
|
2021-10-19 21:27:04 +00:00
|
|
|
accounts.forEach { AccountsModel.remove($0) }
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-07 21:39:28 +00:00
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
func standardizedURL(_ url: String) -> String {
|
2022-06-26 15:35:31 +00:00
|
|
|
if url.count > 7, url.last == "/" {
|
2021-11-07 21:39:28 +00:00
|
|
|
return String(url.dropLast())
|
|
|
|
}
|
2023-06-17 12:09:51 +00:00
|
|
|
return url
|
2021-11-07 21:39:28 +00:00
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|