2021-09-28 18:06:05 +00:00
|
|
|
import Defaults
|
|
|
|
import Foundation
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct AccountSelectionView: View {
|
|
|
|
@EnvironmentObject<InstancesModel> private var instancesModel
|
|
|
|
@EnvironmentObject<InvidiousAPI> private var api
|
|
|
|
|
|
|
|
@Default(.accounts) private var accounts
|
|
|
|
@Default(.instances) private var instances
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
Section(header: Text("Current Account")) {
|
|
|
|
Button(api.account?.name ?? "Not selected") {
|
|
|
|
if let account = nextAccount {
|
|
|
|
api.setAccount(account)
|
|
|
|
}
|
|
|
|
}
|
2021-09-29 14:29:17 +00:00
|
|
|
.disabled(instances.isEmpty)
|
2021-09-28 18:06:05 +00:00
|
|
|
.contextMenu {
|
|
|
|
ForEach(instances) { instance in
|
|
|
|
Button(accountButtonTitle(instance: instance, account: instance.anonymousAccount)) {
|
|
|
|
api.setAccount(instance.anonymousAccount)
|
|
|
|
}
|
|
|
|
|
|
|
|
ForEach(instancesModel.accounts(instance.id)) { account in
|
|
|
|
Button(accountButtonTitle(instance: instance, account: account)) {
|
|
|
|
api.setAccount(account)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-29 12:36:52 +00:00
|
|
|
|
|
|
|
Button("Cancel", role: .cancel) {}
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var nextAccount: Instance.Account? {
|
|
|
|
guard api.account != nil else {
|
|
|
|
return accounts.first
|
|
|
|
}
|
|
|
|
|
|
|
|
return accounts.next(after: api.account!)
|
|
|
|
}
|
|
|
|
|
|
|
|
func accountButtonTitle(instance: Instance, account: Instance.Account) -> String {
|
|
|
|
instances.count > 1 ? "\(account.description) — \(instance.shortDescription)" : account.description
|
|
|
|
}
|
|
|
|
}
|