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

81 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 {
2021-10-21 03:51:50 +05:30
struct InstancesBridge: Defaults.Bridge {
typealias Value = Instance
typealias Serializable = [String: String]
2021-09-27 03:33:33 +05:30
2021-10-21 03:51:50 +05:30
func serialize(_ value: Value?) -> Serializable? {
guard let value = value else {
return nil
2021-09-25 13:48:22 +05:30
}
2021-10-21 03:51:50 +05:30
return [
"app": value.app.rawValue,
"id": value.id,
"name": value.name,
"url": value.url
]
2021-09-25 13:48:22 +05:30
}
2021-10-21 03:51:50 +05:30
func deserialize(_ object: Serializable?) -> Value? {
guard
let object = object,
let app = VideosApp(rawValue: object["app"] ?? ""),
let id = object["id"],
let url = object["url"]
else {
return nil
2021-09-25 13:48:22 +05:30
}
2021-10-21 03:51:50 +05:30
let name = object["name"] ?? ""
2021-09-27 03:33:33 +05:30
2021-10-21 03:51:50 +05:30
return Instance(app: app, id: id, name: name, url: url)
2021-09-25 13:48:22 +05:30
}
}
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
let url: String
2021-10-21 03:51:50 +05:30
init(app: VideosApp, id: String? = nil, name: String, url: String) {
2021-10-17 04:18:58 +05:30
self.app = app
self.id = id ?? UUID().uuidString
2021-09-25 13:48:22 +05:30
self.name = name
self.url = url
}
2021-10-21 03:51:50 +05:30
var anonymous: VideosAPI {
switch app {
case .invidious:
return InvidiousAPI(account: anonymousAccount)
case .piped:
return PipedAPI(account: anonymousAccount)
}
}
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 {
name.isEmpty ? "\(app.name) - \(url)" : "\(app.name) - \(name) (\(url))"
2021-09-25 13:48:22 +05:30
}
var shortDescription: String {
name.isEmpty ? url : name
}
var anonymousAccount: Account {
2021-10-21 03:51:50 +05:30
Account(instanceID: id, name: "Anonymous", url: url, anonymous: true)
2021-09-25 13:48:22 +05:30
}
func hash(into hasher: inout Hasher) {
hasher.combine(url)
}
}