Skip to content

Instantly share code, notes, and snippets.

@exallium
Created November 18, 2016 16:38
Show Gist options
  • Save exallium/5812a1992173d16fa66b3cf2405339fc to your computer and use it in GitHub Desktop.
Save exallium/5812a1992173d16fa66b3cf2405339fc to your computer and use it in GitHub Desktop.
// Database interface
interface Database {
fun getUserById(id: String): Observable<User>
fun getAllUsers(): Observable<List<User>>
fun getUserChangeEvents(): Observable<ChangeEvent<User>>
}
// Describes what happened to what item
data class ChangeEvent<T>(val eventType: ChangeEventType, val t: T)
// Describes what happened to an item
enum class ChangeEventType { ADDED, REMOVED, CHANGED, MOVED }
// Concrete Impl
class DatabaseImpl : Database {
// Impl...
}
// Database module (optional, you could just add this to AppModule)
@Module
class DatabaseModule {
@Provides
@Singleton
fun provideDatabase(): DatabaseImpl()
}
// Application level module
@Module(includes=arrayOf(DatabaseModule::class))
class AppModule {
/* ... */
}
// Application level dagger component
@Singleton
@Component(modules=arrayOf(AppModule::class))
class AppComponent {
fun database(): Database
}
class SomeFragment : Fragment {
@Inject
lateinit var database: Database
fun onCreate(/*...*/) {
database.getAllUsers().subscribe {
// Do something with your users
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment