mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2025-09-03 08:33:46 +00:00
Better accounts selector / switcher
This commit is contained in:
parent
75d8b9c90b
commit
be4b61ed30
6 changed files with 52 additions and 33 deletions
|
@ -1,7 +1,9 @@
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
import DesignSystem
|
import DesignSystem
|
||||||
|
import Env
|
||||||
|
|
||||||
struct AppAccountView: View {
|
struct AppAccountView: View {
|
||||||
|
@EnvironmentObject private var routeurPath: RouterPath
|
||||||
@EnvironmentObject var appAccounts: AppAccountsManager
|
@EnvironmentObject var appAccounts: AppAccountsManager
|
||||||
@StateObject var viewModel: AppAccountViewModel
|
@StateObject var viewModel: AppAccountViewModel
|
||||||
|
|
||||||
|
@ -25,11 +27,22 @@ struct AppAccountView: View {
|
||||||
.foregroundColor(.gray)
|
.foregroundColor(.gray)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Spacer()
|
||||||
|
Image(systemName: "chevron.right")
|
||||||
|
.foregroundColor(.gray)
|
||||||
}
|
}
|
||||||
.onAppear {
|
.onAppear {
|
||||||
Task {
|
Task {
|
||||||
await viewModel.fetchAccount()
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,10 @@ public class AppAccountViewModel: ObservableObject {
|
||||||
|
|
||||||
@Published var account: Account?
|
@Published var account: Account?
|
||||||
|
|
||||||
|
var acct: String {
|
||||||
|
"@\(account?.acct ?? "...")@\(appAccount.server)"
|
||||||
|
}
|
||||||
|
|
||||||
init(appAccount: AppAccount) {
|
init(appAccount: AppAccount) {
|
||||||
self.appAccount = appAccount
|
self.appAccount = appAccount
|
||||||
self.client = .init(server: appAccount.server, oauthToken: appAccount.oauthToken)
|
self.client = .init(server: appAccount.server, oauthToken: appAccount.oauthToken)
|
||||||
|
|
|
@ -11,9 +11,31 @@ struct AppAccountsSelectorView: View {
|
||||||
@State private var accountsViewModel: [AppAccountViewModel] = []
|
@State private var accountsViewModel: [AppAccountViewModel] = []
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
Button {
|
Menu {
|
||||||
if let account = currentAccount.account {
|
ForEach(accountsViewModel, id: \.appAccount.id) { viewModel in
|
||||||
routeurPath.navigate(to: .accountDetailWithAccount(account: account))
|
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: {
|
} label: {
|
||||||
if let avatar = currentAccount.account?.avatar {
|
if let avatar = currentAccount.account?.avatar {
|
||||||
|
@ -25,25 +47,6 @@ struct AppAccountsSelectorView: View {
|
||||||
.onAppear {
|
.onAppear {
|
||||||
refreshAccounts()
|
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
|
.onChange(of: currentAccount.account?.id) { _ in
|
||||||
refreshAccounts()
|
refreshAccounts()
|
||||||
}
|
}
|
||||||
|
@ -54,9 +57,9 @@ struct AppAccountsSelectorView: View {
|
||||||
accountsViewModel = []
|
accountsViewModel = []
|
||||||
for account in appAccounts.availableAccounts {
|
for account in appAccounts.availableAccounts {
|
||||||
let viewModel: AppAccountViewModel = .init(appAccount: account)
|
let viewModel: AppAccountViewModel = .init(appAccount: account)
|
||||||
accountsViewModel.append(viewModel)
|
|
||||||
Task {
|
Task {
|
||||||
await viewModel.fetchAccount()
|
await viewModel.fetchAccount()
|
||||||
|
accountsViewModel.append(viewModel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,8 @@ struct SettingsTabs: View {
|
||||||
|
|
||||||
@State private var addAccountSheetPresented = false
|
@State private var addAccountSheetPresented = false
|
||||||
|
|
||||||
|
@Binding var popToRootTab: Tab
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationStack(path: $routeurPath.path) {
|
NavigationStack(path: $routeurPath.path) {
|
||||||
Form {
|
Form {
|
||||||
|
@ -43,19 +45,17 @@ struct SettingsTabs: View {
|
||||||
}
|
}
|
||||||
.withSafariRouteur()
|
.withSafariRouteur()
|
||||||
.environmentObject(routeurPath)
|
.environmentObject(routeurPath)
|
||||||
|
.onChange(of: $popToRootTab.wrappedValue) { popToRootTab in
|
||||||
|
if popToRootTab == .notifications {
|
||||||
|
routeurPath.path = []
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var accountsSection: some View {
|
private var accountsSection: some View {
|
||||||
Section("Accounts") {
|
Section("Accounts") {
|
||||||
ForEach(appAccountsManager.availableAccounts) { account in
|
ForEach(appAccountsManager.availableAccounts) { account in
|
||||||
HStack {
|
AppAccountView(viewModel: .init(appAccount: account))
|
||||||
AppAccountView(viewModel: .init(appAccount: account))
|
|
||||||
}
|
|
||||||
.onTapGesture {
|
|
||||||
withAnimation {
|
|
||||||
appAccountsManager.currentAccount = account
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
.onDelete { indexSet in
|
.onDelete { indexSet in
|
||||||
if let index = indexSet.first {
|
if let index = indexSet.first {
|
||||||
|
|
|
@ -31,7 +31,7 @@ enum Tab: Int, Identifiable, Hashable {
|
||||||
case .messages:
|
case .messages:
|
||||||
MessagesTab(popToRootTab: popToRootTab)
|
MessagesTab(popToRootTab: popToRootTab)
|
||||||
case .settings:
|
case .settings:
|
||||||
SettingsTabs()
|
SettingsTabs(popToRootTab: popToRootTab)
|
||||||
case .other:
|
case .other:
|
||||||
EmptyView()
|
EmptyView()
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,6 @@ struct ConversationsListRow: View {
|
||||||
.font(.footnote)
|
.font(.footnote)
|
||||||
}
|
}
|
||||||
Text(conversation.lastStatus.content.asRawText)
|
Text(conversation.lastStatus.content.asRawText)
|
||||||
.foregroundColor(.gray)
|
|
||||||
.multilineTextAlignment(.leading)
|
.multilineTextAlignment(.leading)
|
||||||
}
|
}
|
||||||
Spacer()
|
Spacer()
|
||||||
|
|
Loading…
Reference in a new issue