Created
March 12, 2020 16:46
-
-
Save thegarlynch/b566254982288fc6e68f3c7be4e8263f to your computer and use it in GitHub Desktop.
tutorial-dagger-snippet-change-scope
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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