@@ -3,40 +3,32 @@ package com.ninecraft.booket.core.data.impl.repository
33import com.google.firebase.remoteconfig.FirebaseRemoteConfig
44import com.google.firebase.remoteconfig.get
55import com.ninecraft.booket.core.common.utils.isUpdateRequired
6+ import com.ninecraft.booket.core.common.utils.runSuspendCatching
67import com.ninecraft.booket.core.data.api.repository.RemoteConfigRepository
78import com.ninecraft.booket.core.data.impl.BuildConfig
89import com.orhanobut.logger.Logger
9- import kotlinx.coroutines.suspendCancellableCoroutine
10+ import kotlinx.coroutines.tasks.await
1011import javax.inject.Inject
11- import kotlin.coroutines.resume
1212
1313class DefaultRemoteConfigRepository @Inject constructor(
1414 private val remoteConfig : FirebaseRemoteConfig ,
1515) : RemoteConfigRepository {
16- override suspend fun getLatestVersion (): Result <String > = suspendCancellableCoroutine { continuation ->
17- remoteConfig.fetchAndActivate().addOnCompleteListener { task ->
18- if (task.isSuccessful) {
19- val latestVersion = remoteConfig[KEY_LATEST_VERSION ].asString()
20- Logger .d(" LatestVersion: $latestVersion " )
21- continuation.resume(Result .success(latestVersion))
22- } else {
23- Logger .e(task.exception, " getLatestVersion failed" )
24- continuation.resume(Result .failure(task.exception ? : Exception (" Unknown error" )))
25- }
26- }
16+ override suspend fun getLatestVersion (): Result <String > = runSuspendCatching {
17+ remoteConfig.fetchAndActivate().await()
18+ val latestVersion = remoteConfig[KEY_LATEST_VERSION ].asString()
19+ Logger .d(" LatestVersion: $latestVersion " )
20+ latestVersion
21+ }.onFailure { exception ->
22+ Logger .e(exception, " getLatestVersion failed" )
2723 }
2824
29- override suspend fun shouldUpdate (): Result <Boolean > = suspendCancellableCoroutine { continuation ->
30- remoteConfig.fetchAndActivate().addOnCompleteListener { task ->
31- if (task.isSuccessful) {
32- val minVersion = remoteConfig[KEY_MIN_VERSION ].asString()
33- val currentVersion = BuildConfig .APP_VERSION
34- continuation.resume(Result .success(isUpdateRequired(currentVersion, minVersion)))
35- } else {
36- Logger .e(task.exception, " shouldUpdate: getMinVersion failed" )
37- continuation.resume(Result .failure(task.exception ? : Exception (" Unknown error" )))
38- }
39- }
25+ override suspend fun shouldUpdate (): Result <Boolean > = runSuspendCatching {
26+ remoteConfig.fetchAndActivate().await()
27+ val minVersion = remoteConfig[KEY_MIN_VERSION ].asString()
28+ val currentVersion = BuildConfig .APP_VERSION
29+ isUpdateRequired(currentVersion, minVersion)
30+ }.onFailure { exception ->
31+ Logger .e(exception, " shouldUpdate: getMinVersion failed" )
4032 }
4133
4234 companion object {
0 commit comments