Better accounts selector / switcher

This commit is contained in:
Thomas Ricouard 2023-01-10 06:58:50 +01:00
parent 75d8b9c90b
commit be4b61ed30
6 changed files with 52 additions and 33 deletions

View File

@ -1,7 +1,9 @@
import SwiftUI
import DesignSystem
import Env
struct AppAccountView: View {
@EnvironmentObject private var routeurPath: RouterPath
@EnvironmentObject var appAccounts: AppAccountsManager
@StateObject var viewModel: AppAccountViewModel
@ -25,11 +27,22 @@ struct AppAccountView: View {
.foregroundColor(.gray)
}
}
Spacer()
Image(systemName: "chevron.right")
.foregroundColor(.gray)
}
.onAppear {
Task {
await viewModel.fetchAccount()
}
}
.onTapGesture {
if appAccounts.currentAccount.id == viewModel.appAccount.id,
let account = viewModel.account {
routeurPath.navigate(to: .accountDetailWithAccount(account: account))
} else {
appAccounts.currentAccount = viewModel.appAccount
}
}
}
}

View File

@ -9,6 +9,10 @@ public class AppAccountViewModel: ObservableObject {
@Published var account: Account?
var acct: String {
"@\(account?.acct ?? "...")@\(appAccount.server)"
}
init(appAccount: AppAccount) {
self.appAccount = appAccount
self.client = .init(server: appAccount.server, oauthToken: appAccount.oauthToken)

View File

@ -11,9 +11,31 @@ struct AppAccountsSelectorView: View {
@State private var accountsViewModel: [AppAccountViewModel] = []
var body: some View {
Button {
if let account = currentAccount.account {
routeurPath.navigate(to: .accountDetailWithAccount(account: account))
Menu {
ForEach(accountsViewModel, id: \.appAccount.id) { viewModel in
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 ?? "")")
}
}
}
}
Divider()
Button {
routeurPath.presentedSheet = .addAccount
} label: {
Label("Add Account", systemImage: "person.badge.plus")
}
} label: {
if let avatar = currentAccount.account?.avatar {
@ -25,25 +47,6 @@ struct AppAccountsSelectorView: View {
.onAppear {
refreshAccounts()
}
.contextMenu {
ForEach(accountsViewModel, id: \.appAccount.id) { viewModel in
Button {
appAccounts.currentAccount = viewModel.appAccount
} label: {
HStack {
if viewModel.account?.id == currentAccount.account?.id {
Image(systemName: "checkmark.circle.fill")
}
Text("\(viewModel.account?.displayName ?? "")")
}
}
}
Button {
routeurPath.presentedSheet = .addAccount
} label: {
Label("Add Account", systemImage: "person.badge.plus")
}
}
.onChange(of: currentAccount.account?.id) { _ in
refreshAccounts()
}
@ -54,9 +57,9 @@ struct AppAccountsSelectorView: View {
accountsViewModel = []
for account in appAccounts.availableAccounts {
let viewModel: AppAccountViewModel = .init(appAccount: account)
accountsViewModel.append(viewModel)
Task {
await viewModel.fetchAccount()
accountsViewModel.append(viewModel)
}
}
}

View File

@ -18,6 +18,8 @@ struct SettingsTabs: View {
@State private var addAccountSheetPresented = false
@Binding var popToRootTab: Tab
var body: some View {
NavigationStack(path: $routeurPath.path) {
Form {
@ -43,19 +45,17 @@ struct SettingsTabs: View {
}
.withSafariRouteur()
.environmentObject(routeurPath)
.onChange(of: $popToRootTab.wrappedValue) { popToRootTab in
if popToRootTab == .notifications {
routeurPath.path = []
}
}
}
private var accountsSection: some View {
Section("Accounts") {
ForEach(appAccountsManager.availableAccounts) { account in
HStack {
AppAccountView(viewModel: .init(appAccount: account))
}
.onTapGesture {
withAnimation {
appAccountsManager.currentAccount = account
}
}
AppAccountView(viewModel: .init(appAccount: account))
}
.onDelete { indexSet in
if let index = indexSet.first {

View File

@ -31,7 +31,7 @@ enum Tab: Int, Identifiable, Hashable {
case .messages:
MessagesTab(popToRootTab: popToRootTab)
case .settings:
SettingsTabs()
SettingsTabs(popToRootTab: popToRootTab)
case .other:
EmptyView()
}

View File

@ -33,7 +33,6 @@ struct ConversationsListRow: View {
.font(.footnote)
}
Text(conversation.lastStatus.content.asRawText)
.foregroundColor(.gray)
.multilineTextAlignment(.leading)
}
Spacer()