2023-01-06 11:14:05 +00:00
|
|
|
import DesignSystem
|
2023-01-17 10:36:01 +00:00
|
|
|
import Env
|
|
|
|
import SwiftUI
|
2023-01-06 11:14:05 +00:00
|
|
|
|
2023-01-12 17:17:21 +00:00
|
|
|
public struct AppAccountsSelectorView: View {
|
2023-02-18 14:36:18 +00:00
|
|
|
@EnvironmentObject private var preferences: UserPreferences
|
2023-01-06 11:14:05 +00:00
|
|
|
@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
|
|
|
|
2023-01-17 14:14:50 +00:00
|
|
|
@ObservedObject var routerPath: RouterPath
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2023-01-06 11:14:05 +00:00
|
|
|
@State private var accountsViewModel: [AppAccountViewModel] = []
|
2023-03-08 18:02:31 +00:00
|
|
|
@State private var isPresented: Bool = false
|
2023-01-25 12:02:28 +00:00
|
|
|
|
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
|
|
|
|
2023-02-23 17:57:28 +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-02-23 17:57:28 +00:00
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2023-01-17 14:14:50 +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)
|
|
|
|
{
|
2023-01-17 14:14:50 +00:00
|
|
|
self.routerPath = routerPath
|
2023-01-16 12:39:35 +00:00
|
|
|
self.accountCreationEnabled = accountCreationEnabled
|
|
|
|
self.avatarSize = avatarSize
|
2023-01-12 17:17:21 +00:00
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2023-01-12 17:17:21 +00:00
|
|
|
public var body: some View {
|
2023-03-08 18:02:31 +00:00
|
|
|
Button {
|
|
|
|
isPresented.toggle()
|
2023-02-06 16:53:27 +00:00
|
|
|
HapticManager.shared.fireHaptic(of: .buttonPress)
|
2023-03-08 18:02:31 +00:00
|
|
|
} label: {
|
|
|
|
labelView
|
2023-01-24 10:40:18 +00:00
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
})
|
2023-01-06 11:14:05 +00:00
|
|
|
.onChange(of: currentAccount.account?.id) { _ in
|
|
|
|
refreshAccounts()
|
|
|
|
}
|
2023-03-10 07:13:40 +00:00
|
|
|
.onAppear {
|
|
|
|
refreshAccounts()
|
|
|
|
}
|
2023-01-06 11:14:05 +00:00
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2023-01-16 20:15:33 +00:00
|
|
|
@ViewBuilder
|
|
|
|
private var labelView: some View {
|
2023-01-25 12:02:28 +00:00
|
|
|
Group {
|
2023-01-30 18:14:43 +00:00
|
|
|
if let avatar = currentAccount.account?.avatar, !currentAccount.isLoadingAccount {
|
2023-01-25 12:02:28 +00:00
|
|
|
AvatarView(url: avatar, size: avatarSize)
|
|
|
|
} else {
|
2023-02-16 06:19:20 +00:00
|
|
|
AvatarView(url: nil, size: avatarSize)
|
|
|
|
.redacted(reason: .placeholder)
|
2023-01-25 12:02:28 +00:00
|
|
|
}
|
|
|
|
}.overlay(alignment: .topTrailing) {
|
2023-03-08 18:02:31 +00:00
|
|
|
if (!currentAccount.followRequests.isEmpty || showNotificationBadge) && accountCreationEnabled {
|
2023-01-25 12:02:28 +00:00
|
|
|
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-02-18 14:36:18 +00:00
|
|
|
}
|
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) {
|
2023-01-26 06:34:29 +00:00
|
|
|
routerPath.presentedSheet = .settings
|
|
|
|
}
|
2023-03-08 18:02:31 +00:00
|
|
|
} label: {
|
|
|
|
Label("tab.settings", systemImage: "gear")
|
2023-01-26 06:34:29 +00:00
|
|
|
}
|
2023-01-16 20:15:33 +00:00
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2023-01-06 11:14:05 +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)
|
2023-01-06 11:14:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|