Skip to content

Instantly share code, notes, and snippets.

@thegarlynch
Created March 12, 2020 16:46
Show Gist options
  • Save thegarlynch/b566254982288fc6e68f3c7be4e8263f to your computer and use it in GitHub Desktop.
Save thegarlynch/b566254982288fc6e68f3c7be4e8263f to your computer and use it in GitHub Desktop.
tutorial-dagger-snippet-change-scope
import dagger.Component
import javax.inject.Inject
import javax.inject.Scope
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class TutorialScope
@Component
@TutorialScope
interface TutorialComponent {
// Ini kita akan panggil nanti
val contohMelaluiComponent : Contoh
fun inject(mainActivity: MainActivity)
@Component.Factory
interface Factory {
fun create() : TutorialComponent
}
}
// Class contoh yang bisa kita gunakan untuk inject (harus punya anotasi @Inject ya.)
@TutorialScope
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