Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Balance After Planned Payments #2820

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package com.ivy.balance
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableDoubleStateOf
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.viewModelScope
import com.ivy.domain.ComposeViewModel
import com.ivy.legacy.data.model.TimePeriod
import com.ivy.legacy.utils.ioThread
import com.ivy.wallet.domain.action.settings.BaseCurrencyAct
import com.ivy.wallet.domain.action.wallet.CalcWalletBalanceAct
import com.ivy.wallet.domain.deprecated.logic.PlannedPaymentsLogic
import com.ivy.legacy.utils.ioThread
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand All @@ -28,6 +29,7 @@ class BalanceViewModel @Inject constructor(
private val currentBalance = mutableDoubleStateOf(0.0)
private val plannedPaymentsAmount = mutableDoubleStateOf(0.0)
private val balanceAfterPlannedPayments = mutableDoubleStateOf(0.0)
private val numberOfMonthsAhead = mutableIntStateOf(1)

@Composable
override fun uiState(): BalanceState {
Expand All @@ -52,7 +54,9 @@ class BalanceViewModel @Inject constructor(
}
}

private fun start(timePeriod: TimePeriod = ivyContext.selectedPeriod) {
private fun start(
timePeriod: TimePeriod = ivyContext.selectedPeriod
) {
viewModelScope.launch {
baseCurrencyCode.value = baseCurrencyAct(Unit)
period.value = timePeriod
Expand All @@ -64,32 +68,40 @@ class BalanceViewModel @Inject constructor(
plannedPaymentsAmount.doubleValue = ioThread {
plannedPaymentsLogic.plannedPaymentsAmountFor(
timePeriod.toRange(ivyContext.startDayOfMonth)
) // + positive if Income > Expenses else - negative
// + positive if Income > Expenses else - negative
) * if (numberOfMonthsAhead.intValue >= 0) {
numberOfMonthsAhead.intValue.toDouble()
} else {
1.0
}
}
balanceAfterPlannedPayments.doubleValue = currentBalance.doubleValue + plannedPaymentsAmount.doubleValue
balanceAfterPlannedPayments.doubleValue =
currentBalance.doubleValue + plannedPaymentsAmount.doubleValue
}
}

private fun setPeriod(timePeriod: com.ivy.legacy.data.model.TimePeriod) {
private fun setPeriod(timePeriod: TimePeriod) {
start(timePeriod = timePeriod)
}

private fun nextMonth() {
val month = period.value.month
val year = period.value.year ?: com.ivy.legacy.utils.dateNowUTC().year
numberOfMonthsAhead.intValue += 1
if (month != null) {
start(
timePeriod = month.incrementMonthPeriod(ivyContext, 1L, year = year),
timePeriod = month.incrementMonthPeriod(ivyContext, 1L, year = year)
)
}
}

private fun previousMonth() {
val month = period.value.month
val year = period.value.year ?: com.ivy.legacy.utils.dateNowUTC().year
numberOfMonthsAhead.intValue -= 1
if (month != null) {
start(
timePeriod = month.incrementMonthPeriod(ivyContext, -1L, year = year),
timePeriod = month.incrementMonthPeriod(ivyContext, -1L, year = year)
)
}
}
Expand Down