Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add runSuspendCatchingWithContext #98

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions core/auth/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ 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

@Singleton
public class AuthManager @Inject constructor(@Named("InternalFilesDirPath") filesDir: String) {
private val authDataStore by preferencesDataStore(filesDir, DATASTORE_NAME)

public suspend fun getAuthToken(): Result<String, Throwable> {
return runSuspendCatching {
public suspend fun getAuthToken(): Result<String, Throwable> = 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<String, Throwable> {
return runSuspendCatching {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public object NetworkModule {
}

@Provides
@Singleton
public fun provideOkHttpClient(loggingInterceptor: HttpLoggingInterceptor): OkHttpClient {
return OkHttpClient.Builder().addInterceptor(loggingInterceptor).build()
}
Expand All @@ -59,7 +58,6 @@ public object NetworkModule {
}

@Provides
@Singleton
public fun provideMemoryCacheFactory(
@MemoryCacheSizeLimit maxSizeBytes: Int,
@MemoryCacheExpireTime expireAfterMillis: Long
Expand Down
2 changes: 1 addition & 1 deletion core/util/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ 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
* encapsulated result if invocation was successful, catching any [Throwable] except
* [CancellationException] that was thrown from the [block] function execution and encapsulating it
* as a failure.
*/
public suspend inline fun <V> runSuspendCatching(block: () -> V): Result<V, Throwable> {
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")
public suspend inline fun <V> runSuspendCatching(block: suspend () -> V): Result<V, Throwable> {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }

return try {
Expand Down Expand Up @@ -46,3 +49,12 @@ public suspend inline infix fun <T, V> T.runSuspendCatching(
Err(e)
}
}

public suspend inline fun <V> runSuspendCatchingWithContext(
context: CoroutineContext,
crossinline block: suspend () -> V
): Result<V, Throwable> =
withContext(context) {
return@withContext runSuspendCatching(block)
}