From 004005d8be3e1823eb2cd70bae21e94fe4b1eb66 Mon Sep 17 00:00:00 2001 From: Ash Date: Thu, 31 Mar 2022 17:16:12 +0800 Subject: [PATCH] Inject CoroutineScope and CoroutineDispatcher --- app/src/main/java/me/ash/reader/App.kt | 7 +++-- .../data/module/CoroutineDispatcherModule.kt | 29 +++++++++++++++++++ .../reader/data/module/CoroutineQualifier.kt | 19 ++++++++++++ .../data/module/CoroutineScopeModule.kt | 27 +++++++++++++++++ .../ash/reader/data/module/DatabaseModule.kt | 7 ++++- .../ash/reader/data/module/RetrofitModule.kt | 9 +++--- .../me/ash/reader/data/module/WorkerModule.kt | 5 ++-- 7 files changed, 93 insertions(+), 10 deletions(-) create mode 100644 app/src/main/java/me/ash/reader/data/module/CoroutineDispatcherModule.kt create mode 100644 app/src/main/java/me/ash/reader/data/module/CoroutineQualifier.kt create mode 100644 app/src/main/java/me/ash/reader/data/module/CoroutineScopeModule.kt diff --git a/app/src/main/java/me/ash/reader/App.kt b/app/src/main/java/me/ash/reader/App.kt index 3c99daa..a6879d4 100644 --- a/app/src/main/java/me/ash/reader/App.kt +++ b/app/src/main/java/me/ash/reader/App.kt @@ -6,16 +6,15 @@ import androidx.work.Configuration import androidx.work.WorkManager import dagger.hilt.android.HiltAndroidApp import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import me.ash.reader.data.module.ApplicationScope import me.ash.reader.data.repository.* import me.ash.reader.data.source.OpmlLocalDataSource import me.ash.reader.data.source.ReaderDatabase import me.ash.reader.data.source.RssNetworkDataSource import javax.inject.Inject -@DelicateCoroutinesApi @HiltAndroidApp class App : Application(), Configuration.Provider { @Inject @@ -54,7 +53,9 @@ class App : Application(), Configuration.Provider { @Inject lateinit var rssRepository: RssRepository - private val applicationScope = CoroutineScope(Dispatchers.Default) + @Inject + @ApplicationScope + lateinit var applicationScope: CoroutineScope override fun onCreate() { super.onCreate() diff --git a/app/src/main/java/me/ash/reader/data/module/CoroutineDispatcherModule.kt b/app/src/main/java/me/ash/reader/data/module/CoroutineDispatcherModule.kt new file mode 100644 index 0000000..ea39da8 --- /dev/null +++ b/app/src/main/java/me/ash/reader/data/module/CoroutineDispatcherModule.kt @@ -0,0 +1,29 @@ +package me.ash.reader.data.module + +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 CoroutineDispatcherModule { + + @Provides + @DispatcherDefault + fun provideDispatcherDefault(): CoroutineDispatcher = Dispatchers.Default + + @Provides + @DispatcherIO + fun provideDispatcherIO(): CoroutineDispatcher = Dispatchers.IO + + @Provides + @DispatcherMain + fun provideDispatcherMain(): CoroutineDispatcher = Dispatchers.Main + + @Provides + @DispatcherMainImmediate + fun provideDispatcherMainImmediate(): CoroutineDispatcher = Dispatchers.Main.immediate +} diff --git a/app/src/main/java/me/ash/reader/data/module/CoroutineQualifier.kt b/app/src/main/java/me/ash/reader/data/module/CoroutineQualifier.kt new file mode 100644 index 0000000..e24d9cf --- /dev/null +++ b/app/src/main/java/me/ash/reader/data/module/CoroutineQualifier.kt @@ -0,0 +1,19 @@ +package me.ash.reader.data.module + +import javax.inject.Qualifier + +@Retention(AnnotationRetention.RUNTIME) +@Qualifier +annotation class DispatcherDefault + +@Retention(AnnotationRetention.RUNTIME) +@Qualifier +annotation class DispatcherIO + +@Retention(AnnotationRetention.RUNTIME) +@Qualifier +annotation class DispatcherMain + +@Retention(AnnotationRetention.BINARY) +@Qualifier +annotation class DispatcherMainImmediate \ No newline at end of file diff --git a/app/src/main/java/me/ash/reader/data/module/CoroutineScopeModule.kt b/app/src/main/java/me/ash/reader/data/module/CoroutineScopeModule.kt new file mode 100644 index 0000000..543fdae --- /dev/null +++ b/app/src/main/java/me/ash/reader/data/module/CoroutineScopeModule.kt @@ -0,0 +1,27 @@ +package me.ash.reader.data.module + +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.SupervisorJob +import javax.inject.Qualifier +import javax.inject.Singleton + +@Retention(AnnotationRetention.RUNTIME) +@Qualifier +annotation class ApplicationScope + +@Module +@InstallIn(SingletonComponent::class) +object CoroutineScopeModule { + + @Provides + @Singleton + @ApplicationScope + fun provideCoroutineScope( + @DispatcherDefault dispatcherDefault: CoroutineDispatcher + ): CoroutineScope = CoroutineScope(SupervisorJob() + dispatcherDefault) +} \ No newline at end of file diff --git a/app/src/main/java/me/ash/reader/data/module/DatabaseModule.kt b/app/src/main/java/me/ash/reader/data/module/DatabaseModule.kt index 9dbf38a..1922e64 100644 --- a/app/src/main/java/me/ash/reader/data/module/DatabaseModule.kt +++ b/app/src/main/java/me/ash/reader/data/module/DatabaseModule.kt @@ -15,20 +15,25 @@ import javax.inject.Singleton @Module @InstallIn(SingletonComponent::class) -class DatabaseModule { +object DatabaseModule { + @Provides + @Singleton fun provideArticleDao(readerDatabase: ReaderDatabase): ArticleDao = readerDatabase.articleDao() @Provides + @Singleton fun provideFeedDao(readerDatabase: ReaderDatabase): FeedDao = readerDatabase.feedDao() @Provides + @Singleton fun provideGroupDao(readerDatabase: ReaderDatabase): GroupDao = readerDatabase.groupDao() @Provides + @Singleton fun provideAccountDao(readerDatabase: ReaderDatabase): AccountDao = readerDatabase.accountDao() diff --git a/app/src/main/java/me/ash/reader/data/module/RetrofitModule.kt b/app/src/main/java/me/ash/reader/data/module/RetrofitModule.kt index 5a3f7ac..b9b97d6 100644 --- a/app/src/main/java/me/ash/reader/data/module/RetrofitModule.kt +++ b/app/src/main/java/me/ash/reader/data/module/RetrofitModule.kt @@ -11,19 +11,20 @@ import javax.inject.Singleton @Module @InstallIn(SingletonComponent::class) -class RetrofitModule { - @Singleton +object RetrofitModule { + @Provides + @Singleton fun provideRssNetworkDataSource(): RssNetworkDataSource = RssNetworkDataSource.getInstance() - @Singleton @Provides + @Singleton fun provideFeverApiDataSource(): FeverApiDataSource = FeverApiDataSource.getInstance() - @Singleton @Provides + @Singleton fun provideGoogleReaderApiDataSource(): GoogleReaderApiDataSource = GoogleReaderApiDataSource.getInstance() } \ No newline at end of file diff --git a/app/src/main/java/me/ash/reader/data/module/WorkerModule.kt b/app/src/main/java/me/ash/reader/data/module/WorkerModule.kt index 6177667..9fcc37c 100644 --- a/app/src/main/java/me/ash/reader/data/module/WorkerModule.kt +++ b/app/src/main/java/me/ash/reader/data/module/WorkerModule.kt @@ -11,9 +11,10 @@ import javax.inject.Singleton @Module @InstallIn(SingletonComponent::class) -class WorkerModule { - @Singleton +object WorkerModule { + @Provides + @Singleton fun provideWorkManager(@ApplicationContext context: Context): WorkManager = WorkManager.getInstance(context) } \ No newline at end of file