@@ -5,27 +5,23 @@ import androidx.fragment.app.Fragment
5
5
import androidx.fragment.app.FragmentActivity
6
6
import androidx.lifecycle.ViewModel
7
7
import androidx.lifecycle.ViewModelProvider
8
- import org.kodein.di.DKodein
9
- import org.kodein.di.Kodein
10
- import org.kodein.di.KodeinAware
11
- import org.kodein.di.bindings.BindingKodein
12
- import org.kodein.di.bindings.NoArgBindingKodein
13
- import org.kodein.di.direct
14
- import org.kodein.di.generic.*
8
+ import org.kodein.di.*
9
+ import org.kodein.di.bindings.BindingDI
10
+ import org.kodein.di.bindings.NoArgBindingDI
15
11
16
12
/* *
17
13
* Binds a ViewModel to a Kotlin module, assuming that it's a provided dependency.
18
14
*/
19
- inline fun <reified VM : ViewModel > Kodein .Builder.bindViewModel (overrides : Boolean? = null,
20
- noinline creator : NoArgBindingKodein <* >.() -> VM ) {
15
+ inline fun <reified VM : ViewModel > DI .Builder.bindViewModel (overrides : Boolean? = null,
16
+ noinline creator : NoArgBindingDI <* >.() -> VM ) {
21
17
bind<VM >(VM ::class .java.simpleName, overrides) with provider(creator)
22
18
}
23
19
24
20
/* *
25
21
* Binds a ViewModel factory to a Kotlin module in order to create new ViewModels.
26
22
*/
27
- inline fun <reified VM : ViewModel , reified F : ViewModelProvider.Factory > Kodein .Builder.bindViewModelFactory (overrides : Boolean? = null,
28
- noinline creator : BindingKodein <* >.(Any ) -> F ) {
23
+ inline fun <reified VM : ViewModel , reified F : ViewModelProvider.Factory > DI .Builder.bindViewModelFactory (overrides : Boolean? = null,
24
+ noinline creator : BindingDI <* >.(Any ) -> F ) {
29
25
bind<F >(VM ::class .java, overrides) with factory(creator = creator)
30
26
}
31
27
@@ -37,7 +33,7 @@ inline fun <reified VM : ViewModel, reified F : ViewModelProvider.Factory> Kodei
37
33
* if you allow creating new instances of them via [Class.newInstance] with [allowNewInstance].
38
34
* The default is true to mimic the default behaviour of [ViewModelProvider].
39
35
*/
40
- class KodeinViewModelFactory (private val injector : DKodein ,
36
+ class DIViewModelFactory (private val injector : DirectDI ,
41
37
private val allowNewInstance : Boolean = true ) : ViewModelProvider.Factory {
42
38
@Suppress(" UNCHECKED_CAST" )
43
39
override fun <T : ViewModel > create (modelClass : Class <T >): T {
@@ -51,71 +47,72 @@ class KodeinViewModelFactory(private val injector: DKodein,
51
47
}
52
48
53
49
/* *
54
- * Injects a [ViewModel] into a [FragmentActivity] that implements [KodeinAware ].
50
+ * Injects a [ViewModel] into a [FragmentActivity] that implements [DIAware ].
55
51
*/
56
52
@MainThread
57
- inline fun <reified VM : ViewModel , A > A.viewModel (): Lazy <VM > where A : KodeinAware , A : FragmentActivity {
53
+ inline fun <reified VM : ViewModel , A > A.viewModel (): Lazy <VM > where A : DIAware , A : FragmentActivity {
58
54
return lazy {
59
55
ViewModelProvider (this , direct.instance()).get(VM ::class .java)
60
56
}
61
57
}
62
58
63
59
/* *
64
- * Injects a [ViewModel] into a [Fragment] that implements [KodeinAware ].
60
+ * Injects a [ViewModel] into a [Fragment] that implements [DIAware ].
65
61
*/
66
62
@MainThread
67
- inline fun <reified VM : ViewModel , F > F.viewModel (): Lazy <VM > where F : KodeinAware , F : Fragment {
63
+ inline fun <reified VM : ViewModel , F > F.viewModel (): Lazy <VM > where F : DIAware , F : Fragment {
68
64
return lazy {
69
65
ViewModelProvider (this , direct.instance()).get(VM ::class .java)
70
66
}
71
67
}
72
68
73
69
/* *
74
- * Injects a [ViewModel] into a [FragmentActivity] that implements [KodeinAware ].
70
+ * Injects a [ViewModel] into a [FragmentActivity] that implements [DIAware ].
75
71
*
76
72
* Requires previous [ViewModelProvider.Factory] injection for the [ViewModel] via [bindViewModelFactory]
77
73
* to work and a [TypedViewModel] to be used.
78
74
*/
79
75
@MainThread
80
- inline fun <reified T , reified VM : TypedViewModel <T >, A > A.viewModel (params : T ): Lazy <VM > where A : KodeinAware , A : FragmentActivity {
76
+ inline fun <reified T : Any , reified VM : TypedViewModel <T >, A > A.viewModel (params : T ): Lazy <VM > where A : DIAware , A : FragmentActivity {
81
77
return lazy {
82
78
ViewModelProvider (this , direct.instance(VM ::class .java, params)).get(VM ::class .java)
83
79
}
84
80
}
85
81
86
82
/* *
87
- * Injects a [ViewModel] into a [Fragment] that implements [KodeinAware ].
83
+ * Injects a [ViewModel] into a [Fragment] that implements [DIAware ].
88
84
*
89
85
* Requires previous [ViewModelProvider.Factory] injection for the [ViewModel] via [bindViewModelFactory]
90
86
* to work and a [TypedViewModel] to be used.
91
87
*/
92
88
@MainThread
93
- inline fun <reified T , reified VM : TypedViewModel <T >, F > F.viewModel (params : T ): Lazy <VM > where F : KodeinAware , F : Fragment {
89
+ inline fun <reified T : Any , reified VM : TypedViewModel <T >, F > F.viewModel (params : T ): Lazy <VM > where F : DIAware , F : Fragment {
94
90
return lazy {
95
91
ViewModelProvider (this , direct.instance(VM ::class .java, params)).get(VM ::class .java)
96
92
}
97
93
}
98
94
99
95
/* *
100
- * Injects a [ViewModel] with an [Activity] context that implements [KodeinAware], in order to share it between
96
+ * Injects a [ViewModel] with an [Activity] context that implements [DIAware
97
+ * ], in order to share it between
101
98
* different fragments hosted by that same [Activity].
102
99
*/
103
100
@MainThread
104
- inline fun <reified VM : ViewModel , F > F.sharedActivityViewModel (): Lazy <VM > where F : KodeinAware , F : Fragment {
101
+ inline fun <reified VM : ViewModel , F > F.sharedActivityViewModel (): Lazy <VM > where F : DIAware , F : Fragment {
105
102
return lazy {
106
103
ViewModelProvider (this .requireActivity(), direct.instance()).get(VM ::class .java)
107
104
}
108
105
}
109
106
110
107
/* *
111
- * Injects a [ViewModel] with an [Activity] context that implements [KodeinAware ], in order to share it between
108
+ * Injects a [ViewModel] with an [Activity] context that implements [DIAware ], in order to share it between
112
109
* different fragments hosted by that same [Activity].
113
110
*
114
111
* Requires previous [ViewModelProvider.Factory] injection for the [ViewModel] via [bindViewModelFactory]
115
112
* to work and a [TypedViewModel] to be used.
116
113
*/
117
114
@MainThread
118
- inline fun <reified T , reified VM : TypedViewModel <T >, F > F.sharedActivityViewModel (params : T ): Lazy <VM > where F : KodeinAware , F : Fragment {
115
+ inline fun <reified T : Any , reified VM : TypedViewModel <T >, F > F.sharedActivityViewModel (params : T ): Lazy <VM > where F : DIAware , F : Fragment {
119
116
return lazy {
120
117
ViewModelProvider (this .requireActivity(), direct.instance(VM ::class .java, params)).get(VM ::class .java)
121
118
}
@@ -125,4 +122,4 @@ inline fun <reified T, reified VM : TypedViewModel<T>, F> F.sharedActivityViewMo
125
122
* Generic [ViewModel] that adds support for adding a single [params] object to ease parameter
126
123
* injection.
127
124
*/
128
- open class TypedViewModel <T >(private val params : T ) : ViewModel()
125
+ open class TypedViewModel <T >(private val params : T ) : ViewModel()
0 commit comments