Skip to content

Instantly share code, notes, and snippets.

@manuelvicnt
Last active October 29, 2019 19:50
Show Gist options
  • Save manuelvicnt/4aa85080c217bfeffa9c73ed6699f349 to your computer and use it in GitHub Desktop.
Save manuelvicnt/4aa85080c217bfeffa9c73ed6699f349 to your computer and use it in GitHub Desktop.
@Singleton
@Component(modules = [FeedCacheModule::class, DatabaseModule::class])
interface AppComponent { ... }
@Module
object FeedCacheModule {
@Provides
fun feedCache(feedDatabase: FeedDatabase): FeedCache {
return RoomFeedCache(database.feedDao())
}
}
@Module
object DatabaseModule {
@Provides
@Singleton
fun providesDatabase(context: Context): FeedDatabase {
return Room.databaseBuilder(
context,
FeedDatabase::class.java,
DATABASE_NAME
).build()
}
}
// Other options: If DB is always needed but the FeedCache doesn't need to be always available
@Singleton
@Component(modules = [DatabaseModule::class])
interface AppComponent { ... }
@Subcomponent(modules = [FeedCacheModule::class])
interface FeedComponent { ... }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment