2021-09-28 18:06:05 +00:00
|
|
|
import Defaults
|
|
|
|
import Foundation
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct AccountSelectionView: View {
|
2021-10-17 23:06:00 +00:00
|
|
|
var showHeader = true
|
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
@ObservedObject private var accountsModel = AccountsModel.shared
|
2021-09-28 18:06:05 +00:00
|
|
|
|
2021-10-19 21:27:04 +00:00
|
|
|
@Default(.accounts) private var accounts
|
2021-09-28 18:06:05 +00:00
|
|
|
@Default(.instances) private var instances
|
2022-08-25 23:36:46 +00:00
|
|
|
@Default(.accountPickerDisplaysAnonymousAccounts) private var accountPickerDisplaysAnonymousAccounts
|
2021-09-28 18:06:05 +00:00
|
|
|
|
|
|
|
var body: some View {
|
2022-07-01 21:28:32 +00:00
|
|
|
Section(header: Text(showHeader ? "Current Location" : "")) {
|
|
|
|
Button(accountButtonTitle(account: accountsModel.current)) {
|
2021-09-28 18:06:05 +00:00
|
|
|
if let account = nextAccount {
|
2021-10-19 21:27:04 +00:00
|
|
|
accountsModel.setCurrent(account)
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-01 21:28:32 +00:00
|
|
|
.disabled(instances.isEmpty && Defaults[.countryOfPublicInstances].isNil)
|
2021-09-28 18:06:05 +00:00
|
|
|
.contextMenu {
|
2021-10-19 21:27:04 +00:00
|
|
|
ForEach(allAccounts) { account in
|
2021-10-16 22:48:58 +00:00
|
|
|
Button(accountButtonTitle(account: account)) {
|
2021-10-19 21:27:04 +00:00
|
|
|
accountsModel.setCurrent(account)
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-29 12:36:52 +00:00
|
|
|
|
|
|
|
Button("Cancel", role: .cancel) {}
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-16 22:48:58 +00:00
|
|
|
.id(UUID())
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 22:21:50 +00:00
|
|
|
var allAccounts: [Account] {
|
2022-08-25 23:36:46 +00:00
|
|
|
let anonymousAccounts = accountPickerDisplaysAnonymousAccounts ? instances.map(\.anonymousAccount) : []
|
|
|
|
return accounts + anonymousAccounts + [accountsModel.publicAccount].compactMap { $0 }
|
2021-10-19 21:27:04 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 22:21:50 +00:00
|
|
|
private var nextAccount: Account? {
|
2021-10-19 21:27:04 +00:00
|
|
|
allAccounts.next(after: accountsModel.current)
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 21:28:32 +00:00
|
|
|
func accountButtonTitle(account: Account! = nil) -> String {
|
2021-10-16 22:48:58 +00:00
|
|
|
guard account != nil else {
|
|
|
|
return "Not selected"
|
|
|
|
}
|
|
|
|
|
2022-07-01 21:28:32 +00:00
|
|
|
return account.isPublic ? account.description : "\(account.description) — \(account.instance.shortDescription)"
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
|
|
|
}
|