@@ -55,7 +55,7 @@ class KodeinViewModelFactory(private val injector: DKodein,
5555 * Injects a [ViewModel] into a [FragmentActivity] that implements [KodeinAware].
5656 */
5757@MainThread
58- inline fun <reified VM : ViewModel , T > T .viewModel (): Lazy <VM > where T : KodeinAware , T : FragmentActivity {
58+ inline fun <reified VM : ViewModel , A > A .viewModel (): Lazy <VM > where A : KodeinAware , A : FragmentActivity {
5959 return lazy {
6060 ViewModelProviders .of(this , direct.instance()).get(VM ::class .java)
6161 }
@@ -65,7 +65,7 @@ inline fun <reified VM : ViewModel, T> T.viewModel(): Lazy<VM> where T : KodeinA
6565 * Injects a [ViewModel] into a [Fragment] that implements [KodeinAware].
6666 */
6767@MainThread
68- inline fun <reified VM : ViewModel , T > T .viewModel (): Lazy <VM > where T : KodeinAware , T : Fragment {
68+ inline fun <reified VM : ViewModel , F > F .viewModel (): Lazy <VM > where F : KodeinAware , F : Fragment {
6969 return lazy {
7070 ViewModelProviders .of(this , direct.instance()).get(VM ::class .java)
7171 }
@@ -97,6 +97,31 @@ inline fun <reified T, reified VM : TypedViewModel<T>, F> F.viewModel(params: T)
9797 }
9898}
9999
100+ /* *
101+ * Injects a [ViewModel] with an [Activity] context that implements [KodeinAware], in order to share it between
102+ * different fragments hosted by that same [Activity].
103+ */
104+ @MainThread
105+ inline fun <reified VM : ViewModel , F > F.sharedActivityViewModel (): Lazy <VM > where F : KodeinAware , F : Fragment {
106+ return lazy {
107+ ViewModelProviders .of(this .requireActivity(), direct.instance()).get(VM ::class .java)
108+ }
109+ }
110+
111+ /* *
112+ * Injects a [ViewModel] with an [Activity] context that implements [KodeinAware], in order to share it between
113+ * different fragments hosted by that same [Activity].
114+ *
115+ * Requires previous [ViewModelProvider.Factory] injection for the [ViewModel] via [bindViewModelFactory]
116+ * to work and a [TypedViewModel] to be used.
117+ */
118+ @MainThread
119+ inline fun <reified T , reified VM : TypedViewModel <T >, F > F.sharedActivityViewModel (params : T ): Lazy <VM > where F : KodeinAware , F : Fragment {
120+ return lazy {
121+ ViewModelProviders .of(this .requireActivity(), direct.instance(VM ::class .java, params)).get(VM ::class .java)
122+ }
123+ }
124+
100125/* *
101126 * Generic [ViewModel] that adds support for adding a single [params] object to ease parameter
102127 * injection.
0 commit comments