Skip to content

Instantly share code, notes, and snippets.

View WendyYanto's full-sized avatar

Wenn WendyYanto

  • Singapore
View GitHub Profile
class MainActivity: AppCompatActivity() {
@Inject
lateinit var mainPresenter: MainPresenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Injector.inject(MainModule::class, this)
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()
class MainActivity: AppCompatActivity() {
@Inject
lateinit var mainPresenter: MainPresenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// inject mainPresenter here
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FIELD, AnnotationTarget.CONSTRUCTOR)
annotation class Inject
@WendyYanto
WendyYanto / di8.kt
Last active December 6, 2021 17:47
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()
interface InjectorModule {
}
@WendyYanto
WendyYanto / di7.kt
Last active December 6, 2021 18:33
class MainModule: InjectorModule {
@Provides
fun provideMainPresenter(): MainPresenter {
return MainPresenterImpl()
}
}
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION)
annotation class Provides
@WendyYanto
WendyYanto / di5.kt
Last active December 6, 2021 17:35
class MainModule: InjectorModule {
fun provideMainPresenter(): MainPresenter {
return MainPresenterImpl()
}
}
object Injector {
fun inject()
}