yattee/Shared/Navigation/AccountsMenuView.swift

45 lines
1.3 KiB
Swift
Raw Normal View History

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