From ddf0817e4fa51bf995e3be517f9ef793f470144a Mon Sep 17 00:00:00 2001 From: Kishan Kumar Date: Thu, 28 Nov 2024 14:35:54 +0530 Subject: [PATCH] NTP-16109-caching-mechanism-for-scratch-card-config (#13850) --- .../helper/ScratchCardNudgeHelperImpl.kt | 36 +++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/android/navi-rr/src/main/java/com/navi/rr/scratchcard/helper/ScratchCardNudgeHelperImpl.kt b/android/navi-rr/src/main/java/com/navi/rr/scratchcard/helper/ScratchCardNudgeHelperImpl.kt index 0668c96d3f..7bbba07c3f 100644 --- a/android/navi-rr/src/main/java/com/navi/rr/scratchcard/helper/ScratchCardNudgeHelperImpl.kt +++ b/android/navi-rr/src/main/java/com/navi/rr/scratchcard/helper/ScratchCardNudgeHelperImpl.kt @@ -19,6 +19,7 @@ import com.navi.common.firebaseremoteconfig.FirebaseRemoteConfigHelper.UPI_SCRAT import com.navi.common.forge.model.ScreenDefinition import com.navi.common.model.RequestConfig import com.navi.common.network.models.RepoResult +import com.navi.common.network.models.isSuccessWithData import com.navi.common.utils.NaviApiPoller import com.navi.common.utils.isValidResponse import com.navi.common.utils.log @@ -42,6 +43,8 @@ import com.navi.rr.utils.injector.FieldInjector import java.util.concurrent.atomic.AtomicBoolean import javax.inject.Inject import kotlin.time.Duration.Companion.seconds +import kotlin.time.DurationUnit +import kotlin.time.toDuration import kotlinx.coroutines.Deferred import kotlinx.coroutines.async import kotlinx.coroutines.cancel @@ -165,9 +168,31 @@ constructor( return callbackFlow { val userId = PreferenceManager.getStringPreference(CommonPrefConstants.USER_ID) val forgeResponse = async { - scratchCardRepo.fetchScratchCardTemplate( - metricInfo = MetricInfo.RewardMetric(screen = screenName, isNae = { false }) - ) + val currentValueInDb = naviCacheRepository.get(SCRATCH_CARD_UI_CONFIG)?.value + if (currentValueInDb != null) { + val dbResponse = gson.fromJson(currentValueInDb, ScreenDefinition::class.java) + RepoResult(dbResponse) + } else { + val networkResponse = + scratchCardRepo.fetchScratchCardTemplate( + metricInfo = + MetricInfo.RewardMetric(screen = screenName, isNae = { false }) + ) + if (networkResponse.isSuccessWithData()) { + naviCacheRepository.save( + naviCacheEntity = + NaviCacheEntity( + key = SCRATCH_CARD_UI_CONFIG, + value = gson.toJson(networkResponse.data), + version = 1, + ttl = 1.toDuration(DurationUnit.DAYS).inWholeMilliseconds + ) + ) + } else { + send(GratificationStatus.Timeout()) + } + networkResponse + } } eventTracker.sendEvent(SCRATCH_CARD_GET_GRATIFICATION_CALLED) val responseProcessed = AtomicBoolean(false) @@ -365,11 +390,8 @@ constructor( companion object { private const val SCRATCH_CARD_OVERLAY_SCREEN_NAME = "SCRATCH_CARD_OVERLAY_SCREEN_NAME" - private const val CURRENT_BALANCE = "currentBalance" - private const val PREVIOUS_BALANCE = "previousBalance" private const val TRANSACTION_ID = "transaction_id" private const val SCREEN_NAME = "screen_name" - private const val PAYMENT_SUMMARY_SCREEN = "PAYMENT_SUMMARY_SCREEN" private const val REWARD_EARNED = "reward_earned" private const val Y = "Y" private const val N = "N" @@ -396,5 +418,7 @@ constructor( private const val DEV_REWARDS_ON_UPI_POLLING_EXCEPTION = "dev_reward_upi_polling_exception" private const val DEV_REWARDS_ON_UPI_COLLECT_EXCEPTION = "dev_reward_upi_on_collect_exception" + + private const val SCRATCH_CARD_UI_CONFIG = "scratchCardUiConfig" } }