Skip to content

Instantly share code, notes, and snippets.

@ininmm
Last active September 28, 2019 09:57
Show Gist options
  • Save ininmm/d95cbace1b16ad13ed2942cbba5787ef to your computer and use it in GitHub Desktop.
Save ininmm/d95cbace1b16ad13ed2942cbba5787ef to your computer and use it in GitHub Desktop.
DataModule 的 整理
@Module(includes = [DatabaseModuleBinds::class])
class DatabaseModule {
@Singleton
@Provides
fun provideDataBase(context: Context): ToDoRoomDatabase {
return Room.databaseBuilder(
context.applicationContext,
ToDoRoomDatabase::class.java,
"Task.db"
).build()
}
@Provides
fun provideTasksDao(db: ToDoDatabase) = db.tasksDao()
}
@Module
abstract class DatabaseModuleBinds {
@Binds
abstract fun bindToDoDatabase(roomDatabase: ToDoRoomDatabase): ToDoDatabase
}
@Module(
includes = [
DataSourceModule::class,
RepositoryBinds::class,
DatabaseModule::class,
NetworkModule::class
]
)
class DataModule {
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class TasksLocalData
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class TasksRemoteData
}
@Module(includes = [DataSourceBinds::class])
class DataSourceModule {
@DataModule.TasksRemoteData
@Provides
fun provideTasksRemoteDataSource(): TasksDataSource {
return TasksRemoteDataSource
}
}
@Module
abstract class DataSourceBinds {
@DataModule.TasksLocalData
@Binds
abstract fun bindToTasksLocalData(tasksLocalDataSource: TasksLocalDataSource): TasksDataSource
}
@Module
abstract class RepositoryBinds {
@Singleton
@Binds
abstract fun bindToTasksRepository(tasksRepository: TasksRepository): ITasksRepository
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment