diff --git a/screen/loans/src/main/java/com/ivy/loans/loan/LoanViewModel.kt b/screen/loans/src/main/java/com/ivy/loans/loan/LoanViewModel.kt index e584b940d1..9a8c8eaef9 100644 --- a/screen/loans/src/main/java/com/ivy/loans/loan/LoanViewModel.kt +++ b/screen/loans/src/main/java/com/ivy/loans/loan/LoanViewModel.kt @@ -3,7 +3,9 @@ package com.ivy.loans.loan import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.Stable +import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue import androidx.lifecycle.viewModelScope import com.ivy.base.legacy.SharedPrefs import com.ivy.base.model.processByType @@ -58,16 +60,16 @@ class LoanViewModel @Inject constructor( private val dateTimePicker: DateTimePicker, ) : ComposeViewModel() { - private val baseCurrencyCode = mutableStateOf(getDefaultFIATCurrency().currencyCode) - private val loans = mutableStateOf>(persistentListOf()) - private val accounts = mutableStateOf>(persistentListOf()) - private val selectedAccount = mutableStateOf(null) - private val loanModalData = mutableStateOf(null) - private val reorderModalVisible = mutableStateOf(false) - private var dateTime = mutableStateOf(timeProvider.utcNow()) + private var baseCurrencyCode by mutableStateOf(getDefaultFIATCurrency().currencyCode) + private var loans by mutableStateOf>(persistentListOf()) + private var accounts by mutableStateOf>(persistentListOf()) + private var selectedAccount by mutableStateOf(null) + private var loanModalData by mutableStateOf(null) + private var reorderModalVisible by mutableStateOf(false) + private var dateTime by mutableStateOf(timeProvider.utcNow()) /** If true paid off loans will be visible */ - private val paidOffLoanVisibility = mutableStateOf(true) + private var paidOffLoanVisibility by mutableStateOf(true) /** Contains all loans including both paidOff and pending*/ private var allLoans: ImmutableList = persistentListOf() @@ -91,34 +93,34 @@ class LoanViewModel @Inject constructor( totalOweAmount = getTotalOweAmount(totalOweAmount, defaultCurrencyCode), totalOwedAmount = getTotalOwedAmount(totalOwedAmount, defaultCurrencyCode), paidOffLoanVisibility = getPaidOffLoanVisibility(), - dateTime = dateTime.value + dateTime = dateTime ) } @Composable - private fun getReorderModalVisible() = reorderModalVisible.value + private fun getReorderModalVisible() = reorderModalVisible @Composable - private fun getLoanModalData() = loanModalData.value + private fun getLoanModalData() = loanModalData @Composable private fun getLoans(): ImmutableList { - return loans.value + return loans } @Composable private fun getBaseCurrencyCode(): String { - return baseCurrencyCode.value + return baseCurrencyCode } @Composable - private fun getSelectedAccount() = selectedAccount.value + private fun getSelectedAccount() = selectedAccount @Composable - private fun getAccounts() = accounts.value + private fun getAccounts() = accounts @Composable - private fun getPaidOffLoanVisibility(): Boolean = paidOffLoanVisibility.value + private fun getPaidOffLoanVisibility(): Boolean = paidOffLoanVisibility override fun onEvent(event: LoanScreenEvent) { when (event) { @@ -127,20 +129,20 @@ class LoanViewModel @Inject constructor( } is LoanScreenEvent.OnAddLoan -> { - loanModalData.value = LoanModalData( + loanModalData = LoanModalData( loan = null, - baseCurrency = baseCurrencyCode.value, - selectedAccount = selectedAccount.value + baseCurrency = baseCurrencyCode, + selectedAccount = selectedAccount ) } is LoanScreenEvent.OnLoanModalDismiss -> { - loanModalData.value = null - dateTime.value = timeProvider.utcNow() + loanModalData = null + dateTime = timeProvider.utcNow() } is LoanScreenEvent.OnReOrderModalShow -> { - reorderModalVisible.value = event.show + reorderModalVisible = event.show } is LoanScreenEvent.OnReordered -> { @@ -168,12 +170,12 @@ class LoanViewModel @Inject constructor( viewModelScope.launch(Dispatchers.Default) { TestIdlingResource.increment() - dateTime.value = timeProvider.utcNow() + dateTime = timeProvider.utcNow() defaultCurrencyCode = ioThread { settingsDao.findFirst().currency }.also { - baseCurrencyCode.value = it + baseCurrencyCode = it } initialiseAccounts() @@ -190,7 +192,7 @@ class LoanViewModel @Inject constructor( } else { 0.0 } - var currCode = findCurrencyCode(accounts.value, loan.accountId) + var currCode = findCurrencyCode(accounts, loan.accountId) when (loan.type) { LoanType.BORROW -> totalOweAmount += (loanTotalAmount - amountPaid) @@ -239,20 +241,20 @@ class LoanViewModel @Inject constructor( private suspend fun initialiseAccounts() { val accountsList = accountsAct(Unit) - accounts.value = accountsList - selectedAccount.value = defaultAccountId(accountsList) - selectedAccount.value?.let { - baseCurrencyCode.value = it.currency ?: defaultCurrencyCode + accounts = accountsList + selectedAccount = defaultAccountId(accountsList) + selectedAccount?.let { + baseCurrencyCode = it.currency ?: defaultCurrencyCode } } private fun handleChangeDate() { dateTimePicker.pickDate( - initialDate = loanModalData.value?.loan?.dateTime?.let { + initialDate = loanModalData?.loan?.dateTime?.let { with(timeConverter) { it.toUTC() } } ?: timeProvider.utcNow() ) { localDate -> - val localTime = loanModalData.value?.loan?.dateTime?.let { + val localTime = loanModalData?.loan?.dateTime?.let { with(timeConverter) { it.toLocalTime() } } ?: timeProvider.localTimeNow() @@ -262,11 +264,11 @@ class LoanViewModel @Inject constructor( private fun handleChangeTime() { dateTimePicker.pickTime( - initialTime = loanModalData.value?.loan?.dateTime?.let { + initialTime = loanModalData?.loan?.dateTime?.let { with(timeConverter) { it.toLocalTime() } } ?: timeProvider.localTimeNow() ) { localTime -> - val localDate = loanModalData.value?.loan?.dateTime?.let { + val localDate = loanModalData?.loan?.dateTime?.let { with(timeConverter) { it.toLocalDate() } } ?: timeProvider.localDateNow() @@ -276,13 +278,13 @@ class LoanViewModel @Inject constructor( private fun updateDateTime(newDateTime: LocalDateTime) { val newDateTimeUtc = with(timeConverter) { newDateTime.toUTC() } - loanModalData.value?.let { currentData -> - loanModalData.value = currentData.copy( + loanModalData?.let { currentData -> + loanModalData = currentData.copy( loan = currentData.loan?.copy( dateTime = newDateTime ) ) - dateTime.value = newDateTimeUtc + dateTime = newDateTimeUtc } } @@ -324,7 +326,7 @@ class LoanViewModel @Inject constructor( /** It filters [allLoans] and updates [loans] based on weather to show paid off loans or not */ private fun filterLoans() { - loans.value = when (paidOffLoanVisibility.value) { + loans = when (paidOffLoanVisibility) { true -> allLoans false -> allLoans.filter { loan -> loan.percentPaid < 1.0 }.toImmutableList() } @@ -335,7 +337,7 @@ class LoanViewModel @Inject constructor( TestIdlingResource.increment() accountCreator.createAccount(data) { - accounts.value = accountsAct(Unit) + accounts = accountsAct(Unit) } TestIdlingResource.decrement() @@ -383,7 +385,7 @@ class LoanViewModel @Inject constructor( } private fun updatePaidOffLoanVisibility() { - paidOffLoanVisibility.value = paidOffLoanVisibility.value.not() + paidOffLoanVisibility = paidOffLoanVisibility.not() filterLoans() } }