Inject CoroutineScope and CoroutineDispatcher

This commit is contained in:
Ash 2022-03-31 17:16:12 +08:00
parent 525870b613
commit 004005d8be
7 changed files with 93 additions and 10 deletions

View File

@ -6,16 +6,15 @@ import androidx.work.Configuration
import androidx.work.WorkManager import androidx.work.WorkManager
import dagger.hilt.android.HiltAndroidApp import dagger.hilt.android.HiltAndroidApp
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import me.ash.reader.data.module.ApplicationScope
import me.ash.reader.data.repository.* import me.ash.reader.data.repository.*
import me.ash.reader.data.source.OpmlLocalDataSource import me.ash.reader.data.source.OpmlLocalDataSource
import me.ash.reader.data.source.ReaderDatabase import me.ash.reader.data.source.ReaderDatabase
import me.ash.reader.data.source.RssNetworkDataSource import me.ash.reader.data.source.RssNetworkDataSource
import javax.inject.Inject import javax.inject.Inject
@DelicateCoroutinesApi
@HiltAndroidApp @HiltAndroidApp
class App : Application(), Configuration.Provider { class App : Application(), Configuration.Provider {
@Inject @Inject
@ -54,7 +53,9 @@ class App : Application(), Configuration.Provider {
@Inject @Inject
lateinit var rssRepository: RssRepository lateinit var rssRepository: RssRepository
private val applicationScope = CoroutineScope(Dispatchers.Default) @Inject
@ApplicationScope
lateinit var applicationScope: CoroutineScope
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()

View File

@ -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
}

View File

@ -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

View File

@ -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)
}

View File

@ -15,20 +15,25 @@ import javax.inject.Singleton
@Module @Module
@InstallIn(SingletonComponent::class) @InstallIn(SingletonComponent::class)
class DatabaseModule { object DatabaseModule {
@Provides @Provides
@Singleton
fun provideArticleDao(readerDatabase: ReaderDatabase): ArticleDao = fun provideArticleDao(readerDatabase: ReaderDatabase): ArticleDao =
readerDatabase.articleDao() readerDatabase.articleDao()
@Provides @Provides
@Singleton
fun provideFeedDao(readerDatabase: ReaderDatabase): FeedDao = fun provideFeedDao(readerDatabase: ReaderDatabase): FeedDao =
readerDatabase.feedDao() readerDatabase.feedDao()
@Provides @Provides
@Singleton
fun provideGroupDao(readerDatabase: ReaderDatabase): GroupDao = fun provideGroupDao(readerDatabase: ReaderDatabase): GroupDao =
readerDatabase.groupDao() readerDatabase.groupDao()
@Provides @Provides
@Singleton
fun provideAccountDao(readerDatabase: ReaderDatabase): AccountDao = fun provideAccountDao(readerDatabase: ReaderDatabase): AccountDao =
readerDatabase.accountDao() readerDatabase.accountDao()

View File

@ -11,19 +11,20 @@ import javax.inject.Singleton
@Module @Module
@InstallIn(SingletonComponent::class) @InstallIn(SingletonComponent::class)
class RetrofitModule { object RetrofitModule {
@Singleton
@Provides @Provides
@Singleton
fun provideRssNetworkDataSource(): RssNetworkDataSource = fun provideRssNetworkDataSource(): RssNetworkDataSource =
RssNetworkDataSource.getInstance() RssNetworkDataSource.getInstance()
@Singleton
@Provides @Provides
@Singleton
fun provideFeverApiDataSource(): FeverApiDataSource = fun provideFeverApiDataSource(): FeverApiDataSource =
FeverApiDataSource.getInstance() FeverApiDataSource.getInstance()
@Singleton
@Provides @Provides
@Singleton
fun provideGoogleReaderApiDataSource(): GoogleReaderApiDataSource = fun provideGoogleReaderApiDataSource(): GoogleReaderApiDataSource =
GoogleReaderApiDataSource.getInstance() GoogleReaderApiDataSource.getInstance()
} }

View File

@ -11,9 +11,10 @@ import javax.inject.Singleton
@Module @Module
@InstallIn(SingletonComponent::class) @InstallIn(SingletonComponent::class)
class WorkerModule { object WorkerModule {
@Singleton
@Provides @Provides
@Singleton
fun provideWorkManager(@ApplicationContext context: Context): WorkManager = fun provideWorkManager(@ApplicationContext context: Context): WorkManager =
WorkManager.getInstance(context) WorkManager.getInstance(context)
} }