Skip to content

Commit

Permalink
Merge pull request #150 from /issues/145
Browse files Browse the repository at this point in the history
Issue #145
  • Loading branch information
vazarkevych authored Nov 20, 2024
2 parents 5638cb3 + ce3a500 commit 0a1806e
Showing 1 changed file with 53 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,43 +45,27 @@ internal class FeaturesViewModel(
fun fetchFeatures(remoteEval: Boolean = false, payload: GBRemoteEvalParams? = null) {
try {
// Check for cache data
val dataModel = manager.getLayer().getData(
Constants.FEATURE_CACHE,
FeaturesDataModel.serializer()
)

val dataModel = getDataFromCache()
if (dataModel != null) {
// Call Success Delegate with mention of data available but its not remote
dataModel.features?.let {
this.delegate.featuresFetchedSuccessfully(
features = it,
isRemote = false
)
}
dataModel.encryptedFeatures?.let { encryptedFeatures: String ->
encryptionKey?.let { encryptionKey ->
val features = getFeaturesFromEncryptedFeatures(
encryptedString = encryptedFeatures,
encryptionKey = encryptionKey,
)
features?.let {
this.delegate.featuresFetchedSuccessfully(
features = it,
isRemote = false
)
}
}
}
handleFetchFeaturesWithoutRemoteEval(dataModel)
}
} catch (error: Throwable) {
// Call Error Delegate with mention of data not available but its not remote
this.delegate.featuresFetchFailed(GBError(error), false)
}
handleFetchFeaturesWithRemoteEval(remoteEval, payload)
}

private fun handleFetchFeaturesWithRemoteEval(
remoteEval: Boolean,
payload: GBRemoteEvalParams?
) {
if (remoteEval) {
dataSource.fetchRemoteEval(
params = payload,
success = { responseFeaturesDataModel ->
prepareFeaturesData(responseFeaturesDataModel.data)
prepareFeaturesDataForRemoteEval(responseFeaturesDataModel.data)
},
failure = { error ->
this.delegate.featuresFetchFailed(GBError(error.exception), true)
Expand All @@ -90,7 +74,7 @@ internal class FeaturesViewModel(
} else {
dataSource.fetchFeatures(
success = { dataModel ->
prepareFeaturesData(dataModel)
prepareFeaturesDataForRemoteEval(dataModel)
},
failure = { error ->
// Call Error Delegate with mention of data not available but its not remote
Expand All @@ -100,12 +84,43 @@ internal class FeaturesViewModel(
}
}

private fun handleFetchFeaturesWithoutRemoteEval(dataModel: FeaturesDataModel) {
dataModel.features?.let {
this.delegate.featuresFetchedSuccessfully(
features = it,
isRemote = false
)
}
dataModel.encryptedFeatures?.let { encryptedFeatures: String ->
encryptionKey?.let { encryptionKey ->
val features = getFeaturesFromEncryptedFeatures(
encryptedString = encryptedFeatures,
encryptionKey = encryptionKey,
)
features?.let {
this.delegate.featuresFetchedSuccessfully(
features = it,
isRemote = false
)
}
}
}
}

private fun getDataFromCache(): FeaturesDataModel? {
val dataModel = manager.getLayer().getData(
Constants.FEATURE_CACHE,
FeaturesDataModel.serializer()
)
return dataModel
}

/**
* Supportive method for automatically refresh features
*/
fun autoRefreshFeatures(): Flow<Resource<GBFeatures?>> {
return dataSource.autoRefresh(success = { dataModel ->
prepareFeaturesData(dataModel = dataModel)
prepareFeaturesDataForRemoteEval(dataModel = dataModel)
}, failure = { error ->
// Call Error Delegate with mention of data not available but its not remote
this.delegate.featuresFetchFailed(GBError(error), true)
Expand All @@ -115,19 +130,15 @@ internal class FeaturesViewModel(
/**
* Cache API Response and push success event
*/
private fun prepareFeaturesData(dataModel: FeaturesDataModel?) {
private fun prepareFeaturesDataForRemoteEval(dataModel: FeaturesDataModel?) {
var features = dataModel?.features
var savedGroups = dataModel?.savedGroups
val encryptedFeatures = dataModel?.encryptedFeatures
val encryptedSavedGroups = dataModel?.encryptedSavedGroups

try {
if (dataModel != null) {
manager.getLayer().putData(
fileName = Constants.FEATURE_CACHE,
content = dataModel,
serializer = FeaturesDataModel.serializer()
)
putDataToCache(dataModel)

delegate.featuresAPIModelSuccessfully(dataModel)
if (!features.isNullOrEmpty()) {
Expand Down Expand Up @@ -214,4 +225,12 @@ internal class FeaturesViewModel(
return
}
}

private fun putDataToCache(dataModel: FeaturesDataModel) {
manager.getLayer().putData(
fileName = Constants.FEATURE_CACHE,
content = dataModel,
serializer = FeaturesDataModel.serializer()
)
}
}

0 comments on commit 0a1806e

Please sign in to comment.