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-07-02 02:58:32 +05:30
|
|
|
Section(header: Text(showHeader ? "Current Location" : "")) {
|
|
|
|
Button(accountButtonTitle(account: accountsModel.current)) {
|
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
|
|
|
}
|
|
|
|
}
|
2022-07-02 02:58:32 +05:30
|
|
|
.disabled(instances.isEmpty && Defaults[.countryOfPublicInstances].isNil)
|
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] {
|
2022-07-02 02:58:32 +05:30
|
|
|
accounts + instances.map(\.anonymousAccount) + [accountsModel.publicAccount].compactMap { $0 }
|
2021-10-20 02:57:04 +05:30
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-07-02 02:58:32 +05:30
|
|
|
func accountButtonTitle(account: Account! = nil) -> String {
|
2021-10-17 04:18:58 +05:30
|
|
|
guard account != nil else {
|
|
|
|
return "Not selected"
|
|
|
|
}
|
|
|
|
|
2022-07-02 02:58:32 +05:30
|
|
|
return account.isPublic ? account.description : "\(account.description) — \(account.instance.shortDescription)"
|
2021-09-28 23:36:05 +05:30
|
|
|
}
|
|
|
|
}
|