From 3774ef1253625f402d3332944bf70b106adcdb41 Mon Sep 17 00:00:00 2001 From: tgyuuAn Date: Tue, 7 Jan 2025 13:54:49 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[PC-000]=20suspendRunCatching=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/puzzle/common/ResultUtil.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 core/common/src/main/java/com/puzzle/common/ResultUtil.kt diff --git a/core/common/src/main/java/com/puzzle/common/ResultUtil.kt b/core/common/src/main/java/com/puzzle/common/ResultUtil.kt new file mode 100644 index 00000000..915ef4c2 --- /dev/null +++ b/core/common/src/main/java/com/puzzle/common/ResultUtil.kt @@ -0,0 +1,13 @@ +package com.puzzle.common + +import kotlin.coroutines.cancellation.CancellationException + +suspend inline fun T.suspendRunCatching(crossinline block: suspend T.() -> R): Result { + return try { + Result.success(block()) + } catch (e: CancellationException) { + throw e + } catch (t: Throwable) { + Result.failure(t) + } +} From 68759011c9fb62bb4185cae8304c1aad44d94d2a Mon Sep 17 00:00:00 2001 From: tgyuuAn Date: Tue, 7 Jan 2025 14:09:11 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[PC-256]=20map=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=B0=B8=EC=A1=B0=20=ED=95=A8=EC=88=98=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/common/src/main/java/com/puzzle/common/ResultUtil.kt | 2 +- core/data/build.gradle.kts | 1 + .../java/com/puzzle/data/repository/TermsRepositoryImpl.kt | 7 ++++--- .../com/puzzle/network/model/terms/LoadTermsResponse.kt | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/core/common/src/main/java/com/puzzle/common/ResultUtil.kt b/core/common/src/main/java/com/puzzle/common/ResultUtil.kt index 915ef4c2..58aff15c 100644 --- a/core/common/src/main/java/com/puzzle/common/ResultUtil.kt +++ b/core/common/src/main/java/com/puzzle/common/ResultUtil.kt @@ -10,4 +10,4 @@ suspend inline fun T.suspendRunCatching(crossinline block: suspend T.() - } catch (t: Throwable) { Result.failure(t) } -} +} \ No newline at end of file diff --git a/core/data/build.gradle.kts b/core/data/build.gradle.kts index e903b3cc..5d2e8f38 100644 --- a/core/data/build.gradle.kts +++ b/core/data/build.gradle.kts @@ -11,4 +11,5 @@ dependencies { implementation(projects.core.domain) implementation(projects.core.network) implementation(projects.core.database) + implementation(projects.core.common) } diff --git a/core/data/src/main/java/com/puzzle/data/repository/TermsRepositoryImpl.kt b/core/data/src/main/java/com/puzzle/data/repository/TermsRepositoryImpl.kt index 225a74ed..a8240100 100644 --- a/core/data/src/main/java/com/puzzle/data/repository/TermsRepositoryImpl.kt +++ b/core/data/src/main/java/com/puzzle/data/repository/TermsRepositoryImpl.kt @@ -1,5 +1,6 @@ package com.puzzle.data.repository +import com.puzzle.common.suspendRunCatching import com.puzzle.database.model.terms.TermEntity import com.puzzle.database.source.term.LocalTermDataSource import com.puzzle.domain.model.terms.Term @@ -12,7 +13,7 @@ class TermsRepositoryImpl @Inject constructor( private val termDataSource: TermDataSource, private val localTermDataSource: LocalTermDataSource, ) : TermsRepository { - override suspend fun loadTerms(): Result = runCatching { + override suspend fun loadTerms(): Result = suspendRunCatching { val terms = termDataSource.loadTerms() .getOrThrow() .toDomain() @@ -31,8 +32,8 @@ class TermsRepositoryImpl @Inject constructor( localTermDataSource.clearAndInsertTerms(termsEntity) } - override suspend fun getTerms(): Result> = runCatching { + override suspend fun getTerms(): Result> = suspendRunCatching { localTermDataSource.getTerms() - .map { it.toDomain() } + .map(TermEntity::toDomain) } } diff --git a/core/network/src/main/java/com/puzzle/network/model/terms/LoadTermsResponse.kt b/core/network/src/main/java/com/puzzle/network/model/terms/LoadTermsResponse.kt index e2b6cfa4..bb705cfa 100644 --- a/core/network/src/main/java/com/puzzle/network/model/terms/LoadTermsResponse.kt +++ b/core/network/src/main/java/com/puzzle/network/model/terms/LoadTermsResponse.kt @@ -11,7 +11,7 @@ import java.time.LocalDateTime data class LoadTermsResponse( val responses: List?, ) { - fun toDomain() = responses?.map { it.toDomain() } ?: emptyList() + fun toDomain() = responses?.map(TermResponse::toDomain) ?: emptyList() } @Serializable