2022-07-02 02:58:32 +05:30
|
|
|
import Defaults
|
|
|
|
import Foundation
|
|
|
|
import Siesta
|
|
|
|
import SwiftyJSON
|
|
|
|
|
|
|
|
final class InstancesManifest: Service, ObservableObject {
|
|
|
|
static let shared = InstancesManifest()
|
|
|
|
|
|
|
|
@Published var instances = [ManifestedInstance]()
|
|
|
|
|
|
|
|
init() {
|
|
|
|
super.init()
|
|
|
|
|
2022-10-26 16:41:35 +05:30
|
|
|
configure()
|
|
|
|
}
|
|
|
|
|
|
|
|
func configure() {
|
|
|
|
invalidateConfiguration()
|
|
|
|
|
2022-07-02 02:58:32 +05:30
|
|
|
configure {
|
|
|
|
$0.pipeline[.parsing].add(SwiftyJSONTransformer, contentTypes: ["*/json"])
|
|
|
|
}
|
|
|
|
|
2022-10-26 16:41:35 +05:30
|
|
|
if let manifestURL {
|
|
|
|
configureTransformer(
|
|
|
|
manifestURL,
|
|
|
|
requestMethods: [.get]
|
|
|
|
) { (content: Entity<JSON>
|
|
|
|
) -> [ManifestedInstance] in
|
|
|
|
guard let instances = content.json.dictionaryValue["instances"] else { return [] }
|
2022-07-02 02:58:32 +05:30
|
|
|
|
2022-10-26 16:41:35 +05:30
|
|
|
return instances.arrayValue.compactMap(self.extractInstance)
|
|
|
|
}
|
2022-07-02 02:58:32 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 02:06:05 +05:30
|
|
|
func setPublicAccount(_ country: String?, asCurrent: Bool = true) {
|
2022-09-28 19:57:01 +05:30
|
|
|
guard let country else {
|
2022-11-25 02:06:05 +05:30
|
|
|
AccountsModel.shared.publicAccount = nil
|
2022-07-02 02:58:32 +05:30
|
|
|
if asCurrent {
|
2022-11-25 02:06:05 +05:30
|
|
|
AccountsModel.shared.configureAccount()
|
2022-07-02 02:58:32 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-26 16:41:35 +05:30
|
|
|
instancesList?.load().onSuccess { response in
|
2022-07-02 02:58:32 +05:30
|
|
|
if let instances: [ManifestedInstance] = response.typedContent() {
|
2023-06-17 17:39:51 +05:30
|
|
|
let countryInstances = instances.filter { $0.country == country }
|
|
|
|
guard let instance = countryInstances.randomElement() else { return }
|
2022-07-02 02:58:32 +05:30
|
|
|
let account = instance.anonymousAccount
|
2022-11-25 02:06:05 +05:30
|
|
|
AccountsModel.shared.publicAccount = account
|
2022-07-02 02:58:32 +05:30
|
|
|
if asCurrent {
|
2022-11-25 02:06:05 +05:30
|
|
|
AccountsModel.shared.setCurrent(account)
|
2022-07-02 02:58:32 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 02:06:05 +05:30
|
|
|
func changePublicAccount() {
|
2022-10-26 16:41:35 +05:30
|
|
|
instancesList?.load().onSuccess { response in
|
2022-07-02 02:58:32 +05:30
|
|
|
if let instances: [ManifestedInstance] = response.typedContent() {
|
2022-07-04 15:05:27 +05:30
|
|
|
var countryInstances = instances.filter { $0.country == Defaults[.countryOfPublicInstances] }
|
2022-07-02 02:58:32 +05:30
|
|
|
let region = countryInstances.first?.region ?? "Europe"
|
|
|
|
var regionInstances = instances.filter { $0.region == region }
|
|
|
|
|
2022-11-25 02:06:05 +05:30
|
|
|
if let publicAccountUrl = AccountsModel.shared.publicAccount?.url {
|
2022-12-09 05:45:19 +05:30
|
|
|
countryInstances = countryInstances.filter { $0.url != publicAccountUrl }
|
|
|
|
regionInstances = regionInstances.filter { $0.url != publicAccountUrl }
|
2022-07-02 02:58:32 +05:30
|
|
|
}
|
|
|
|
|
2022-07-04 15:05:27 +05:30
|
|
|
var instance: ManifestedInstance?
|
|
|
|
|
2022-11-25 02:06:05 +05:30
|
|
|
if AccountsModel.shared.current?.isPublic ?? false {
|
2022-07-04 15:05:27 +05:30
|
|
|
instance = regionInstances.randomElement()
|
|
|
|
} else {
|
|
|
|
instance = countryInstances.randomElement() ?? regionInstances.randomElement()
|
|
|
|
}
|
|
|
|
|
2022-09-28 19:57:01 +05:30
|
|
|
guard let instance else {
|
2022-11-25 02:06:05 +05:30
|
|
|
SettingsModel.shared.presentAlert(title: "Could not change location", message: "No locations available at the moment")
|
2022-07-02 02:58:32 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let account = instance.anonymousAccount
|
2022-11-25 02:06:05 +05:30
|
|
|
AccountsModel.shared.publicAccount = account
|
|
|
|
AccountsModel.shared.setCurrent(account)
|
2022-07-02 02:58:32 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractInstance(from json: JSON) -> ManifestedInstance? {
|
|
|
|
guard let app = json["app"].string,
|
|
|
|
let videosApp = VideosApp(rawValue: app.lowercased()),
|
|
|
|
let region = json["region"].string,
|
|
|
|
let country = json["country"].string,
|
|
|
|
let flag = json["flag"].string,
|
|
|
|
let url = json["url"].url else { return nil }
|
|
|
|
|
|
|
|
return ManifestedInstance(
|
|
|
|
app: videosApp,
|
|
|
|
country: country,
|
|
|
|
region: region,
|
|
|
|
flag: flag,
|
|
|
|
url: url
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-10-26 16:41:35 +05:30
|
|
|
var manifestURL: String? {
|
|
|
|
Defaults[.instancesManifest].isEmpty ? nil : Defaults[.instancesManifest]
|
2022-07-02 02:58:32 +05:30
|
|
|
}
|
|
|
|
|
2022-10-26 16:41:35 +05:30
|
|
|
var instancesList: Resource? {
|
|
|
|
guard let manifestURL else { return nil }
|
|
|
|
return resource(absoluteURL: manifestURL)
|
2022-07-02 02:58:32 +05:30
|
|
|
}
|
|
|
|
}
|