-
Notifications
You must be signed in to change notification settings - Fork 174
/
DIAware.kt
89 lines (85 loc) · 3.73 KB
/
DIAware.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
@file:Suppress("UNCHECKED_CAST")
package org.kodein.di.android.x.viewmodel.savedstate
import androidx.annotation.MainThread
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.createViewModelLazy
import androidx.lifecycle.*
import androidx.lifecycle.viewmodel.CreationExtras
import org.kodein.di.DI
import org.kodein.di.DIAware
import org.kodein.di.direct
import org.kodein.type.generic
/**
* Returns a [Lazy] property delegate to access the [AppCompatActivity]'s [ViewModel], which will be obtained from a
* Kodein DI factory with a [SavedStateHandle] argument.
*
* ```
* class MyAppCompatActivity : AppCompatActivity(), DIAware {
* override val di: DI by closestDI()
* private val viewmodel: MyViewModel by viewModelWithSavedStateHandle()
* }
* ```
* @param A Extension function applies to [AppCompatActivity] which implements [DIAware]
* @param VM The type of [ViewModel] to obtain
* @param tag The bound Kodein DI tag
* @throws IllegalArgumentException if accessed before the [AppCompatActivity] is attached to the [Application]
* @throws DI.NotFoundException If no factory was found.
* @throws DI.DependencyLoopException When calling the factory, if the value construction triggered a dependency loop.
*/
@MainThread
inline fun <A, reified VM> A.viewModelWithSavedStateHandle(
tag: Any? = null,
): Lazy<VM> where A : AppCompatActivity, A : DIAware, VM : ViewModel {
val factoryProducer = { object : AbstractSavedStateViewModelFactory(this, null) {
override fun <T : ViewModel> create(key: String, modelClass: Class<T>, handle: SavedStateHandle): T {
val factory = direct.Factory(generic<SavedStateHandle>(), generic<VM>(), tag)
return factory(handle) as T
}
} }
return ViewModelLazy(VM::class, { viewModelStore }, factoryProducer)
}
/**
* Returns a [Lazy] property delegate to access a [ViewModel] by **default** scoped to this [Fragment], which will
* be obtained from a Kodein DI factory with a [SavedStateHandle] argument.
* ```
* class MyFragment : Fragment(), DIAware {
* override val di: DI by closesDI()
* private val viewmodel: MyViewModel by viewModelWithSavedStateHandle()
* }
* ```
*
* Default scope may be overridden with parameter [ownerProducer]:
* ```
* class MyFragment : Fragment(), DIAware {
* override val di: DI by closestDI()
* private val viewmodel: MyViewModel by viewModelWithSavedStateHandle(ownerProducer = { requireParentFragment() })
* }
* ```
*
* @param F Extension function applies to [Fragment] which implements [DIAware]
* @param VM The type of [ViewModel] to obtain
* @param ownerProducer Optionally override the default ViewModel owner
* @param tag The bound Kodein DI tag
* @throws IllegalArgumentException if accessed before this Fragment is attached
* @throws DI.NotFoundException If no factory was found.
* @throws DI.DependencyLoopException When calling the factory, if the value construction triggered a dependency loop.
*/
@MainThread
inline fun <F, reified VM> F.viewModelWithSavedStateHandle(
noinline ownerProducer: () -> ViewModelStoreOwner = { this },
tag: Any? = null,
): Lazy<VM> where F : Fragment, F : DIAware, VM : ViewModel {
return createViewModelLazy(
viewModelClass = VM::class,
storeProducer = { ownerProducer().viewModelStore},
factoryProducer = {
object : AbstractSavedStateViewModelFactory(this, arguments) {
override fun <T : ViewModel> create(key: String, modelClass: Class<T>, handle: SavedStateHandle): T {
val factory = direct.Factory(generic<SavedStateHandle>(), generic<VM>(), tag)
return factory(handle) as T
}
}
}
)
}