-
Notifications
You must be signed in to change notification settings - Fork 174
/
DIAware.kt
88 lines (84 loc) · 3.32 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
@file:Suppress("UNCHECKED_CAST")
package org.kodein.di.android.x.viewmodel
import androidx.annotation.MainThread
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.createViewModelLazy
import androidx.lifecycle.*
import org.kodein.di.DI
import org.kodein.di.DIAware
import org.kodein.di.direct
import org.kodein.di.provider
/**
* Returns a [Lazy] property delegate to access the [AppCompatActivity]'s [ViewModel], which will be obtained from a
* Kodein DI provider.
*
* ```
* class MyAppCompatActivity : AppCompatActivity(), DIAware {
* override val di: DI by closestDI()
* private val viewmodel: MyViewModel by viewModel()
* }
* ```
* @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 provider was found.
* @throws DI.DependencyLoopException When calling the provider, if the value construction triggered a dependency loop.
*/
@MainThread
inline fun <A, reified VM> A.viewModel(
tag: Any? = null,
): Lazy<VM> where A : AppCompatActivity, A : DIAware, VM : ViewModel {
val factoryProducer = { object : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
val vmProvider = direct.provider<VM>(tag)
return vmProvider() 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 provider.
* ```
* class MyFragment : Fragment(), DIAware {
* override val di: DI by closesDI()
* private val viewmodel: MyViewModel by viewModel()
* }
* ```
*
* Default scope may be overridden with parameter [ownerProducer]:
* ```
* class MyFragment : Fragment(), DIAware {
* override val di: DI by closestDI()
* private val viewmodel: MyViewModel by viewModel (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 provider was found.
* @throws DI.DependencyLoopException When calling the provider, if the value construction triggered a dependency loop.
*/
@MainThread
inline fun <F, reified VM> F.viewModel(
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 : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
val vmProvider = direct.provider<VM>(tag)
return vmProvider() as T
}
}
}
)
}