Skip to content

Instantly share code, notes, and snippets.

@ravisorathiya
Created January 22, 2022 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravisorathiya/1c9be72a01a68f2d90c489d4f58cdd5a to your computer and use it in GitHub Desktop.
Save ravisorathiya/1c9be72a01a68f2d90c489d4f58cdd5a to your computer and use it in GitHub Desktop.
import android.app.Application
import android.content.Context
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainCoroutineDispatcher
import javax.inject.Qualifier
import javax.inject.Singleton
@Module
@InstallIn(SingletonComponent::class)
object DispatcherModule {
@Singleton
@Provides
fun provideContext(application: Application): Context = application.applicationContext
@MainDispatcher
@Provides
@Singleton
fun provideMainDispatcher(): MainCoroutineDispatcher = Dispatchers.Main
@DefaultDispatcher
@Provides
@Singleton
fun provideDefaultDispatcher(): CoroutineDispatcher = Dispatchers.Default
@IoDispatcher
@Provides
@Singleton
fun provideIODispatcher(): CoroutineDispatcher = Dispatchers.IO
@UnconfinedDispatcher
@Provides
@Singleton
fun provideUnconfinedDispatcher(): CoroutineDispatcher = Dispatchers.Unconfined
}
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class IoDispatcher
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class MainDispatcher
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class DefaultDispatcher
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class UnconfinedDispatcher
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment