diff --git a/Twigs/DataStore.swift b/Twigs/DataStore.swift index 157569a..29c28b9 100644 --- a/Twigs/DataStore.swift +++ b/Twigs/DataStore.swift @@ -298,7 +298,7 @@ class DataStore : ObservableObject { self.category = .empty } - @Published var recurringTransactions: AsyncData<[RecurringTransaction]> = .empty + @Published var recurringTransactions: AsyncData> = .empty @Published var recurringTransaction: AsyncData = .empty { didSet { if case let .success(transaction) = self.recurringTransaction { @@ -323,7 +323,18 @@ class DataStore : ObservableObject { } do { let transactions = try await self.apiService.getRecurringTransactions(budget.id) - self.recurringTransactions = .success(transactions.sorted(by: { $0.title < $1.title })) + .sorted(by: { $0.title < $1.title }) + var recurringTransactions: OrderedDictionary = [:] + recurringTransactions["This month"] = transactions.filter { + $0.isThisMonth && !$0.isExpired + } + recurringTransactions["Future"] = transactions.filter { + !$0.isThisMonth && !$0.isExpired + } + recurringTransactions["Expired"] = transactions.filter { + $0.isExpired + } + self.recurringTransactions = .success(recurringTransactions) } catch { self.recurringTransactions = .error(error) } @@ -368,11 +379,7 @@ class DataStore : ObservableObject { } else { self.recurringTransaction = .empty } - if case var .success(transactions) = self.recurringTransactions { - transactions = transactions.filter(withoutId: savedTransaction.id) - transactions.append(savedTransaction) - self.recurringTransactions = .success(transactions.sorted(by: { $0.title < $1.title })) - } + await self.getRecurringTransactions() } catch { self.recurringTransactions = .error(error) } @@ -383,9 +390,7 @@ class DataStore : ObservableObject { do { try await self.apiService.deleteRecurringTransaction(transaction.id) self.recurringTransaction = .empty - if case let .success(transactions) = self.recurringTransactions { - self.recurringTransactions = .success(transactions.filter(withoutId: transaction.id)) - } + await self.getRecurringTransactions() } catch { self.recurringTransaction = .error(error, transaction) } diff --git a/Twigs/Recurring Transactions/RecurringTransactionsListView.swift b/Twigs/Recurring Transactions/RecurringTransactionsListView.swift index ddf9f5f..aebc915 100644 --- a/Twigs/Recurring Transactions/RecurringTransactionsListView.swift +++ b/Twigs/Recurring Transactions/RecurringTransactionsListView.swift @@ -8,6 +8,7 @@ import SwiftUI import TwigsCore +import OrderedCollections struct RecurringTransactionsListView: View { @EnvironmentObject var dataStore: DataStore @@ -17,10 +18,18 @@ struct RecurringTransactionsListView: View { data: $dataStore.recurringTransactions, action: { await self.dataStore.getRecurringTransactions() }, errorTextLocalizedStringKey: "Failed to load recurring transactions" - ) { (transactions: [RecurringTransaction]) in + ) { (transactions: OrderedDictionary) in List { - ForEach(transactions) { transaction in - RecurringTransactionsListItemView(transaction) + ForEach(transactions.keys, id: \.self) { (key: String) in + Group { + if !transactions[key]!.isEmpty { + Section(header: Text(key)) { + ForEach(transactions[key]!) { transaction in + RecurringTransactionsListItemView(transaction) + } + } + } + } } } .refreshable {