1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 13:50:32 +05:30
yattee/Shared/Navigation/AccountsMenuView.swift

60 lines
2.1 KiB
Swift
Raw Normal View History

2021-09-25 13:48:22 +05:30
import Defaults
import SwiftUI
struct AccountsMenuView: View {
@ObservedObject private var model = AccountsModel.shared
2021-09-25 13:48:22 +05:30
@Default(.accounts) private var accounts
2021-09-25 13:48:22 +05:30
@Default(.instances) private var instances
@Default(.accountPickerDisplaysUsername) private var accountPickerDisplaysUsername
@Default(.accountPickerDisplaysAnonymousAccounts) private var accountPickerDisplaysAnonymousAccounts
2021-09-25 13:48:22 +05:30
@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")
}
}
}
2021-09-25 13:48:22 +05:30
}
} label: {
HStack {
if !accountPickerDisplaysUsername || !(model.current?.isPublic ?? true) {
Image(systemName: "globe")
}
if accountPickerDisplaysUsername {
label
.labelStyle(.titleOnly)
}
2021-11-28 20:07:55 +05:30
}
}
.disabled(allAccounts.isEmpty)
.transaction { t in t.animation = .none }
2021-09-25 13:48:22 +05:30
}
}
2021-11-28 20:07:55 +05:30
private var label: some View {
Label(model.current?.description ?? "Select Account", systemImage: "globe")
2021-11-28 20:07:55 +05:30
}
2021-10-21 03:51:50 +05:30
private var allAccounts: [Account] {
let anonymousAccounts = accountPickerDisplaysAnonymousAccounts ? instances.map(\.anonymousAccount) : []
return accounts + anonymousAccounts + [model.publicAccount].compactMap { $0 }
}
2021-10-21 03:51:50 +05:30
private func accountButtonTitle(account: Account) -> String {
account.isPublic ? account.description : "\(account.description)\(account.instance.shortDescription)"
2021-09-25 13:48:22 +05:30
}
}