This file contains hidden or 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
| module { | |
| viewModel { MyViewModel() } | |
| } | |
| class MyActivity : AppCompatActivity() { | |
| val myViewModel : MyViewModel by viewModel() | |
| } |
This file contains hidden or 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
| class MyActivity : AppCompatActivity, AndroidScopeComponent { | |
| // get current Activity's scope | |
| override val scope : Scope by activityScope() | |
| // MyPresenter is resolved from MyActivity's scope | |
| val presenter : MyPresenter by inject() | |
| } |
This file contains hidden or 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
| module { | |
| // Declare a scope for MyActivity | |
| scope<MyActivity> { | |
| scoped { MyPresenter() } | |
| } | |
| } | |
| class MyActivity : ScopeActivity() { | |
| val presenter : MyPresenter by inject() | |
| } |
This file contains hidden or 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
| module { | |
| factory { MyPresenter() } | |
| } | |
| class MyActivity : AppCompatActivity() { | |
| val presenter : MyPresenter by inject() | |
| } |
This file contains hidden or 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
| class MyActivity : AppCompatActivity() { | |
| val presenter = MyPresenter() | |
| } |
This file contains hidden or 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
| // Implementing our own Scope delegation | |
| class MVPActivity : AppCompatActivity(R.layout.mvp_activity), KoinScopeComponent { | |
| // Create Activity scope (backed by ViewModel) | |
| override val scope: Scope by lazy { activityScope() } | |
| // Inject presenter with org.koin.core.scope.inject extension | |
| // also can use directly the scope: scope.inject<>() | |
| val presenter: ScopedPresenter by inject() | |
| } |
This file contains hidden or 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
| class MyTest : KoinTest { | |
| // Use Mockk as Mock provider | |
| @get:Rule | |
| val mockProvider = MockProviderRule.create { clazz -> | |
| mockkClass(clazz, relaxed = true) | |
| } | |
| // JUnit5 style | |
| @JvmField |
This file contains hidden or 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
| // Implementing our own Scope delegation | |
| class MVPActivity : AppCompatActivity(R.layout.mvp_activity), KoinScopeComponent { | |
| // Create scope | |
| override val scope: Scope by lazy { newScope() } | |
| // Inject presenter with org.koin.core.scope.inject extension | |
| // also can use directly the scope: scope.inject<>() | |
| val presenter: ScopedPresenter by inject() | |
This file contains hidden or 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
| val mvpModule = module { | |
| // Describe a scope for MVPActivity | |
| scope<MVPActivity> { | |
| scoped { ScopedPresenter(get()) } // Inject MVPActivity with get() | |
| } | |
| } | |
| // Using ScopeActivity | |
| class MVPActivity : ScopeActivity(R.layout.mvp_activity) { |
This file contains hidden or 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
| // give a Worker | |
| class SimpleWorker( | |
| private val simpleWorkerService: SimpleWorkerService, | |
| appContext: Context, | |
| private val params: WorkerParameters | |
| ) : CoroutineWorker(appContext, params) { | |
| override suspend fun doWork(): Result { ... } | |
| } |