2021-09-25 13:48:22 +05:30
|
|
|
import Defaults
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct AccountsMenuView: View {
|
2021-10-17 04:18:58 +05:30
|
|
|
@EnvironmentObject<AccountsModel> private var model
|
2021-09-25 13:48:22 +05:30
|
|
|
|
2021-10-20 02:57:04 +05:30
|
|
|
@Default(.accounts) private var accounts
|
2021-09-25 13:48:22 +05:30
|
|
|
@Default(.instances) private var instances
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
Menu {
|
2021-10-20 02:57:04 +05:30
|
|
|
ForEach(allAccounts, id: \.id) { account in
|
2021-10-17 04:18:58 +05:30
|
|
|
Button(accountButtonTitle(account: account)) {
|
2021-10-20 02:57:04 +05:30
|
|
|
model.setCurrent(account)
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
} label: {
|
2021-11-28 20:07:55 +05:30
|
|
|
if #available(iOS 15.0, macOS 12.0, *) {
|
|
|
|
label
|
|
|
|
.labelStyle(.titleAndIcon)
|
|
|
|
} else {
|
|
|
|
HStack {
|
|
|
|
Image(systemName: "person.crop.circle")
|
|
|
|
label
|
|
|
|
.labelStyle(.titleOnly)
|
|
|
|
}
|
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
.disabled(instances.isEmpty)
|
|
|
|
.transaction { t in t.animation = .none }
|
|
|
|
}
|
|
|
|
|
2021-11-28 20:07:55 +05:30
|
|
|
private var label: some View {
|
|
|
|
Label(model.current?.description ?? "Select Account", systemImage: "person.crop.circle")
|
|
|
|
}
|
|
|
|
|
2021-10-21 03:51:50 +05:30
|
|
|
private var allAccounts: [Account] {
|
2021-10-20 02:57:04 +05:30
|
|
|
accounts + instances.map(\.anonymousAccount)
|
|
|
|
}
|
|
|
|
|
2021-10-21 03:51:50 +05:30
|
|
|
private func accountButtonTitle(account: Account) -> String {
|
2021-10-17 04:18:58 +05:30
|
|
|
instances.count > 1 ? "\(account.description) — \(account.instance.description)" : account.description
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
}
|