Thank you everyone in advance. I'm new to SwiftUI. I am trying to find transactions total for dates filtered with CoreData fetch sectioned by month showing month by name sales total as section header for that month when grouping transactions by month.
Transaction entity has name, date and amount attributes. How can calculate sum for each month inside groupTransactionsByMonth function for header label of section in ViewModel.
here is my codes for it
typealias TransactionGroup = OrderedDictionary<String,
[Transaction]>
class: ViewModel: NSObject, ObservableObject {
@Published var transactions = [Transaction]()
func groupTransactionsByMonth()->TransactionGroup{
guard !transactions.isEmpty else {return[]}
let groupedTransactions = Transaction(grouping: transactions, by: {"\ ($0.date?.formatted(.dateTime.year().month(.wide)) ?? "")"})
return groupedTransactions
}
}
I tried this inside of groupTransactionsByMonth function:
for(_, value) in groupedTransactions{
var total: Double = 0
for transaction in value {
total += transaction.amount
return total
}
}
But it gives an error
Cannot convert return expression 0f type "Double" to return type 'TransactionGroup (aka 'OrderedDictionary<String, Array>')
@Edit1
at moment I am showing my transactions in forEach as following
@ObservedObject private var vm = ViewModel()
ForEach(Array(vm.groupTransactionsByMonth()), id: \.key){month,
transactions in
Section{
ForEach(transactions){transaction in
ReportCellView(date: transaction.date ?? Date(), name:
transaction.name ?? "", amount: transaction.amount)
}
} header {
HStack { Text(month)
Spacer()
//here I wanna show monthly total amount for
posted month in section header not achieved yet
//Text(" Sales")
//Text("\(monthlyTotalSalesAmount)")
}
Example:
I hope you don't have thousands of Transactions cause this isn't the most efficient way to do this.