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 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()

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

View File

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

View File

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