2021-09-28 23:36:05 +05:30
|
|
|
import Defaults
|
|
|
|
import Foundation
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct AccountSelectionView: View {
|
2021-10-18 04:36:00 +05:30
|
|
|
var showHeader = true
|
|
|
|
|
2021-10-20 02:57:04 +05:30
|
|
|
@EnvironmentObject<AccountsModel> private var accountsModel
|
2021-09-28 23:36:05 +05:30
|
|
|
|
2021-10-20 02:57:04 +05:30
|
|
|
@Default(.accounts) private var accounts
|
2021-09-28 23:36:05 +05:30
|
|
|
@Default(.instances) private var instances
|
|
|
|
|
|
|
|
var body: some View {
|
2022-01-06 20:32:53 +05:30
|
|
|
Section(header: Text(showHeader ? "Current Account" : "")) {
|
2021-10-22 20:30:09 +05:30
|
|
|
Button(accountButtonTitle(account: accountsModel.current, long: true)) {
|
2021-09-28 23:36:05 +05:30
|
|
|
if let account = nextAccount {
|
2021-10-20 02:57:04 +05:30
|
|
|
accountsModel.setCurrent(account)
|
2021-09-28 23:36:05 +05:30
|
|
|
}
|
|
|
|
}
|
2021-09-29 19:59:17 +05:30
|
|
|
.disabled(instances.isEmpty)
|
2021-09-28 23:36:05 +05:30
|
|
|
.contextMenu {
|
2021-10-20 02:57:04 +05:30
|
|
|
ForEach(allAccounts) { account in
|
2021-10-17 04:18:58 +05:30
|
|
|
Button(accountButtonTitle(account: account)) {
|
2021-10-20 02:57:04 +05:30
|
|
|
accountsModel.setCurrent(account)
|
2021-09-28 23:36:05 +05:30
|
|
|
}
|
|
|
|
}
|
2021-09-29 18:06:52 +05:30
|
|
|
|
|
|
|
Button("Cancel", role: .cancel) {}
|
2021-09-28 23:36:05 +05:30
|
|
|
}
|
|
|
|
}
|
2021-10-17 04:18:58 +05:30
|
|
|
.id(UUID())
|
2021-09-28 23:36:05 +05:30
|
|
|
}
|
|
|
|
|
2021-10-21 03:51:50 +05:30
|
|
|
var allAccounts: [Account] {
|
2021-10-20 02:57:04 +05:30
|
|
|
accounts + instances.map(\.anonymousAccount)
|
|
|
|
}
|
|
|
|
|
2021-10-21 03:51:50 +05:30
|
|
|
private var nextAccount: Account? {
|
2021-10-20 02:57:04 +05:30
|
|
|
allAccounts.next(after: accountsModel.current)
|
2021-09-28 23:36:05 +05:30
|
|
|
}
|
|
|
|
|
2021-10-22 20:30:09 +05:30
|
|
|
func accountButtonTitle(account: Account! = nil, long: Bool = false) -> String {
|
2021-10-17 04:18:58 +05:30
|
|
|
guard account != nil else {
|
|
|
|
return "Not selected"
|
|
|
|
}
|
|
|
|
|
2021-10-22 20:30:09 +05:30
|
|
|
let instanceDescription = long ? account.instance.longDescription : account.instance.description
|
|
|
|
|
|
|
|
return instances.count > 1 ? "\(account.description) — \(instanceDescription)" : account.description
|
2021-09-28 23:36:05 +05:30
|
|
|
}
|
|
|
|
}
|