yattee/tvOS/AccountSelectionView.swift

52 lines
1.7 KiB
Swift
Raw Normal View History

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