diff --git a/android/navi-bbps/src/main/kotlin/com/navi/bbps/feature/prepaidrecharge/PrepaidRechargeViewModel.kt b/android/navi-bbps/src/main/kotlin/com/navi/bbps/feature/prepaidrecharge/PrepaidRechargeViewModel.kt index 38f1075f4e..16a898982d 100644 --- a/android/navi-bbps/src/main/kotlin/com/navi/bbps/feature/prepaidrecharge/PrepaidRechargeViewModel.kt +++ b/android/navi-bbps/src/main/kotlin/com/navi/bbps/feature/prepaidrecharge/PrepaidRechargeViewModel.kt @@ -70,6 +70,7 @@ import com.navi.bbps.network.di.NaviBbpsGsonBuilder import com.navi.bbps.parallelMap import com.navi.common.constants.DBCacheConstants import com.navi.common.di.CoroutineDispatcherProvider +import com.navi.common.extensions.safeSubstring import com.navi.common.firebaseremoteconfig.FirebaseRemoteConfigHelper import com.navi.common.model.common.NudgeDetailEntity import com.navi.common.network.models.isSuccessWithData @@ -90,6 +91,7 @@ import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.stateIn @@ -241,6 +243,7 @@ constructor( initConfig() updateRewardsNudgeEntity() fetchPrepaidRechargeScreenData() + observeQueriedRechargePlans() } private fun updatePlanItemLoadingState(isLoading: Boolean, selectedPlanItem: PlanItemEntity?) { @@ -693,6 +696,88 @@ constructor( initialValue = false, ) + private fun observeQueriedRechargePlans() { + combine(_searchQuery, _prepaidRechargeState) { searchQuery, state -> + if (state is PrepaidRechargeState.Loaded) { + val filteredPlanGroups = + allPlanGroups.parallelMap { group -> + group.copy( + plans = + group.plans.filter { plan -> + plan.price.contains(searchQuery, ignoreCase = true) || + plan.validity.contains( + searchQuery, + ignoreCase = true, + ) || + plan.packageDescription.contains( + searchQuery, + ignoreCase = true, + ) + } + ) + } + val filteredPlans = filteredPlanGroups.flatMap { it.plans } + val sortedPlans = + filteredPlans.sortedWith( + compareBy { plan -> + when { + plan.price.equals(searchQuery, ignoreCase = true) -> + 0 // Plans with price equal to search query come first + plan.price.contains(searchQuery, ignoreCase = false) -> 1 + plan.validity + .safeSubstring(0, 2) + .equals(searchQuery, ignoreCase = false) -> 2 + plan.validity + .safeSubstring(0, 2) + .contains(searchQuery, ignoreCase = false) -> 3 + plan.packageDescription.contains( + searchQuery, + ignoreCase = false, + ) -> 4 + else -> 5 // All other plans + } + } + ) + val sortedPlansAsListOfPlanGroupEntity = + PlanGroupEntity( + planType = "", + plans = + sortedPlans.parallelMap { plan -> + PlanItemEntity( + planName = plan.planName, + price = plan.price, + validity = plan.validity, + talkTime = plan.talkTime, + packageDescription = plan.packageDescription, + ) + }, + ) + + val planGroups = + if (_searchQuery.value.isEmpty()) { + filteredPlanGroups + } else { + listOf(sortedPlansAsListOfPlanGroupEntity) + } + val filteredNetRechargePlanState = + state.copy( + operatorCode = state.operatorCode, + circleId = state.circleId, + planGroups = planGroups, + ) + updatePrepaidRechargeState(state = filteredNetRechargePlanState) + } else { + state + } + } + .flowOn(dispatcherProvider.default) + .stateIn( + scope = viewModelScope, + started = SharingStarted.Eagerly, + initialValue = _prepaidRechargeState.value, + ) + } + fun updateBottomSheetUIState( bottomSheetStateChange: Boolean? = null, bottomSheetUIState: PrepaidRechargeScreenBottomSheetUIState? = null,