diff --git a/core/common/src/main/java/garousi/dev/common/di/CoroutinesDispatchersModule.kt b/core/common/src/main/java/garousi/dev/common/di/CoroutinesDispatchersModule.kt new file mode 100644 index 0000000..e304ed4 --- /dev/null +++ b/core/common/src/main/java/garousi/dev/common/di/CoroutinesDispatchersModule.kt @@ -0,0 +1,29 @@ +package garousi.dev.common.di + +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers + +@Module +@InstallIn(SingletonComponent::class) +object CoroutinesDispatchersModule { + + @DefaultDispatcher + @Provides + fun providesDefaultDispatcher(): CoroutineDispatcher = Dispatchers.Default + + @IoDispatcher + @Provides + fun providesIoDispatcher(): CoroutineDispatcher = Dispatchers.IO + + @MainDispatcher + @Provides + fun providesMainDispatcher(): CoroutineDispatcher = Dispatchers.Main + + @MainImmediateDispatcher + @Provides + fun providesMainImmediateDispatcher(): CoroutineDispatcher = Dispatchers.Main.immediate +} diff --git a/core/common/src/main/java/garousi/dev/common/di/CoroutinesQualifiers.kt b/core/common/src/main/java/garousi/dev/common/di/CoroutinesQualifiers.kt new file mode 100644 index 0000000..4e80cae --- /dev/null +++ b/core/common/src/main/java/garousi/dev/common/di/CoroutinesQualifiers.kt @@ -0,0 +1,23 @@ +package garousi.dev.common.di + +import javax.inject.Qualifier + +@Retention(AnnotationRetention.BINARY) +@Qualifier +annotation class DefaultDispatcher + +@Retention(AnnotationRetention.BINARY) +@Qualifier +annotation class IoDispatcher + +@Retention(AnnotationRetention.BINARY) +@Qualifier +annotation class MainDispatcher + +@Retention(AnnotationRetention.BINARY) +@Qualifier +annotation class MainImmediateDispatcher + +@Retention(AnnotationRetention.BINARY) +@Qualifier +annotation class ApplicationScope diff --git a/core/common/src/main/java/garousi/dev/common/di/CoroutinesScopesModule.kt b/core/common/src/main/java/garousi/dev/common/di/CoroutinesScopesModule.kt new file mode 100644 index 0000000..c40c1c6 --- /dev/null +++ b/core/common/src/main/java/garousi/dev/common/di/CoroutinesScopesModule.kt @@ -0,0 +1,23 @@ +package garousi.dev.common.di + +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent +import javax.inject.Singleton +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.SupervisorJob + +@Module +@InstallIn(SingletonComponent::class) +object CoroutinesScopesModule { + @Singleton // Provide always the same instance + @Provides + fun providesCoroutineScope( + @DefaultDispatcher defaultDispatcher: CoroutineDispatcher + ): CoroutineScope { + // Run this code when providing an instance of CoroutineScope + return CoroutineScope(SupervisorJob() + defaultDispatcher) + } +} \ No newline at end of file