1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 22:00:31 +05:30
yattee/Model/Accounts/InstancesModel.swift

97 lines
2.7 KiB
Swift
Raw Normal View History

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