1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 22:00:31 +05:30
yattee/tvOS/AccountSelectionView.swift

52 lines
1.5 KiB
Swift
Raw Normal View History

import Defaults
import Foundation
import SwiftUI
struct AccountSelectionView: View {
2021-10-18 04:36:00 +05:30
var showHeader = true
@EnvironmentObject<AccountsModel> private var accountsModel
@Default(.accounts) private var accounts
@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)) {
if let account = nextAccount {
accountsModel.setCurrent(account)
}
}
.disabled(instances.isEmpty)
.contextMenu {
ForEach(allAccounts) { account in
2021-10-17 04:18:58 +05:30
Button(accountButtonTitle(account: account)) {
accountsModel.setCurrent(account)
}
}
2021-09-29 18:06:52 +05:30
Button("Cancel", role: .cancel) {}
}
}
2021-10-17 04:18:58 +05:30
.id(UUID())
}
2021-10-21 03:51:50 +05:30
var allAccounts: [Account] {
accounts + instances.map(\.anonymousAccount)
}
2021-10-21 03:51:50 +05:30
private var nextAccount: Account? {
allAccounts.next(after: accountsModel.current)
}
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
}
}