refactor: move coroutine-utils into core

This commit is contained in:
Harsh Shandilya 2023-06-05 09:50:04 +05:30
parent c50c9c8528
commit 68db3dcda6
No known key found for this signature in database
16 changed files with 30 additions and 49 deletions

View file

@ -16,12 +16,13 @@ android { namespace = "dev.msfjarvis.claw.core" }
anvil { generateDaggerFactories.set(true) }
dependencies {
api(libs.javax.inject)
api(libs.kotlinx.coroutines.core)
api(libs.kotlinx.serialization.json)
api(libs.okhttp.loggingInterceptor)
implementation(platform(libs.okhttp.bom))
implementation(libs.dagger)
implementation(libs.javax.inject)
implementation(libs.napier)
implementation(libs.okhttp.core)
implementation(libs.retrofit.kotlinxSerializationConverter)

View file

@ -0,0 +1,32 @@
/*
* Copyright © 2022-2023 Harsh Shandilya.
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
@file:Suppress("InjectDispatcher") // False-positive
package dev.msfjarvis.claw.core.coroutines
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
/** Interface to allow abstracting individual [CoroutineDispatcher]s out of class dependencies. */
@OptIn(ExperimentalCoroutinesApi::class)
interface DispatcherProvider {
fun main(): CoroutineDispatcher = Dispatchers.Main
fun default(): CoroutineDispatcher = Dispatchers.Default
fun io(): CoroutineDispatcher = Dispatchers.IO
fun unconfined(): CoroutineDispatcher = Dispatchers.Unconfined
fun database(): CoroutineDispatcher = Dispatchers.IO.limitedParallelism(1)
}
/** Concrete type for [DispatcherProvider] with all the defaults from the class. */
class DefaultDispatcherProvider @Inject constructor() : DispatcherProvider

View file

@ -0,0 +1,54 @@
/*
* Copyright © 2022-2023 Harsh Shandilya.
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
package dev.msfjarvis.claw.core.injection
import com.deliveryhero.whetstone.app.ApplicationScope
import com.squareup.anvil.annotations.ContributesTo
import dagger.Binds
import dagger.Module
import dagger.Provides
import dev.msfjarvis.claw.core.coroutines.DefaultDispatcherProvider
import dev.msfjarvis.claw.core.coroutines.DispatcherProvider
import javax.inject.Qualifier
import kotlinx.coroutines.CoroutineDispatcher
@Qualifier @Retention(AnnotationRetention.RUNTIME) annotation class DatabaseDispatcher
@Qualifier @Retention(AnnotationRetention.RUNTIME) annotation class MainDispatcher
@Qualifier @Retention(AnnotationRetention.RUNTIME) annotation class IODispatcher
@Qualifier @Retention(AnnotationRetention.RUNTIME) annotation class DefaultDispatcher
@Module
@ContributesTo(ApplicationScope::class)
interface CoroutineDispatcherModule {
@Binds fun DefaultDispatcherProvider.bind(): DispatcherProvider
companion object {
@[Provides IODispatcher]
fun provideIODispatcher(dispatcherProvider: DispatcherProvider): CoroutineDispatcher {
return dispatcherProvider.io()
}
@[Provides DatabaseDispatcher]
fun provideDatabaseDispatcher(dispatcherProvider: DispatcherProvider): CoroutineDispatcher {
return dispatcherProvider.database()
}
@[Provides MainDispatcher]
fun provideMainDispatcher(dispatcherProvider: DispatcherProvider): CoroutineDispatcher {
return dispatcherProvider.main()
}
@[Provides DefaultDispatcher]
fun provideDefaultDispatcher(dispatcherProvider: DispatcherProvider): CoroutineDispatcher {
return dispatcherProvider.default()
}
}
}