我有一个ViewModel
显示transactions
和transactionSummary
LiveData的句柄
// Transactions
val transactions: LiveData<List<Transaction>> = getTransactionByDateUseCase
.getTransactionsByDate(dateRange.startDate, dateRange.endDate)
// Transaction Summary
private var _transactionsSummary = MutableLiveData<TransactionSummary>()
val transactionsSummary: LiveData<TransactionSummary> = _transactionsSummary
transactions
来自Room的返回LiveData<T>
类似,transactionSummary
但有一个转换过程来获取事务的摘要,如income
和expense
。
下面是改造过程:
private fun getTransactionsSummary(dateRange: DateRange): LiveData<TransactionSummary> {
return Transformations.map(
getTransactionByDateUseCase.getTransactionsByDate(dateRange.startDate, dateRange.endDate)
) { data ->
getTransactionSummaryUseCase.transactions = data
val summary = getTransactionSummaryUseCase.getSummaries(dateRange)
summary
}
}
但是,当我从 Fragment 请求更新时,transactionSummary
总是返回NULL
getFetchTransactionViewModel().updateTransactionSummary(selectedDateRange)
这是我里面的更新功能ViewModel
:
fun updateTransactionSummary(dateRange: DateRange) {
_transactionsSummary.value = getTransactionsSummary(dateRange).value
}
我观察到transactionSummary
这样的:
getFetchTransactionViewModel().transactionsSummary.observe(this, Observer {
textViewIncome.text = it.totalIncome.formatCurrency(isVisibleCurrency = true)
textViewExpense.text = it.totalExpense.formatCurrency(isVisibleCurrency = true)
textViewBalance.text = (it.totalIncome - it.totalExpense).formatCurrency(isVisibleCurrency = true)
})
是否有任何遗漏的步骤或不正确的实现?
谢谢