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

139 lines
4 KiB
Swift
Raw Normal View History

import DesignSystem
2023-01-17 10:36:01 +00:00
import Env
import SwiftUI
public struct AppAccountsSelectorView: View {
@EnvironmentObject private var preferences: UserPreferences
@EnvironmentObject private var currentAccount: CurrentAccount
@EnvironmentObject private var appAccounts: AppAccountsManager
2023-03-08 18:02:31 +00:00
@EnvironmentObject private var theme: Theme
2023-01-17 10:36:01 +00:00
@ObservedObject var routerPath: RouterPath
2023-01-17 10:36:01 +00:00
@State private var accountsViewModel: [AppAccountViewModel] = []
2023-03-08 18:02:31 +00:00
@State private var isPresented: Bool = false
2023-01-16 12:39:35 +00:00
private let accountCreationEnabled: Bool
private let avatarSize: AvatarView.Size
2023-02-26 05:45:57 +00:00
var showNotificationBadge: Bool {
accountsViewModel
2023-02-26 05:45:57 +00:00
.filter { $0.account?.id != currentAccount.account?.id }
.compactMap { $0.appAccount.oauthToken }
.map { preferences.getNotificationsCount(for: $0) }
.reduce(0, +) > 0
}
2023-01-17 10:36:01 +00:00
public init(routerPath: RouterPath,
2023-01-16 12:39:35 +00:00
accountCreationEnabled: Bool = true,
2023-01-17 10:36:01 +00:00
avatarSize: AvatarView.Size = .badge)
{
self.routerPath = routerPath
2023-01-16 12:39:35 +00:00
self.accountCreationEnabled = accountCreationEnabled
self.avatarSize = avatarSize
}
2023-01-17 10:36:01 +00:00
public var body: some View {
2023-03-08 18:02:31 +00:00
Button {
isPresented.toggle()
HapticManager.shared.fireHaptic(of: .buttonPress)
2023-03-08 18:02:31 +00:00
} label: {
labelView
}
2023-03-08 18:02:31 +00:00
.sheet(isPresented: $isPresented, content: {
2023-03-09 05:47:06 +00:00
accountsView.presentationDetents([.medium, .large])
2023-03-08 18:02:31 +00:00
.onAppear {
refreshAccounts()
}
})
.onChange(of: currentAccount.account?.id) { _ in
refreshAccounts()
}
.onAppear {
refreshAccounts()
}
}
2023-01-17 10:36:01 +00:00
2023-01-16 20:15:33 +00:00
@ViewBuilder
private var labelView: some View {
Group {
if let avatar = currentAccount.account?.avatar, !currentAccount.isLoadingAccount {
AvatarView(url: avatar, size: avatarSize)
} else {
AvatarView(url: nil, size: avatarSize)
.redacted(reason: .placeholder)
}
}.overlay(alignment: .topTrailing) {
2023-03-08 18:02:31 +00:00
if (!currentAccount.followRequests.isEmpty || showNotificationBadge) && accountCreationEnabled {
Circle()
.fill(Color.red)
.frame(width: 9, height: 9)
}
2023-01-16 20:15:33 +00:00
}
2023-02-18 06:26:48 +00:00
.accessibilityLabel("accessibility.app-account.selector.accounts")
2023-01-16 20:15:33 +00:00
}
2023-01-17 10:36:01 +00:00
2023-03-08 18:02:31 +00:00
private var accountsView: some View {
NavigationStack {
List {
Section {
ForEach(accountsViewModel.sorted { $0.acct < $1.acct }, id: \.appAccount.id) { viewModel in
AppAccountView(viewModel: viewModel)
2023-01-16 20:15:33 +00:00
}
2023-03-08 18:02:31 +00:00
}
.listRowBackground(theme.primaryBackgroundColor)
if accountCreationEnabled {
Section {
Button {
isPresented = false
HapticManager.shared.fireHaptic(of: .buttonPress)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
routerPath.presentedSheet = .addAccount
}
} label: {
Label("app-account.button.add", systemImage: "person.badge.plus")
}
2023-03-08 18:02:31 +00:00
settingsButton
2023-01-16 20:15:33 +00:00
}
2023-03-08 18:02:31 +00:00
.listRowBackground(theme.primaryBackgroundColor)
2023-01-16 20:15:33 +00:00
}
}
2023-03-08 18:02:31 +00:00
.listStyle(.insetGrouped)
.scrollContentBackground(.hidden)
.background(theme.secondaryBackgroundColor)
.navigationTitle("settings.section.accounts")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button {
isPresented.toggle()
} label: {
Image(systemName: "xmark.circle")
}
}
2023-01-16 20:15:33 +00:00
}
}
2023-03-08 18:02:31 +00:00
}
private var settingsButton: some View {
Button {
isPresented = false
HapticManager.shared.fireHaptic(of: .buttonPress)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
routerPath.presentedSheet = .settings
}
2023-03-08 18:02:31 +00:00
} label: {
Label("tab.settings", systemImage: "gear")
}
2023-01-16 20:15:33 +00:00
}
2023-01-17 10:36:01 +00:00
private func refreshAccounts() {
2023-03-08 18:02:31 +00:00
accountsViewModel = []
for account in appAccounts.availableAccounts {
let viewModel: AppAccountViewModel = .init(appAccount: account, isInNavigation: false, showBadge: true)
accountsViewModel.append(viewModel)
}
}
}