@@ -9,18 +9,25 @@ import androidx.lifecycle.ViewModelProviders
9
9
import org.kodein.di.DKodein
10
10
import org.kodein.di.Kodein
11
11
import org.kodein.di.KodeinAware
12
+ import org.kodein.di.bindings.BindingKodein
12
13
import org.kodein.di.bindings.NoArgBindingKodein
13
14
import org.kodein.di.direct
14
- import org.kodein.di.generic.bind
15
- import org.kodein.di.generic.instance
16
- import org.kodein.di.generic.instanceOrNull
17
- import org.kodein.di.generic.provider
15
+ import org.kodein.di.generic.*
18
16
19
17
/* *
20
18
* Binds a ViewModel to a Kotlin module, assuming that it's a provided dependency.
21
19
*/
22
- inline fun <reified T : ViewModel > Kodein.Builder.bindViewModel (overrides : Boolean? = null, noinline creator : NoArgBindingKodein <* >.() -> T ) {
23
- bind<T >(T ::class .java.simpleName, overrides) with provider(creator)
20
+ inline fun <reified VM : ViewModel > Kodein.Builder.bindViewModel (overrides : Boolean? = null,
21
+ noinline creator : NoArgBindingKodein <* >.() -> VM ) {
22
+ bind<VM >(VM ::class .java.simpleName, overrides) with provider(creator)
23
+ }
24
+
25
+ /* *
26
+ * Binds a ViewModel factory to a Kotlin module in order to create new ViewModels.
27
+ */
28
+ inline fun <reified VM : ViewModel , reified F : ViewModelProvider.Factory > Kodein.Builder.bindViewModelFactory (overrides : Boolean? = null,
29
+ noinline creator : BindingKodein <* >.(Any ) -> F ) {
30
+ bind<F >(VM ::class .java, overrides) with factory(creator = creator)
24
31
}
25
32
26
33
/* *
@@ -31,10 +38,8 @@ inline fun <reified T : ViewModel> Kodein.Builder.bindViewModel(overrides: Boole
31
38
* if you allow creating new instances of them via [Class.newInstance] with [allowNewInstance].
32
39
* The default is true to mimic the default behaviour of [ViewModelProviders.of].
33
40
*/
34
- class KodeinViewModelFactory (
35
- private val injector : DKodein ,
36
- private val allowNewInstance : Boolean = true
37
- ) : ViewModelProvider.Factory {
41
+ class KodeinViewModelFactory (private val injector : DKodein ,
42
+ private val allowNewInstance : Boolean = true ) : ViewModelProvider.Factory {
38
43
@Suppress(" UNCHECKED_CAST" )
39
44
override fun <T : ViewModel > create (modelClass : Class <T >): T {
40
45
return injector.instanceOrNull<ViewModel >(tag = modelClass.simpleName) as T ?
@@ -64,4 +69,36 @@ inline fun <reified VM : ViewModel, T> T.viewModel(): Lazy<VM> where T : KodeinA
64
69
return lazy {
65
70
ViewModelProviders .of(this , direct.instance()).get(VM ::class .java)
66
71
}
67
- }
72
+ }
73
+
74
+ /* *
75
+ * Injects a [ViewModel] into a [FragmentActivity] that implements [KodeinAware].
76
+ *
77
+ * Requires previous [ViewModelProvider.Factory] injection for the [ViewModel] via [bindViewModelFactory]
78
+ * to work and a [TypedViewModel] to be used.
79
+ */
80
+ @MainThread
81
+ inline fun <reified T , reified VM : TypedViewModel <T >, A > A.viewModel (params : T ): Lazy <VM > where A : KodeinAware , A : FragmentActivity {
82
+ return lazy {
83
+ ViewModelProviders .of(this , direct.instance(VM ::class .java, params)).get(VM ::class .java)
84
+ }
85
+ }
86
+
87
+ /* *
88
+ * Injects a [ViewModel] into a [Fragment] that implements [KodeinAware].
89
+ *
90
+ * Requires previous [ViewModelProvider.Factory] injection for the [ViewModel] via [bindViewModelFactory]
91
+ * to work and a [TypedViewModel] to be used.
92
+ */
93
+ @MainThread
94
+ inline fun <reified T , reified VM : TypedViewModel <T >, F > F.viewModel (params : T ): Lazy <VM > where F : KodeinAware , F : Fragment {
95
+ return lazy {
96
+ ViewModelProviders .of(this , direct.instance(VM ::class .java, params)).get(VM ::class .java)
97
+ }
98
+ }
99
+
100
+ /* *
101
+ * Generic [ViewModel] that adds support for adding a single [params] object to ease parameter
102
+ * injection.
103
+ */
104
+ open class TypedViewModel <T >(private val params : T ) : ViewModel()
0 commit comments