Skip to content

Instantly share code, notes, and snippets.

@thegarlynch
Last active March 12, 2020 16:25
Show Gist options
  • Save thegarlynch/e9002b89613d2c1bb017f296a05ea832 to your computer and use it in GitHub Desktop.
Save thegarlynch/e9002b89613d2c1bb017f296a05ea832 to your computer and use it in GitHub Desktop.
tutorial-dagger-snippet-add-component
import dagger.Component
import javax.inject.Inject
import javax.inject.Singleton
@Component
@Singleton
interface TutorialComponent {
val contohMelaluiComponent : Contoh
fun inject(mainActivity : MainActivity)
@Component.Factory
interface Factory {
fun create() : TutorialComponent
}
}
// Class contoh yang akan kita suntik / inject (harus punya anotasi @Inject ya.)
class Contoh @Inject constructor(private val anakContoh: AnakContoh){
val nomor by lazy { (1..10).random() }
val nomorAnak get() = anakContoh.nomor
}
class AnakContoh @Inject constructor(){
val nomor by lazy { (1..10).random() }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment