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
|
|
|
|
2021-10-19 21:27:04 +00:00
|
|
|
@Default(.accounts) private var accounts
|
2021-09-25 08:18:22 +00:00
|
|
|
@Default(.instances) private var instances
|
2022-01-02 19:34:50 +00:00
|
|
|
@Default(.accountPickerDisplaysUsername) private var accountPickerDisplaysUsername
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2022-07-01 21:28:32 +00:00
|
|
|
@ViewBuilder var body: some View {
|
|
|
|
if !instances.isEmpty {
|
|
|
|
Menu {
|
|
|
|
ForEach(allAccounts, id: \.id) { account in
|
|
|
|
Button {
|
|
|
|
model.setCurrent(account)
|
|
|
|
} label: {
|
|
|
|
HStack {
|
|
|
|
Text(accountButtonTitle(account: account))
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
if model.current == account {
|
|
|
|
Image(systemName: "checkmark")
|
|
|
|
}
|
2022-01-02 19:34:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
2022-07-01 21:28:32 +00:00
|
|
|
} label: {
|
|
|
|
HStack {
|
|
|
|
if !accountPickerDisplaysUsername || !(model.current?.isPublic ?? true) {
|
|
|
|
Image(systemName: "globe")
|
|
|
|
}
|
|
|
|
|
|
|
|
if accountPickerDisplaysUsername {
|
|
|
|
label
|
|
|
|
.labelStyle(.titleOnly)
|
|
|
|
}
|
2021-11-28 14:37:55 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-01 21:28:32 +00:00
|
|
|
.disabled(allAccounts.isEmpty)
|
|
|
|
.transaction { t in t.animation = .none }
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
private var label: some View {
|
2022-07-01 21:28:32 +00:00
|
|
|
Label(model.current?.description ?? "Select Account", systemImage: "globe")
|
2021-11-28 14:37:55 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 22:21:50 +00:00
|
|
|
private var allAccounts: [Account] {
|
2022-07-01 21:28:32 +00:00
|
|
|
accounts + instances.map(\.anonymousAccount) + [model.publicAccount].compactMap { $0 }
|
2021-10-19 21:27:04 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 22:21:50 +00:00
|
|
|
private func accountButtonTitle(account: Account) -> String {
|
2022-07-01 21:28:32 +00:00
|
|
|
account.isPublic ? account.description : "\(account.description) — \(account.instance.shortDescription)"
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
}
|