TP-62634 | Selective-refresh (#10718)
Co-authored-by: Hitesh <hitesh.kumar@navi.com> Co-authored-by: namankhurmi <naman.khurmi@navi.com>
This commit is contained in:
14
android/navi-base/src/main/java/com/navi/base/cache/model/NaviCacheEntityDetails.kt
vendored
Normal file
14
android/navi-base/src/main/java/com/navi/base/cache/model/NaviCacheEntityDetails.kt
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
*
|
||||
* * Copyright © 2024 by Navi Technologies Limited
|
||||
* * All rights reserved. Strictly confidential
|
||||
*
|
||||
*/
|
||||
|
||||
package com.navi.base.cache.model
|
||||
|
||||
data class NaviCacheEntityDetails(
|
||||
val data: NaviCacheEntity? = null,
|
||||
val isComingFromAltSource: Boolean? = null,
|
||||
val isCurrentAndAltDataSame: Boolean? = null
|
||||
)
|
||||
@@ -10,6 +10,7 @@ package com.navi.base.cache.repository
|
||||
import com.navi.base.cache.dao.NaviCacheDao
|
||||
import com.navi.base.cache.model.NaviCacheAltSourceEntity
|
||||
import com.navi.base.cache.model.NaviCacheEntity
|
||||
import com.navi.base.cache.model.NaviCacheEntityDetails
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
@@ -45,6 +46,22 @@ interface NaviCacheRepository {
|
||||
},
|
||||
emitMultipleValues: Boolean = false
|
||||
): Flow<NaviCacheEntity?>
|
||||
|
||||
fun getDataAndFetchFromAltSourceWithDetails(
|
||||
key: String,
|
||||
version: Long? = null,
|
||||
getDataFromAltSource: suspend () -> NaviCacheAltSourceEntity,
|
||||
isCurrentAndAltDataSame:
|
||||
(currentData: NaviCacheEntity?, altData: NaviCacheAltSourceEntity) -> Boolean =
|
||||
fun(currentData, altData): Boolean {
|
||||
val currentDataValue = currentData?.value ?: return false
|
||||
val altDataValue = altData.value ?: return false
|
||||
|
||||
return currentData.version == altData.version &&
|
||||
currentDataValue.hashCode() == altDataValue.hashCode()
|
||||
},
|
||||
emitMultipleValues: Boolean = false
|
||||
): Flow<NaviCacheEntityDetails?>
|
||||
}
|
||||
|
||||
class NaviCacheRepositoryImpl @Inject constructor(private val naviCacheDao: NaviCacheDao) :
|
||||
@@ -165,6 +182,62 @@ class NaviCacheRepositoryImpl @Inject constructor(private val naviCacheDao: Navi
|
||||
}
|
||||
}
|
||||
|
||||
override fun getDataAndFetchFromAltSourceWithDetails(
|
||||
key: String,
|
||||
version: Long?,
|
||||
getDataFromAltSource: suspend () -> NaviCacheAltSourceEntity,
|
||||
isCurrentAndAltDataSame:
|
||||
(currentData: NaviCacheEntity?, altData: NaviCacheAltSourceEntity) -> Boolean,
|
||||
emitMultipleValues: Boolean
|
||||
) = flow {
|
||||
var isValueEmitted = false
|
||||
|
||||
val currentValueInDB = get(key = key)
|
||||
|
||||
if (currentValueInDB != null) { // Its a valid value
|
||||
emit(NaviCacheEntityDetails(data = currentValueInDB, isComingFromAltSource = false))
|
||||
isValueEmitted = true
|
||||
}
|
||||
|
||||
val naviCacheValueEntityFromAltSource = getDataFromAltSource.invoke()
|
||||
|
||||
if (
|
||||
!naviCacheValueEntityFromAltSource.isSuccess ||
|
||||
naviCacheValueEntityFromAltSource.value == null
|
||||
) { // alternate source data invalid
|
||||
return@flow
|
||||
}
|
||||
|
||||
val naviCacheEntity =
|
||||
NaviCacheEntity(
|
||||
key = key,
|
||||
value = naviCacheValueEntityFromAltSource.value,
|
||||
version = naviCacheValueEntityFromAltSource.version ?: 1,
|
||||
ttl = naviCacheValueEntityFromAltSource.ttl,
|
||||
clearOnLogout = naviCacheValueEntityFromAltSource.clearOnLogout,
|
||||
metaData = naviCacheValueEntityFromAltSource.metaData
|
||||
)
|
||||
|
||||
val isDataSame =
|
||||
isCurrentAndAltDataSame(currentValueInDB, naviCacheValueEntityFromAltSource)
|
||||
|
||||
if (isDataSame.not()) {
|
||||
// Data from Alt source is different
|
||||
save(naviCacheEntity = naviCacheEntity)
|
||||
}
|
||||
|
||||
// If multiple values are required or no value is emitted yet then emit latest value
|
||||
if (emitMultipleValues || !isValueEmitted) {
|
||||
emit(
|
||||
NaviCacheEntityDetails(
|
||||
data = naviCacheEntity,
|
||||
isComingFromAltSource = true,
|
||||
isCurrentAndAltDataSame = isDataSame
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun checkIfDBValueIsValidElseRemoveEntry(
|
||||
naviCacheEntity: NaviCacheEntity?,
|
||||
version: Long?,
|
||||
|
||||
Reference in New Issue
Block a user