Skip to content

Commit

Permalink
AccountListView support expand collapse
Browse files Browse the repository at this point in the history
  • Loading branch information
guanlisheng committed Sep 17, 2024
1 parent 7ad0404 commit 9e46417
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
2 changes: 1 addition & 1 deletion MMEX/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"images" : [
{
"filename" : "mmex_1024.png",
"filename" : "mmex_green.png",
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 46 additions & 21 deletions MMEX/Views/AccountListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ struct AccountListView: View {
@State private var accounts_by_type: [String:[Account]] = [:]
@State private var newAccount = Account.empty
@State private var isPresentingAccountAddView = false

@State private var expandedSections: [String: Bool] = [:] // Tracks the expanded/collapsed state

private var repository: AccountRepository

init(databaseURL: URL) {
self.databaseURL = databaseURL
self.repository = DataManager(databaseURL: databaseURL).getAccountRepository()
Expand All @@ -24,34 +25,50 @@ struct AccountListView: View {
var body: some View {
NavigationStack {
List {
ForEach(self.accounts_by_type.keys.sorted(), id:\.self) { accountType in
ForEach(self.accounts_by_type.keys.sorted(), id: \.self) { accountType in
Section(
header: HStack {
if let accountSymbol = Account.accountTypeToSFSymbol[accountType] {
Image(systemName: accountSymbol)
.frame(width: 5, alignment: .leading) // Adjust width as needed
.font(.system(size: 16, weight: .bold)) // Customize size and weight
.foregroundColor(.blue) // Customize icon style
}
Text(accountType)
.font(.subheadline)
.padding(.leading)
}
) {
ForEach(accounts_by_type[accountType]!) { account in
NavigationLink(destination: AccountDetailView(account: account, databaseURL: databaseURL, currencies: $currencies)) {
HStack{
Text(account.name)
Button(action: {
// Toggle expanded/collapsed state
expandedSections[accountType]?.toggle()
}) {
HStack {
if let accountSymbol = Account.accountTypeToSFSymbol[accountType] {
Image(systemName: accountSymbol)
.frame(width: 5, alignment: .leading) // Adjust width as needed
.font(.system(size: 16, weight: .bold)) // Customize size and weight
.foregroundColor(.blue) // Customize icon style
}
Text(accountType)
.font(.subheadline)
.padding(.leading)

Spacer()

if let currency = account.currency {
Text(currency.name)
// Expand or collapse indicator
Image(systemName: expandedSections[accountType] == true ? "chevron.down" : "chevron.right")
.foregroundColor(.gray)
}
}
}
) {
// Show account list based on expandedSections state
if expandedSections[accountType] == true {
ForEach(accounts_by_type[accountType]!) { account in
NavigationLink(destination: AccountDetailView(account: account, databaseURL: databaseURL, currencies: $currencies)) {
HStack {
Text(account.name)
.font(.subheadline)

Spacer()

if let currency = account.currency {
Text(currency.name)
.font(.subheadline)
}
}
.padding(.horizontal)
}
.padding(.horizontal)
}
}
}
Expand All @@ -77,6 +94,13 @@ struct AccountListView: View {
}
}
}

// Initialize the expanded state for each account type
private func initializeExpandedSections() {
for accountType in accounts_by_type.keys {
expandedSections[accountType] = true // Default to expanded
}
}

func loadAccounts() {
print("Loading payees in AccountListView...")
Expand All @@ -88,6 +112,7 @@ struct AccountListView: View {
self.accounts_by_type = Dictionary(grouping: accounts) { account in
account.type
}
self.initializeExpandedSections() // Initialize expanded states
}
}
}
Expand Down

0 comments on commit 9e46417

Please sign in to comment.