From 8ff2f84ce87ee282bc6aa09b4fe25c86c824db37 Mon Sep 17 00:00:00 2001
From: Vahid Garousi <garousiv@gmail.com>
Date: Fri, 17 Mar 2023 11:57:32 +0330
Subject: [PATCH] Provide Coroutine Scope and Dispatchers Modules for #56

---
 .../common/di/CoroutinesDispatchersModule.kt  | 29 +++++++++++++++++++
 .../dev/common/di/CoroutinesQualifiers.kt     | 23 +++++++++++++++
 .../dev/common/di/CoroutinesScopesModule.kt   | 23 +++++++++++++++
 3 files changed, 75 insertions(+)
 create mode 100644 core/common/src/main/java/garousi/dev/common/di/CoroutinesDispatchersModule.kt
 create mode 100644 core/common/src/main/java/garousi/dev/common/di/CoroutinesQualifiers.kt
 create mode 100644 core/common/src/main/java/garousi/dev/common/di/CoroutinesScopesModule.kt

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