From 716cca8555038ba4ebb0a4abb882e3c873da659d Mon Sep 17 00:00:00 2001 From: Aditya Wasan Date: Fri, 15 Apr 2022 12:52:56 +0530 Subject: [PATCH 1/3] feat: add `runSuspendCatchingWithContext` Signed-off-by: Aditya Wasan --- core/util/build.gradle.kts | 2 +- .../core/util/extension/RunCatchingExtension.kt | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/core/util/build.gradle.kts b/core/util/build.gradle.kts index 340a1d1..a148524 100644 --- a/core/util/build.gradle.kts +++ b/core/util/build.gradle.kts @@ -1,6 +1,6 @@ plugins { id("dev.skrilltrax.baka.kotlin-library") } dependencies { + api(libs.thirdparty.kotlin.result) implementation(libs.androidx.datastore.prefs.core) - implementation(libs.thirdparty.kotlin.result) } diff --git a/core/util/src/main/kotlin/dev/skrilltrax/baka/core/util/extension/RunCatchingExtension.kt b/core/util/src/main/kotlin/dev/skrilltrax/baka/core/util/extension/RunCatchingExtension.kt index 73dbf86..8993bd7 100644 --- a/core/util/src/main/kotlin/dev/skrilltrax/baka/core/util/extension/RunCatchingExtension.kt +++ b/core/util/src/main/kotlin/dev/skrilltrax/baka/core/util/extension/RunCatchingExtension.kt @@ -9,7 +9,9 @@ import com.github.michaelbull.result.Result import kotlin.contracts.ExperimentalContracts import kotlin.contracts.InvocationKind import kotlin.contracts.contract +import kotlin.coroutines.CoroutineContext import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.withContext /** * Calls the specified function [block] with [this] value as its receiver and returns its @@ -17,7 +19,8 @@ import kotlinx.coroutines.CancellationException * [CancellationException] that was thrown from the [block] function execution and encapsulating it * as a failure. */ -public suspend inline fun runSuspendCatching(block: () -> V): Result { +@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") +public suspend inline fun runSuspendCatching(block: suspend () -> V): Result { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } return try { @@ -46,3 +49,12 @@ public suspend inline infix fun T.runSuspendCatching( Err(e) } } + +public suspend inline fun runSuspendCatchingWithContext( + context: CoroutineContext, + crossinline block: suspend () -> V +): Result = + withContext(context) { + return@withContext runSuspendCatching(block) + } + From 59536a794e496fb6907ee0b0d395b66cd37cdd2a Mon Sep 17 00:00:00 2001 From: Aditya Wasan Date: Fri, 15 Apr 2022 12:53:45 +0530 Subject: [PATCH 2/3] refactor: use `runSuspendCatchingWithContext` Signed-off-by: Aditya Wasan --- core/auth/build.gradle.kts | 3 +-- .../kotlin/dev/skrilltrax/baka/core/auth/AuthManager.kt | 8 ++++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/core/auth/build.gradle.kts b/core/auth/build.gradle.kts index df12997..a9b0c5f 100644 --- a/core/auth/build.gradle.kts +++ b/core/auth/build.gradle.kts @@ -6,12 +6,11 @@ plugins { anvil { generateDaggerFactories.set(true) } dependencies { - implementation(projects.core.util) + api(projects.core.util) implementation(projects.di) implementation(libs.androidx.datastore.prefs.core) implementation(libs.dagger) - api(libs.thirdparty.kotlin.result) testImplementation(libs.testing.kotlin) testImplementation(libs.testing.kotlin.coroutines) diff --git a/core/auth/src/main/kotlin/dev/skrilltrax/baka/core/auth/AuthManager.kt b/core/auth/src/main/kotlin/dev/skrilltrax/baka/core/auth/AuthManager.kt index b4dbd1b..668f8f5 100644 --- a/core/auth/src/main/kotlin/dev/skrilltrax/baka/core/auth/AuthManager.kt +++ b/core/auth/src/main/kotlin/dev/skrilltrax/baka/core/auth/AuthManager.kt @@ -5,9 +5,11 @@ import androidx.datastore.preferences.core.stringPreferencesKey import androidx.datastore.preferences.preferencesDataStore import com.github.michaelbull.result.Result import dev.skrilltrax.baka.core.util.extension.runSuspendCatching +import dev.skrilltrax.baka.core.util.extension.runSuspendCatchingWithContext import javax.inject.Inject import javax.inject.Named import javax.inject.Singleton +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.map @@ -15,14 +17,12 @@ import kotlinx.coroutines.flow.map public class AuthManager @Inject constructor(@Named("InternalFilesDirPath") filesDir: String) { private val authDataStore by preferencesDataStore(filesDir, DATASTORE_NAME) - public suspend fun getAuthToken(): Result { - return runSuspendCatching { + public suspend fun getAuthToken(): Result = runSuspendCatchingWithContext(Dispatchers.IO) { val authTokenKey = stringPreferencesKey(AUTH_TOKEN_KEY) val authToken = authDataStore.data.map { store -> store[authTokenKey] } - return@runSuspendCatching authToken.firstOrNull() ?: throw AuthTokenNotFoundException() + authToken.firstOrNull() ?: throw AuthTokenNotFoundException() } - } public suspend fun saveAuthToken(authToken: String): Result { return runSuspendCatching { From f15eb217fecdbc5f6e7f8917c10a2f84164f0c28 Mon Sep 17 00:00:00 2001 From: Aditya Wasan Date: Fri, 15 Apr 2022 12:54:43 +0530 Subject: [PATCH 3/3] chore: remove redundant `@Singleton` annotation Signed-off-by: Aditya Wasan --- .../src/main/kotlin/dev/skrilltrax/baka/di/NetworkModule.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/network/src/main/kotlin/dev/skrilltrax/baka/di/NetworkModule.kt b/core/network/src/main/kotlin/dev/skrilltrax/baka/di/NetworkModule.kt index 8066c1a..b0f976e 100644 --- a/core/network/src/main/kotlin/dev/skrilltrax/baka/di/NetworkModule.kt +++ b/core/network/src/main/kotlin/dev/skrilltrax/baka/di/NetworkModule.kt @@ -39,7 +39,6 @@ public object NetworkModule { } @Provides - @Singleton public fun provideOkHttpClient(loggingInterceptor: HttpLoggingInterceptor): OkHttpClient { return OkHttpClient.Builder().addInterceptor(loggingInterceptor).build() } @@ -59,7 +58,6 @@ public object NetworkModule { } @Provides - @Singleton public fun provideMemoryCacheFactory( @MemoryCacheSizeLimit maxSizeBytes: Int, @MemoryCacheExpireTime expireAfterMillis: Long