2021-09-25 08:18:22 +00:00
|
|
|
import Defaults
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct AccountsMenuView: View {
|
2021-09-26 20:39:27 +00:00
|
|
|
@EnvironmentObject<InstancesModel> private var instancesModel
|
2021-09-25 08:18:22 +00:00
|
|
|
@EnvironmentObject<InvidiousAPI> private var api
|
|
|
|
|
|
|
|
@Default(.instances) private var instances
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
Menu {
|
2021-09-26 20:12:43 +00:00
|
|
|
ForEach(instances) { instance in
|
2021-09-25 08:18:22 +00:00
|
|
|
Button(accountButtonTitle(instance: instance, account: instance.anonymousAccount)) {
|
|
|
|
api.setAccount(instance.anonymousAccount)
|
|
|
|
}
|
|
|
|
|
2021-09-26 20:39:27 +00:00
|
|
|
ForEach(instancesModel.accounts(instance.id)) { account in
|
2021-09-25 08:18:22 +00:00
|
|
|
Button(accountButtonTitle(instance: instance, account: account)) {
|
|
|
|
api.setAccount(account)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
Label(api.account?.name ?? "Accounts", systemImage: "person.crop.circle")
|
|
|
|
.labelStyle(.titleAndIcon)
|
|
|
|
}
|
|
|
|
.disabled(instances.isEmpty)
|
|
|
|
.transaction { t in t.animation = .none }
|
|
|
|
}
|
|
|
|
|
|
|
|
func accountButtonTitle(instance: Instance, account: Instance.Account) -> String {
|
|
|
|
instances.count > 1 ? "\(account.description) — \(instance.shortDescription)" : account.description
|
|
|
|
}
|
|
|
|
}
|