|
|
|
@ -4,6 +4,8 @@
|
|
|
|
|
|
|
|
|
|
package org.mozilla.fenix.components
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.MainThread
|
|
|
|
|
import androidx.annotation.VisibleForTesting
|
|
|
|
|
import androidx.lifecycle.ViewModel
|
|
|
|
|
import androidx.lifecycle.ViewModelProvider
|
|
|
|
|
import androidx.lifecycle.ViewModelStoreOwner
|
|
|
|
@ -33,6 +35,7 @@ class StoreProvider<T : Store<*, *>>(
|
|
|
|
|
*
|
|
|
|
|
* @param createStore Callback to create a new [Store], used when the [ViewModel] is first created.
|
|
|
|
|
*/
|
|
|
|
|
@VisibleForTesting
|
|
|
|
|
class StoreProviderFactory<T : Store<*, *>>(
|
|
|
|
|
private val createStore: () -> T,
|
|
|
|
|
) : ViewModelProvider.Factory {
|
|
|
|
@ -42,3 +45,17 @@ class StoreProviderFactory<T : Store<*, *>>(
|
|
|
|
|
return StoreProvider(createStore()) as VM
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper function for lazy creation of a [Store] instance scoped to a [ViewModelStoreOwner].
|
|
|
|
|
*
|
|
|
|
|
* @param createStore Function that creates a [Store] instance.
|
|
|
|
|
*/
|
|
|
|
|
@MainThread
|
|
|
|
|
fun <T : Store<*, *>> ViewModelStoreOwner.lazyStore(
|
|
|
|
|
createStore: () -> T,
|
|
|
|
|
): Lazy<T> {
|
|
|
|
|
return lazy(mode = LazyThreadSafetyMode.NONE) {
|
|
|
|
|
StoreProvider.get(this, createStore)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|