Improve switching between public and private locations

This commit is contained in:
Arkadiusz Fal
2022-07-04 11:35:27 +02:00
parent 4d3eb05d65
commit 3b2f703e47
4 changed files with 19 additions and 5 deletions

View File

@@ -51,15 +51,24 @@ final class InstancesManifest: Service, ObservableObject {
func changePublicAccount(_ accounts: AccountsModel, settings: SettingsModel) {
instancesList.load().onSuccess { response in
if let instances: [ManifestedInstance] = response.typedContent() {
let countryInstances = instances.filter { $0.country == Defaults[.countryOfPublicInstances] }
var countryInstances = instances.filter { $0.country == Defaults[.countryOfPublicInstances] }
let region = countryInstances.first?.region ?? "Europe"
var regionInstances = instances.filter { $0.region == region }
if let publicAccountUrl = accounts.publicAccount?.url {
countryInstances = countryInstances.filter { $0.url.absoluteString != publicAccountUrl }
regionInstances = regionInstances.filter { $0.url.absoluteString != publicAccountUrl }
}
guard let instance = regionInstances.randomElement() else {
var instance: ManifestedInstance?
if accounts.current?.isPublic ?? false {
instance = regionInstances.randomElement()
} else {
instance = countryInstances.randomElement() ?? regionInstances.randomElement()
}
guard let instance = instance else {
settings.presentAlert(title: "Could not change location", message: "No locations available at the moment")
return
}