diff --git a/android/navi-bbps/src/main/kotlin/com/navi/bbps/feature/billhistorydetail/BillHistoryDetailsViewModel.kt b/android/navi-bbps/src/main/kotlin/com/navi/bbps/feature/billhistorydetail/BillHistoryDetailsViewModel.kt index 70a2156070..7d45772526 100644 --- a/android/navi-bbps/src/main/kotlin/com/navi/bbps/feature/billhistorydetail/BillHistoryDetailsViewModel.kt +++ b/android/navi-bbps/src/main/kotlin/com/navi/bbps/feature/billhistorydetail/BillHistoryDetailsViewModel.kt @@ -49,11 +49,13 @@ import com.navi.bbps.feature.mybills.MyBillsSyncJob import com.navi.bbps.feature.mybills.model.view.MyBillEntity import com.navi.bbps.feature.paybill.model.view.PayBillSource import com.navi.common.di.CoroutineDispatcherProvider +import com.navi.common.network.models.RepoResult import com.navi.common.network.models.isSuccessWithData import com.ramcosta.composedestinations.spec.Direction import dagger.hilt.android.lifecycle.HiltViewModel import javax.inject.Inject import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharingStarted @@ -86,6 +88,8 @@ constructor( private val myBillEntity = savedStateHandle.get("myBillEntity")!! + private var billResponse: RepoResult? = null + private val isRootScreen = false private val _billHistoryDetailsState = @@ -100,6 +104,9 @@ constructor( private val _isFetchingNewBill = MutableStateFlow(false) + private val _isLoading = MutableStateFlow(true) + val isLoading = _isLoading.asStateFlow() + val isUserActionBlocked = combine(_isDeleteBillInProgress, _isFetchingNewBill) { isDeleteBillInProgress, @@ -123,7 +130,10 @@ constructor( stringResouceProvider.getString(R.string.bbps_new_contact) init { - fetchBillHistoryList() + viewModelScope.launch(dispatcherProvider.io) { + async { fetchBillHistoryList() } + async { billResponse = fetchBillDetailsResponse() } + } } fun onTransactionItemClicked(billTransactionItemEntity: BillTransactionItemEntity) { @@ -385,24 +395,72 @@ constructor( paymentAmountExactness = myBillEntity.paymentAmountExactness ) - val billDetailsRequest = - BillDetailsRequest( - billerId = billerDetails.billerId, - customerParams = myBillEntity.customerParams, - deviceDetails = DeviceDetails(ip = naviNetworkConnectivity.getIpAddress()) - ) - val billDetailsResponse = - bbpsCommonRepository.fetchBillDetails(billDetailsRequest = billDetailsRequest) - - updateNewBillFetchingState(isInProgress = false) - - if (billDetailsResponse.isSuccessWithData()) { - val billDetails = billDetailsResponse.data!! - navigateToNextScreen(billDetails = billDetails, billerDetails = billerDetails) + if (billResponse?.isSuccessWithData() == true) { + goToPayBillScreen(billerDetails) } else { - notifyError(billDetailsResponse) + if (billResponse != null) { + displayBillFetchError() + } else { + fetchBillDetailsAndRedirect(billerDetails) + } } } } } + + private suspend fun fetchBillDetailsResponse(): RepoResult { + val billerDetails = + BillerDetailsEntity( + billerId = myBillEntity.billerId, + billerName = myBillEntity.billerName, + billerLogoUrl = myBillEntity.billerLogoUrl, + status = myBillEntity.status, + isAdhoc = myBillEntity.isAdhoc, + fetchOption = myBillEntity.fetchOption, + paymentAmountExactness = myBillEntity.paymentAmountExactness + ) + + val billDetailsRequest = + BillDetailsRequest( + billerId = billerDetails.billerId, + customerParams = myBillEntity.customerParams, + deviceDetails = DeviceDetails(ip = naviNetworkConnectivity.getIpAddress()) + ) + return bbpsCommonRepository.fetchBillDetails(billDetailsRequest = billDetailsRequest) + } + + private fun goToPayBillScreen(billerDetails: BillerDetailsEntity) { + viewModelScope.launch(dispatcherProvider.io) { + updateIsLoadingState(isLoading = false) + val billDetails = billResponse!!.data!! + updateNewBillFetchingState(isInProgress = false) + navigateToNextScreen(billDetails = billDetails, billerDetails = billerDetails) + } + } + + private fun displayBillFetchError() { + viewModelScope.launch(dispatcherProvider.io) { + updateIsLoadingState(isLoading = false) + updateNewBillFetchingState(isInProgress = false) + notifyError(billResponse!!) + billResponse = null + } + } + + private fun fetchBillDetailsAndRedirect(billerDetails: BillerDetailsEntity) { + viewModelScope.launch(dispatcherProvider.io) { + if (isLoading.value) { + billResponse = fetchBillDetailsResponse() + } + if (billResponse?.isSuccessWithData() == true) { + goToPayBillScreen(billerDetails) + } else { + displayBillFetchError() + } + } + } + + private fun updateIsLoadingState(isLoading: Boolean) { + _isLoading.update { isLoading } + } }