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

@ -81,8 +81,12 @@ final class AccountsModel: ObservableObject {
piped.setAccount(account) piped.setAccount(account)
} }
Defaults[.lastAccountID] = account.anonymous ? (account.isPublic ? "public" : nil) : account.id Defaults[.lastAccountIsPublic] = account.isPublic
Defaults[.lastInstanceID] = account.instanceID
if !account.isPublic {
Defaults[.lastAccountID] = account.anonymous ? nil : account.id
Defaults[.lastInstanceID] = account.instanceID
}
} }
static func find(_ id: Account.ID) -> Account? { static func find(_ id: Account.ID) -> Account? {

View File

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

View File

@ -26,6 +26,7 @@ extension Defaults.Keys {
static let lastAccountID = Key<Account.ID?>("lastAccountID") static let lastAccountID = Key<Account.ID?>("lastAccountID")
static let lastInstanceID = Key<Instance.ID?>("lastInstanceID") static let lastInstanceID = Key<Instance.ID?>("lastInstanceID")
static let lastUsedPlaylistID = Key<Playlist.ID?>("lastPlaylistID") static let lastUsedPlaylistID = Key<Playlist.ID?>("lastPlaylistID")
static let lastAccountIsPublic = Key<Bool>("lastAccountIsPublic", default: false)
static let sponsorBlockInstance = Key<String>("sponsorBlockInstance", default: "https://sponsor.ajay.app") static let sponsorBlockInstance = Key<String>("sponsorBlockInstance", default: "https://sponsor.ajay.app")
static let sponsorBlockCategories = Key<Set<String>>("sponsorBlockCategories", default: Set(SponsorBlockAPI.categories)) static let sponsorBlockCategories = Key<Set<String>>("sponsorBlockCategories", default: Set(SponsorBlockAPI.categories))

View File

@ -174,7 +174,7 @@ struct YatteeApp: App {
} }
#endif #endif
if Defaults[.lastAccountID] != "public" { if !Defaults[.lastAccountIsPublic] {
accounts.configureAccount() accounts.configureAccount()
} }