Skip to content

Commit

Permalink
LoanViewModel : using by delegation for mutable states (#3523)
Browse files Browse the repository at this point in the history
  • Loading branch information
shamim-emon committed Sep 16, 2024
1 parent 6d97960 commit 4e725bb
Showing 1 changed file with 41 additions and 39 deletions.
80 changes: 41 additions & 39 deletions screen/loans/src/main/java/com/ivy/loans/loan/LoanViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,16 +60,16 @@ class LoanViewModel @Inject constructor(
private val dateTimePicker: DateTimePicker,
) : ComposeViewModel<LoanScreenState, LoanScreenEvent>() {

private val baseCurrencyCode = mutableStateOf(getDefaultFIATCurrency().currencyCode)
private val loans = mutableStateOf<ImmutableList<DisplayLoan>>(persistentListOf())
private val accounts = mutableStateOf<ImmutableList<Account>>(persistentListOf())
private val selectedAccount = mutableStateOf<Account?>(null)
private val loanModalData = mutableStateOf<LoanModalData?>(null)
private val reorderModalVisible = mutableStateOf(false)
private var dateTime = mutableStateOf<Instant>(timeProvider.utcNow())
private var baseCurrencyCode by mutableStateOf(getDefaultFIATCurrency().currencyCode)
private var loans by mutableStateOf<ImmutableList<DisplayLoan>>(persistentListOf())
private var accounts by mutableStateOf<ImmutableList<Account>>(persistentListOf())
private var selectedAccount by mutableStateOf<Account?>(null)
private var loanModalData by mutableStateOf<LoanModalData?>(null)
private var reorderModalVisible by mutableStateOf(false)
private var dateTime by mutableStateOf<Instant>(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<DisplayLoan> = persistentListOf()
Expand All @@ -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<DisplayLoan> {
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) {
Expand All @@ -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 -> {
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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()

Expand All @@ -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()

Expand All @@ -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
}
}

Expand Down Expand Up @@ -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()
}
Expand All @@ -335,7 +337,7 @@ class LoanViewModel @Inject constructor(
TestIdlingResource.increment()

accountCreator.createAccount(data) {
accounts.value = accountsAct(Unit)
accounts = accountsAct(Unit)
}

TestIdlingResource.decrement()
Expand Down Expand Up @@ -383,7 +385,7 @@ class LoanViewModel @Inject constructor(
}

private fun updatePaidOffLoanVisibility() {
paidOffLoanVisibility.value = paidOffLoanVisibility.value.not()
paidOffLoanVisibility = paidOffLoanVisibility.not()
filterLoans()
}
}

0 comments on commit 4e725bb

Please sign in to comment.