Skip to content

Commit 0fd656f

Browse files
author
Adrián García
committed
Add sharedActivityViewModel
1 parent 0f1183f commit 0fd656f

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
- Add `sharedActivityViewModel` to Kodein extensions to support shared Activity view models.
810
### Fixed
911
- Fix `ConcurrentModificationException`s in store subscriptions' iteration by adding safe iteration over them.
12+
### Changed
1013
- Rename `toggleAbility` to `toggleEnabled` as the name was confusing.
1114

1215
## [1.1.2] - 2020-02-07

mini-kodein-android/src/main/java/mini/kodein/android/KodeinAndroidUtils.kt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)