IceCubesApp/Packages/AppAccount/Sources/AppAccount/AppAccountsSelectorView.swift

72 lines
2 KiB
Swift
Raw Normal View History

import SwiftUI
import Env
import DesignSystem
public struct AppAccountsSelectorView: View {
@EnvironmentObject private var currentAccount: CurrentAccount
@EnvironmentObject private var appAccounts: AppAccountsManager
@ObservedObject var routeurPath: RouterPath
@State private var accountsViewModel: [AppAccountViewModel] = []
public init(routeurPath: RouterPath) {
self.routeurPath = routeurPath
}
public var body: some View {
2023-01-10 05:58:50 +00:00
Menu {
ForEach(accountsViewModel, id: \.appAccount.id) { viewModel in
2023-01-10 05:58:50 +00:00
Section(viewModel.acct) {
Button {
if let account = currentAccount.account,
viewModel.account?.id == account.id {
routeurPath.navigate(to: .accountDetailWithAccount(account: account))
} else {
appAccounts.currentAccount = viewModel.appAccount
}
} label: {
HStack {
if viewModel.account?.id == currentAccount.account?.id {
Image(systemName: "checkmark.circle.fill")
}
Text("\(viewModel.account?.displayName ?? "")")
}
}
}
}
2023-01-10 05:58:50 +00:00
Divider()
Button {
routeurPath.presentedSheet = .addAccount
} label: {
Label("Add Account", systemImage: "person.badge.plus")
}
2023-01-10 05:58:50 +00:00
} label: {
if let avatar = currentAccount.account?.avatar {
AvatarView(url: avatar, size: .badge)
} else {
EmptyView()
}
}
.onAppear {
refreshAccounts()
}
.onChange(of: currentAccount.account?.id) { _ in
refreshAccounts()
}
}
private func refreshAccounts() {
if accountsViewModel.isEmpty || appAccounts.availableAccounts.count != accountsViewModel.count {
accountsViewModel = []
for account in appAccounts.availableAccounts {
let viewModel: AppAccountViewModel = .init(appAccount: account)
Task {
await viewModel.fetchAccount()
2023-01-10 05:58:50 +00:00
accountsViewModel.append(viewModel)
}
}
}
}
}