NTP-29551 | Add change for nae for cache coin home screen response (#14577)
This commit is contained in:
@@ -26,6 +26,7 @@ import com.navi.common.model.NotificationSettings
|
||||
import com.navi.common.model.NotificationSettingsRequest
|
||||
import com.navi.common.model.UploadDataAsyncResponse
|
||||
import com.navi.common.network.models.RepoResult
|
||||
import com.navi.common.network.models.isSuccessWithData
|
||||
import com.navi.common.network.retrofit.RetrofitService as CommonRetrofitService
|
||||
import com.navi.common.utils.Constants.GZIP
|
||||
import com.navi.rr.common.network.retrofit.ResponseHandler
|
||||
@@ -43,11 +44,19 @@ constructor(
|
||||
private val cacheHandlerProxy: CacheHandlerProxy,
|
||||
) {
|
||||
suspend fun fetchCoinHomeScreenUiTronConfigs(
|
||||
screenName: String,
|
||||
shouldRefresh: Boolean? = null,
|
||||
metricInfo: MetricInfo<RepoResult<ScreenDefinition>>,
|
||||
): Flow<RepoResult<ScreenDefinition>> {
|
||||
val cacheKeyForResponse = DBCacheConstants.COIN_HOME_SCREEN_CACHE_KEY
|
||||
val isCacheAvailable = cacheHandlerProxy.isCacheAvailable(key = cacheKeyForResponse)
|
||||
|
||||
val metricInfo =
|
||||
MetricInfo.CoinMetric<ScreenDefinition>(
|
||||
screen = screenName,
|
||||
isNae = { isCacheAvailable.not() && !it.isSuccessWithData() },
|
||||
)
|
||||
return cacheHandlerProxy.fetchData<ScreenDefinition>(
|
||||
key = DBCacheConstants.COIN_HOME_SCREEN_CACHE_KEY,
|
||||
key = cacheKeyForResponse,
|
||||
shouldRefresh = shouldRefresh,
|
||||
fetchFromAlternativeSource = {
|
||||
responseHandler.handleResponse(
|
||||
@@ -64,18 +73,31 @@ constructor(
|
||||
}
|
||||
|
||||
suspend fun fetchCoinHomeScreenV2(
|
||||
shouldRefresh: Boolean? = null
|
||||
screenName: String,
|
||||
shouldRefresh: Boolean? = null,
|
||||
): Flow<RepoResult<AlchemistScreenDefinition>> {
|
||||
|
||||
val cacheKeyForResponse = DBCacheConstants.COIN_HOME_SCREEN_V2_CACHE_KEY
|
||||
val isCacheAvailable = cacheHandlerProxy.isCacheAvailable(key = cacheKeyForResponse)
|
||||
|
||||
val metricInfo =
|
||||
MetricInfo.CoinMetric<AlchemistScreenDefinition>(
|
||||
screen = screenName,
|
||||
isNae = { isCacheAvailable.not() && !it.isSuccessWithData() },
|
||||
)
|
||||
|
||||
return cacheHandlerProxy.fetchData<AlchemistScreenDefinition>(
|
||||
key = DBCacheConstants.COIN_HOME_SCREEN_V2_CACHE_KEY,
|
||||
shouldRefresh = shouldRefresh,
|
||||
fetchFromAlternativeSource = {
|
||||
responseHandler.handleResponse(
|
||||
retrofitService.fetchAlchemistScreenUiTronConfigs(
|
||||
acceptEncoding = GZIP,
|
||||
target = ModuleNameV2.ALCHEMIST.name,
|
||||
screenId = COINS_SCREEN_SCREEN_V2_NAME,
|
||||
)
|
||||
metricInfo = metricInfo,
|
||||
response =
|
||||
retrofitService.fetchAlchemistScreenUiTronConfigs(
|
||||
acceptEncoding = GZIP,
|
||||
target = ModuleNameV2.ALCHEMIST.name,
|
||||
screenId = COINS_SCREEN_SCREEN_V2_NAME,
|
||||
),
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
@@ -290,16 +290,12 @@ constructor(
|
||||
/** This should ideally be in AP as a Lambda Action */
|
||||
fun fetchAndSaveCoinHomeScreen() {
|
||||
launch {
|
||||
coinHomeScreenRepo
|
||||
.fetchCoinHomeScreenUiTronConfigs(
|
||||
metricInfo = MetricInfo.CoinMetric(screen = screenName, isNae = { false })
|
||||
)
|
||||
.collect {
|
||||
val response = it.data
|
||||
if (it.isValidResponse()) {
|
||||
cacheCoinHomeImages(response?.screenStructure)
|
||||
}
|
||||
coinHomeScreenRepo.fetchCoinHomeScreenUiTronConfigs(screenName = screenName).collect {
|
||||
val response = it.data
|
||||
if (it.isValidResponse()) {
|
||||
cacheCoinHomeImages(response?.screenStructure)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@ import com.navi.coin.models.model.RedemptionStatusResponse
|
||||
import com.navi.coin.models.states.ApiActionState
|
||||
import com.navi.coin.models.states.CoinHomeScreenState
|
||||
import com.navi.coin.navigator.CoinNavigationActions
|
||||
import com.navi.coin.repo.repository.CoinHistoryScreenRepo
|
||||
import com.navi.coin.repo.repository.CoinHomeScreenRepo
|
||||
import com.navi.coin.utils.analytics.NaviCoinsAnalytics
|
||||
import com.navi.coin.utils.analytics.NaviCoinsAnalytics.Companion.DEV_REDEEM_COIN_FAILURE_EVENT
|
||||
@@ -84,10 +83,7 @@ import org.json.JSONObject
|
||||
@HiltViewModel
|
||||
open class CoinHomeViewModelV1
|
||||
@Inject
|
||||
constructor(
|
||||
private val coinHomeScreenRepo: CoinHomeScreenRepo,
|
||||
private val coinHistoryScreenRepo: CoinHistoryScreenRepo,
|
||||
) : CoinBaseVM() {
|
||||
constructor(private val coinHomeScreenRepo: CoinHomeScreenRepo) : CoinBaseVM() {
|
||||
private val eventPublisher: NaviCoinsAnalytics.BasicEvent =
|
||||
NaviCoinsAnalytics.naviCoinsAnalytics.BasicEvent()
|
||||
|
||||
@@ -150,16 +146,14 @@ constructor(
|
||||
|
||||
fun fetchCoinHomeScreenUiTronConfigs(shouldRefresh: Boolean? = null) {
|
||||
launch {
|
||||
val metricInfo =
|
||||
MetricInfo.CoinMetric<ScreenDefinition>(
|
||||
screen = screenName,
|
||||
isNae = { !it.isSuccessWithData() },
|
||||
)
|
||||
if (shouldRefresh.orFalse()) {
|
||||
_coinHomeScreenData.update { CoinHomeScreenState.Loading }
|
||||
}
|
||||
coinHomeScreenRepo
|
||||
.fetchCoinHomeScreenUiTronConfigs(shouldRefresh.orFalse(), metricInfo)
|
||||
.fetchCoinHomeScreenUiTronConfigs(
|
||||
screenName = screenName,
|
||||
shouldRefresh = shouldRefresh.orFalse(),
|
||||
)
|
||||
.collect { result ->
|
||||
val response = result.data
|
||||
when {
|
||||
|
||||
@@ -9,7 +9,6 @@ package com.navi.coin.vm
|
||||
|
||||
import com.navi.base.utils.orFalse
|
||||
import com.navi.coin.models.states.CoinHomeScreenV2State
|
||||
import com.navi.coin.repo.repository.CoinHistoryScreenRepo
|
||||
import com.navi.coin.repo.repository.CoinHomeScreenRepo
|
||||
import com.navi.common.alchemist.model.AlchemistBottomSheetStructure
|
||||
import com.navi.common.alchemist.model.AlchemistScreenDefinition
|
||||
@@ -27,12 +26,8 @@ import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
|
||||
@HiltViewModel
|
||||
class CoinHomeViewModelV2
|
||||
@Inject
|
||||
constructor(
|
||||
private val coinHomeScreenRepo: CoinHomeScreenRepo,
|
||||
coinHistoryScreenRepo: CoinHistoryScreenRepo,
|
||||
) : CoinHomeViewModelV1(coinHomeScreenRepo, coinHistoryScreenRepo) {
|
||||
class CoinHomeViewModelV2 @Inject constructor(private val coinHomeScreenRepo: CoinHomeScreenRepo) :
|
||||
CoinHomeViewModelV1(coinHomeScreenRepo) {
|
||||
|
||||
private val _screenData = MutableStateFlow<CoinHomeScreenV2State>(CoinHomeScreenV2State.Loading)
|
||||
val screenState = _screenData.asStateFlow()
|
||||
@@ -70,32 +65,38 @@ constructor(
|
||||
if (shouldRefresh.orFalse()) {
|
||||
_screenData.update { CoinHomeScreenV2State.Loading }
|
||||
}
|
||||
coinHomeScreenRepo.fetchCoinHomeScreenV2(shouldRefresh.orFalse()).collect { result ->
|
||||
val response = result.data
|
||||
when {
|
||||
result.isValidResponse() -> {
|
||||
if (result.isFromCache) {
|
||||
setCachePresent()
|
||||
coinHomeScreenRepo
|
||||
.fetchCoinHomeScreenV2(
|
||||
screenName = screenName,
|
||||
shouldRefresh = shouldRefresh.orFalse(),
|
||||
)
|
||||
.collect { result ->
|
||||
val response = result.data
|
||||
when {
|
||||
result.isValidResponse() -> {
|
||||
if (result.isFromCache) {
|
||||
setCachePresent()
|
||||
}
|
||||
_screenData.update {
|
||||
CoinHomeScreenV2State.Success(
|
||||
response?.copy() ?: AlchemistScreenDefinition()
|
||||
)
|
||||
}
|
||||
}
|
||||
_screenData.update {
|
||||
CoinHomeScreenV2State.Success(
|
||||
response?.copy() ?: AlchemistScreenDefinition()
|
||||
|
||||
else -> {
|
||||
showError(
|
||||
RRErrorData(
|
||||
statusCode = result.statusCode ?: ApiConstants.API_CODE_ERROR,
|
||||
methodName = ::fetchScreenConfig.name,
|
||||
error = result.error,
|
||||
errors = result.errors,
|
||||
screenName = COIN_HOME_SCREEN_V2,
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
showError(
|
||||
RRErrorData(
|
||||
statusCode = result.statusCode ?: ApiConstants.API_CODE_ERROR,
|
||||
methodName = ::fetchScreenConfig.name,
|
||||
error = result.error,
|
||||
errors = result.errors,
|
||||
screenName = COIN_HOME_SCREEN_V2,
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,4 +35,8 @@ class CacheHandlerProxy @Inject constructor(val cacheManager: CacheManager) {
|
||||
val type = object : TypeToken<T>() {}.type
|
||||
return flowOf(mockApiResponse(type, key.orEmpty()))
|
||||
}
|
||||
|
||||
suspend fun isCacheAvailable(key: String): Boolean {
|
||||
return cacheManager.isCacheAvailable(key)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,4 +80,8 @@ class CacheManager @Inject constructor(val cacheRepository: NaviCacheRepositoryI
|
||||
cacheMutex.withLock { save(naviCacheEntity = cacheEntity) }
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun isCacheAvailable(key: String): Boolean {
|
||||
return cacheRepository.get(key) != null
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user