Fix some nullability unwrapping in OverviewViewModel

This commit is contained in:
William Brawner 2021-02-15 21:44:30 -07:00
parent c7cdf2fe9c
commit 91f2ef3680

View file

@ -23,7 +23,8 @@ class OverviewViewModel : ViewModel() {
fun loadOverview(lifecycleOwner: LifecycleOwner) { fun loadOverview(lifecycleOwner: LifecycleOwner) {
budgetRepo.currentBudget.observe(lifecycleOwner, Observer { budget -> budgetRepo.currentBudget.observe(lifecycleOwner, Observer { budget ->
if (budget == null) { val budgetId = budget?.id
if (budgetId == null) {
state.postValue(AsyncState.Error("Invalid Budget ID")) state.postValue(AsyncState.Error("Invalid Budget ID"))
return@Observer return@Observer
} }
@ -35,8 +36,9 @@ class OverviewViewModel : ViewModel() {
var expectedIncome = 0L var expectedIncome = 0L
var actualExpenses = 0L var actualExpenses = 0L
var actualIncome = 0L var actualIncome = 0L
categoryRepo.findAll(arrayOf(budget.id!!)).forEach { category -> categoryRepo.findAll(arrayOf(budgetId)).forEach { category ->
val balance = categoryRepo.getBalance(category.id!!) val categoryId = category.id ?: return@forEach
val balance = categoryRepo.getBalance(categoryId)
if (category.expense) { if (category.expense) {
expectedExpenses += category.amount expectedExpenses += category.amount
actualExpenses += (balance * -1) actualExpenses += (balance * -1)
@ -47,7 +49,7 @@ class OverviewViewModel : ViewModel() {
} }
state.postValue(AsyncState.Success(OverviewState( state.postValue(AsyncState.Success(OverviewState(
budget, budget,
budgetRepo.getBalance(budget.id!!), budgetRepo.getBalance(budgetId),
expectedIncome, expectedIncome,
expectedExpenses, expectedExpenses,
actualIncome, actualIncome,