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 self.category = .empty
} }
@Published var recurringTransactions: AsyncData<[RecurringTransaction]> = .empty @Published var recurringTransactions: AsyncData<OrderedDictionary<String, [RecurringTransaction]>> = .empty
@Published var recurringTransaction: AsyncData<RecurringTransaction> = .empty { @Published var recurringTransaction: AsyncData<RecurringTransaction> = .empty {
didSet { didSet {
if case let .success(transaction) = self.recurringTransaction { if case let .success(transaction) = self.recurringTransaction {
@ -323,7 +323,18 @@ class DataStore : ObservableObject {
} }
do { do {
let transactions = try await self.apiService.getRecurringTransactions(budget.id) 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 { } catch {
self.recurringTransactions = .error(error) self.recurringTransactions = .error(error)
} }
@ -368,11 +379,7 @@ class DataStore : ObservableObject {
} else { } else {
self.recurringTransaction = .empty self.recurringTransaction = .empty
} }
if case var .success(transactions) = self.recurringTransactions { await self.getRecurringTransactions()
transactions = transactions.filter(withoutId: savedTransaction.id)
transactions.append(savedTransaction)
self.recurringTransactions = .success(transactions.sorted(by: { $0.title < $1.title }))
}
} catch { } catch {
self.recurringTransactions = .error(error) self.recurringTransactions = .error(error)
} }
@ -383,9 +390,7 @@ class DataStore : ObservableObject {
do { do {
try await self.apiService.deleteRecurringTransaction(transaction.id) try await self.apiService.deleteRecurringTransaction(transaction.id)
self.recurringTransaction = .empty self.recurringTransaction = .empty
if case let .success(transactions) = self.recurringTransactions { await self.getRecurringTransactions()
self.recurringTransactions = .success(transactions.filter(withoutId: transaction.id))
}
} catch { } catch {
self.recurringTransaction = .error(error, transaction) self.recurringTransaction = .error(error, transaction)
} }

View file

@ -8,6 +8,7 @@
import SwiftUI import SwiftUI
import TwigsCore import TwigsCore
import OrderedCollections
struct RecurringTransactionsListView: View { struct RecurringTransactionsListView: View {
@EnvironmentObject var dataStore: DataStore @EnvironmentObject var dataStore: DataStore
@ -17,10 +18,18 @@ struct RecurringTransactionsListView: View {
data: $dataStore.recurringTransactions, data: $dataStore.recurringTransactions,
action: { await self.dataStore.getRecurringTransactions() }, action: { await self.dataStore.getRecurringTransactions() },
errorTextLocalizedStringKey: "Failed to load recurring transactions" errorTextLocalizedStringKey: "Failed to load recurring transactions"
) { (transactions: [RecurringTransaction]) in ) { (transactions: OrderedDictionary<String, [RecurringTransaction]>) in
List { List {
ForEach(transactions) { transaction in ForEach(transactions.keys, id: \.self) { (key: String) in
RecurringTransactionsListItemView(transaction) Group {
if !transactions[key]!.isEmpty {
Section(header: Text(key)) {
ForEach(transactions[key]!) { transaction in
RecurringTransactionsListItemView(transaction)
}
}
}
}
} }
} }
.refreshable { .refreshable {