Skip to content

Commit c9c1ccd

Browse files
committed
[BOOK-427] refactor: suspendCancellableCoroutine -> await
1 parent 198a605 commit c9c1ccd

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

core/data/impl/src/main/kotlin/com/ninecraft/booket/core/data/impl/repository/DefaultRemoteConfigRepository.kt

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,32 @@ package com.ninecraft.booket.core.data.impl.repository
33
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
44
import com.google.firebase.remoteconfig.get
55
import com.ninecraft.booket.core.common.utils.isUpdateRequired
6+
import com.ninecraft.booket.core.common.utils.runSuspendCatching
67
import com.ninecraft.booket.core.data.api.repository.RemoteConfigRepository
78
import com.ninecraft.booket.core.data.impl.BuildConfig
89
import com.orhanobut.logger.Logger
9-
import kotlinx.coroutines.suspendCancellableCoroutine
10+
import kotlinx.coroutines.tasks.await
1011
import javax.inject.Inject
11-
import kotlin.coroutines.resume
1212

1313
class 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

Comments
 (0)