-
public final class MavericksComposeExtensionsKt
-
-
Method Summary
Modifier and Type Method Description final static <VM extends MavericksViewModel<S>, S extends MavericksState> VMmavericksViewModel(LifecycleOwner scope, Function0<String> keyFactory, Function0<Object> argsFactory)Get or create a MavericksViewModel scoped to the closest LocalLifecycleOwner. final static ComponentActivityextractActivityFromContext(Context context)final static FragmentfindFragmentFromView(View view)final static <VM extends MavericksViewModel<S>, S extends MavericksState> VMmavericksActivityViewModel(Function0<String> keyFactory, Function0<Object> argsFactory)Get or create a MavericksViewModel scoped to the local activity. final static <VM extends MavericksViewModel<S>, S extends MavericksState> State<S>collectAsState(VM $self)Creates a Compose State variable that will emit new values whenever this ViewModel's state changes. final static <VM extends MavericksViewModel<S>, S extends MavericksState, O extends Any> State<O>collectAsState(VM $self, Object key, Function1<S, O> mapper)Creates a Compose State variable that will emit new values whenever this ViewModel's state mapped to the provided mapper changes. final static <VM extends MavericksViewModel<S>, S extends MavericksState, A extends Any> State<A>collectAsState(VM $self, KProperty1<S, A> prop1)Creates a Compose State variable that will only update when the value of this property changes. -
-
Method Detail
-
mavericksViewModel
@Composable() final static <VM extends MavericksViewModel<S>, S extends MavericksState> VM mavericksViewModel(LifecycleOwner scope, Function0<String> keyFactory, Function0<Object> argsFactory)
Get or create a MavericksViewModel scoped to the closest LocalLifecycleOwner. In most case, LocalLifecycleOwner will be the host Activity or Nav Graph (if used). However, you can provide a custom scope by overriding the lifecycleOwner parameter.
If you provide your own LifecycleOwner, it MUST also implement ViewModelStoreOwner and SavedStateRegistryOwner. Many standard components such as Fragment, ComponentActivity (and subclasses such as FragmentActivity and AppCompatActivity), and NavBackStackEntry all do.
You can call functions on this ViewModel to update state.
To subscribe to this view model's state, call collectAsState(YourState::yourProp), collectAsState { it.yourProp } or collectAsState() on your view model.
- Parameters:
keyFactory- Optionally provide a key to differentiate multiple viewmodels of the same type in the same scope.argsFactory- If present, the result from this function will be passed to your state constructor as a parameter when the viewmodel is first initialized.
-
extractActivityFromContext
final static ComponentActivity extractActivityFromContext(Context context)
-
findFragmentFromView
final static Fragment findFragmentFromView(View view)
-
mavericksActivityViewModel
@Composable() final static <VM extends MavericksViewModel<S>, S extends MavericksState> VM mavericksActivityViewModel(Function0<String> keyFactory, Function0<Object> argsFactory)
Get or create a MavericksViewModel scoped to the local activity.
-
collectAsState
@Composable() final static <VM extends MavericksViewModel<S>, S extends MavericksState> State<S> collectAsState(VM $self)
Creates a Compose State variable that will emit new values whenever this ViewModel's state changes. Prefer the overload with a state property reference to ensure that your composable only recomposes when the properties it uses changes.
-
collectAsState
@Composable() final static <VM extends MavericksViewModel<S>, S extends MavericksState, O extends Any> State<O> collectAsState(VM $self, Object key, Function1<S, O> mapper)
Creates a Compose State variable that will emit new values whenever this ViewModel's state mapped to the provided mapper changes. Prefer the overload with a state property reference to ensure that your composable only recomposes when the properties it uses changes.
- Parameters:
key- An optional key that should be changed if the mapper changes.
-
collectAsState
@Composable() final static <VM extends MavericksViewModel<S>, S extends MavericksState, A extends Any> State<A> collectAsState(VM $self, KProperty1<S, A> prop1)
Creates a Compose State variable that will only update when the value of this property changes. Prefer this to subscribing to entire state classes which will trigger a recomposition whenever any state variable changes. If you find yourself subscribing to many state properties in a single composable, consider breaking it up into smaller ones.
-
-
-
-