-
Notifications
You must be signed in to change notification settings - Fork 678
fix(feature/client): correct loan repayment navigation flow #2599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,34 +12,75 @@ package com.mifos.feature.loan.loanRepayment | |
| import androidclient.feature.loan.generated.resources.Res | ||
| import androidclient.feature.loan.generated.resources.feature_loan_failed_to_load_loan_repayment | ||
| import androidclient.feature.loan.generated.resources.feature_loan_payment_failed | ||
| import androidclient.feature.loan.generated.resources.feature_loan_unknown_error_occured | ||
| import androidx.lifecycle.SavedStateHandle | ||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.viewModelScope | ||
| import androidx.navigation.toRoute | ||
| import com.mifos.core.common.utils.CurrencyFormatter | ||
| import com.mifos.core.common.utils.DataState | ||
| import com.mifos.core.data.repository.LoanAccountSummaryRepository | ||
| import com.mifos.core.data.repository.LoanRepaymentRepository | ||
| import com.mifos.room.entities.accounts.loans.LoanRepaymentRequestEntity | ||
| import com.mifos.room.entities.accounts.loans.LoanWithAssociationsEntity | ||
| import com.mifos.room.entities.templates.loans.LoanRepaymentTemplateEntity | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import kotlinx.coroutines.launch | ||
| import kotlin.math.round | ||
|
|
||
| class LoanRepaymentViewModel( | ||
| savedStateHandle: SavedStateHandle, | ||
| private val repository: LoanRepaymentRepository, | ||
| private val summaryRepository: LoanAccountSummaryRepository, | ||
| ) : ViewModel() { | ||
|
|
||
| val arg = savedStateHandle.toRoute<LoanRepaymentScreenRoute>() | ||
| private val _arg = MutableStateFlow(savedStateHandle.toRoute<LoanRepaymentScreenRoute>()) | ||
| val arg: StateFlow<LoanRepaymentScreenRoute> = _arg.asStateFlow() | ||
|
|
||
| private val _loanRepaymentUiState = | ||
| MutableStateFlow<LoanRepaymentUiState>(LoanRepaymentUiState.ShowProgressbar) | ||
| val loanRepaymentUiState: StateFlow<LoanRepaymentUiState> get() = _loanRepaymentUiState | ||
|
|
||
| init { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Call checkDatabaseLoanRepaymentByLoanId() in this init block only. (and remove it from the loadLoanById function) it would look like this
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I observed that checkDatabaseLoanRepaymentByLoanId() can be called regardless of the fact whether the loan details are being loaded or we already have from the args as in both we are getting the loanId by args but I have not called the function in viewmodel and kept it like it was before my changes because calling it only in init block may display stale data until the screen is in backstack . Suppose , if we navigate to another screen and navigate back to this screen then launched effect will load the data again but the init block will not do so until the viewmodel is destroyed and created again. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "If we navigate to another screen and navigate back to this screen then launched effect will load the data again" Are you sure about this? because on the internet I see that LaunchedEffect runs only two times 1. Entering in the composition, 2. when the keys are updated. Navigating to other screen doesn't remove current screen from composition, current screen stays in the backstack. So navigating back will not add LaunchedEffect again in the composition and hence checkDatabaseLoanRepaymentByLoanId will not be called again I think. Because if that's not happening then we should add a button to load them
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sahilshivekar
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can check for loan account 053 in maria. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am also confused that why LaunchedEffect is again triggering if the composition is in the backstack.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sahilshivekar |
||
| if (_arg.value.loanAccountNumber.isEmpty()) { | ||
| loadLoanById() | ||
| } | ||
| } | ||
|
|
||
| fun loadLoanById() { | ||
| viewModelScope.launch { | ||
| summaryRepository.getLoanById(_arg.value.loanId).collect { dataState -> | ||
| when (dataState) { | ||
| is DataState.Loading -> { | ||
| _loanRepaymentUiState.value = LoanRepaymentUiState.ShowProgressbar | ||
| } | ||
|
|
||
| is DataState.Success -> { | ||
| val loanWithAssociations = dataState.data ?: LoanWithAssociationsEntity() | ||
| _arg.value = _arg.value.copy( | ||
| loanId = loanWithAssociations.id, | ||
| clientName = loanWithAssociations.clientName, | ||
| loanProductName = loanWithAssociations.loanProductName, | ||
Kartikey15dem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| amountInArrears = loanWithAssociations.summary.totalOverdue, | ||
| loanAccountNumber = loanWithAssociations.accountNo, | ||
| ) | ||
Kartikey15dem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| is DataState.Error -> { | ||
| _loanRepaymentUiState.value = LoanRepaymentUiState.ShowError( | ||
| Res.string.feature_loan_unknown_error_occured, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Kartikey15dem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| fun loanLoanRepaymentTemplate() { | ||
| viewModelScope.launch { | ||
| repository.getLoanRepayTemplate(arg.loanId).collect { state -> | ||
| repository.getLoanRepayTemplate(_arg.value.loanId).collect { state -> | ||
| when (state) { | ||
| is DataState.Error -> | ||
| _loanRepaymentUiState.value = | ||
|
|
@@ -64,7 +105,7 @@ class LoanRepaymentViewModel( | |
| _loanRepaymentUiState.value = LoanRepaymentUiState.ShowProgressbar | ||
|
|
||
| try { | ||
| val loanRepaymentResponse = repository.submitPayment(arg.loanId, request) | ||
| val loanRepaymentResponse = repository.submitPayment(_arg.value.loanId, request) | ||
| _loanRepaymentUiState.value = | ||
| LoanRepaymentUiState.ShowPaymentSubmittedSuccessfully( | ||
| loanRepaymentResponse, | ||
|
|
@@ -78,7 +119,7 @@ class LoanRepaymentViewModel( | |
|
|
||
| fun checkDatabaseLoanRepaymentByLoanId() { | ||
| viewModelScope.launch { | ||
| repository.getDatabaseLoanRepaymentByLoanId(arg.loanId).collect { state -> | ||
| repository.getDatabaseLoanRepaymentByLoanId(_arg.value.loanId).collect { state -> | ||
| when (state) { | ||
| is DataState.Error -> | ||
| _loanRepaymentUiState.value = | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.