Skip to content

Instantly share code, notes, and snippets.

Avatar

Wenn WendyYanto

  • Singapore
View GitHub Profile
View di12.kt
class MainActivity: AppCompatActivity() {
@Inject
lateinit var mainPresenter: MainPresenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Injector.inject(MainModule::class, this)
View di11.kt
object Injector {
fun <T : InjectorModule, R : Any> inject(kClass: KClass<T>, entryPointClass: R) {
// get all provider methods
val methods = kClass.java.declaredMethods
.filter { method -> method.isAnnotationPresent(Provides::class.java) }
// construct module instance
val moduleInstance = kClass.java.newInstance()
View di10.kt
class MainActivity: AppCompatActivity() {
@Inject
lateinit var mainPresenter: MainPresenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// inject mainPresenter here
View d19.kt
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FIELD, AnnotationTarget.CONSTRUCTOR)
annotation class Inject
View di8.kt
object Injector {
fun <T : InjectorModule, R : Any> inject(kClass: KClass<T>, entryPointClass: R) {
// get all provider methods
val methods = kClass.java.declaredMethods
.filter { method -> method.isAnnotationPresent(Provides::class.java) }
// construct module instance
val moduleInstance = kClass.java.newInstance()
View di7.kt
class MainModule: InjectorModule {
@Provides
fun provideMainPresenter(): MainPresenter {
return MainPresenterImpl()
}
}
View di6.kt
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION)
annotation class Provides
View di5.kt
class MainModule: InjectorModule {
fun provideMainPresenter(): MainPresenter {
return MainPresenterImpl()
}
}
View d14.kt
object Injector {
fun inject()
}