Split recurring transactions into current, future, and expired groups

This commit is contained in:
William Brawner 2022-09-09 18:14:03 -06:00
parent c4555fe804
commit 6896b7d9e6
2 changed files with 27 additions and 13 deletions

View file

@ -298,7 +298,7 @@ class DataStore : ObservableObject {
self.category = .empty
}
@Published var recurringTransactions: AsyncData<[RecurringTransaction]> = .empty
@Published var recurringTransactions: AsyncData<OrderedDictionary<String, [RecurringTransaction]>> = .empty
@Published var recurringTransaction: AsyncData<RecurringTransaction> = .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<String, [RecurringTransaction]> = [:]
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)
}

View file

@ -8,6 +8,7 @@
import SwiftUI
import TwigsCore
import OrderedCollections
struct RecurringTransactionsListView: View {
@EnvironmentObject var dataStore: DataStore
@ -17,12 +18,20 @@ struct RecurringTransactionsListView: View {
data: $dataStore.recurringTransactions,
action: { await self.dataStore.getRecurringTransactions() },
errorTextLocalizedStringKey: "Failed to load recurring transactions"
) { (transactions: [RecurringTransaction]) in
) { (transactions: OrderedDictionary<String, [RecurringTransaction]>) in
List {
ForEach(transactions) { transaction in
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 {
await dataStore.getRecurringTransactions(showLoader: false)
}