Package com.airbnb.mvrx

Types

ActivityViewModelContext
Link copied to clipboard
data class ActivityViewModelContext(activity: ComponentActivity, args: Any?, owner: ViewModelStoreOwner, savedStateRegistry: SavedStateRegistry) : ViewModelContext

The ViewModelContext for a ViewModel created with an activity scope (val viewModel by activityViewModel<MyViewModel>). Although a fragment reference is available when an activity scoped ViewModel is first created, during process restoration, activity scoped ViewModels will be created without a fragment reference, so it is only safe to reference the activity.

Async
Link copied to clipboard
sealed class Async<out T>

The T generic is unused for some classes but since it is sealed and useful for Success and Fail, it should be on all of them.

CoroutinesStateStore
Link copied to clipboard
class CoroutinesStateStore<S : MavericksState>(initialState: S, scope: CoroutineScope, contextOverride: CoroutineContext) : MavericksStateStore<S>
DefaultViewModelDelegateFactory
Link copied to clipboard

Creates ViewModels that are wrapped with a lifecycleAwareLazy so that the ViewModel is automatically created when the Fragment is started (if it is not accessed before then).

DeliveryMode
Link copied to clipboard
sealed class DeliveryMode

Defines what updates a subscription should receive. See: RedeliverOnStart, UniqueOnly.

Fail
Link copied to clipboard
data class Fail<out T>(error: Throwable, value: T?) : Async<T>
FragmentViewModelContext
Link copied to clipboard
data class FragmentViewModelContext(activity: ComponentActivity, args: Any?, fragment: Fragment, owner: ViewModelStoreOwner, savedStateRegistry: SavedStateRegistry) : ViewModelContext

The ViewModelContext for a ViewModel created with a fragment scope (val viewModel by fragmentViewModel<MyViewModel>).

Incomplete
Link copied to clipboard
interface Incomplete

Helper interface for using Async in a when clause for handling both Uninitialized and Loading.

InternalMavericksApi
Link copied to clipboard
annotation class InternalMavericksApi
lifecycleAwareLazy
Link copied to clipboard
class lifecycleAwareLazy<out T>(owner: LifecycleOwner, isMainThread: () -> Boolean, initializer: () -> T) : Lazy<T> , Serializable

This was copied from SynchronizedLazyImpl but modified to automatically initialize in ON_CREATE.

Loading
Link copied to clipboard
data class Loading<out T>(value: T?) : Async<T> , Incomplete
Mavericks
Link copied to clipboard
object Mavericks
MavericksDelegateProvider
Link copied to clipboard
abstract class MavericksDelegateProvider<T, R>
MavericksState
Link copied to clipboard
interface MavericksState

Interface that has to be implemented by all Kotlin data classes that will be used as state.

MavericksStateFactory
Link copied to clipboard
interface MavericksStateFactory<VM : MavericksViewModel<S>, S : MavericksState>
MavericksStateStore
Link copied to clipboard
interface MavericksStateStore<S : Any>
MavericksTestOverrides
Link copied to clipboard
open class MavericksTestOverrides
MavericksView
Link copied to clipboard
interface MavericksView : LifecycleOwner
MavericksViewInternalViewModel
Link copied to clipboard
class MavericksViewInternalViewModel(state: SavedStateHandle) : ViewModel
MavericksViewModel
Link copied to clipboard
abstract class MavericksViewModel<S : MavericksState>(initialState: S)

All Mavericks ViewModels must extend this class. In Mavericks, ViewModels are generic on a single state class. The ViewModel owns all state modifications via setState and other classes may observe the state.

MavericksViewModelConfig
Link copied to clipboard
abstract class MavericksViewModelConfig<S : Any>(debugMode: Boolean, stateStore: MavericksStateStore<S>, coroutineScope: CoroutineScope)

Provides configuration for a MavericksViewModel.

MavericksViewModelConfigFactory
Link copied to clipboard
open class MavericksViewModelConfigFactory(debugMode: Boolean, contextOverride: CoroutineContext, storeContextOverride: CoroutineContext, subscriptionCoroutineContextOverride: CoroutineContext)

Factory for providing the MavericksViewModelConfig for each new ViewModel that is created.

MavericksViewModelFactory
Link copied to clipboard
interface MavericksViewModelFactory<VM : MavericksViewModel<S>, S : MavericksState>

Implement this on your ViewModel's companion object for hooks into state creation and ViewModel creation. For example, if you need access to the fragment or activity owner for dependency injection.

MavericksViewModelProvider
Link copied to clipboard
object MavericksViewModelProvider

Helper ViewModelProvider that has a single method for taking either a Fragment or ComponentActivity instead of two separate ones. The logic for providing the correct scope is inside the method.

MavericksViewModelWrapper
Link copied to clipboard
class MavericksViewModelWrapper<VM : MavericksViewModel<S>, S : MavericksState>(viewModel: VM) : ViewModel
PersistState
Link copied to clipboard
annotation class PersistState

Annotate a field in your MvRxViewModel state with PersistState to have it automatically persisted when Android kills your process to free up memory. MvRx will automatically recreate your ViewModel when the process restarts with these fields saved.

RealMavericksStateFactory
Link copied to clipboard
RedeliverOnStart
Link copied to clipboard
object RedeliverOnStart : DeliveryMode

The subscription will receive the most recent state update when transitioning from locked to unlocked states (stopped -> started), even if the state has not changed while locked.

ScriptableMavericksStateStore
Link copied to clipboard
class ScriptableMavericksStateStore<S : Any>(initialState: S) : ScriptableStateStore<S>

A MavericksStateStore which ignores standard calls to set. Instead it can be scripted via calls to next. This is intended to be used for tests only, and in particular UI tests where you wish to test how your UI code reacts to different ViewModel states. This is not as useful for unit testing your view model, as business logic in state reducers will not be used.

ScriptableStateStore
Link copied to clipboard
interface ScriptableStateStore<S : Any> : MavericksStateStore<S>
StateRestorer
Link copied to clipboard
data class StateRestorer<VM : MavericksViewModel<S>, S : MavericksState>(viewModelContext: ViewModelContext, viewModelClass: Class<out VM>, stateClass: Class<out S>, toRestoredState: (S) -> S)
Success
Link copied to clipboard
data class Success<out T>(value: T) : Async<T>
Uninitialized
Link copied to clipboard
object Uninitialized : Async<Nothing> , Incomplete
UniqueOnly
Link copied to clipboard
class UniqueOnly(subscriptionId: String) : DeliveryMode

The subscription will receive the most recent state update when transitioning from locked to unlocked states (stopped -> started), only if the state has changed while locked. This will include the initial state as a state update.

ViewModelContext
Link copied to clipboard
sealed class ViewModelContext

Creation context for the ViewModel. Includes the ViewModel store owner (either an activity or fragment), and fragment arguments set via Mavericks.KEY_ARG.

ViewModelDelegateFactory
Link copied to clipboard
interface ViewModelDelegateFactory

This is invoked each time a Fragment accesses a ViewModel via the MvRx extension functions (eg fragmentViewModel, activityViewModel, existingViewModel).

ViewModelDoesNotExistException
Link copied to clipboard
open class ViewModelDoesNotExistException(message: String) : IllegalStateException

Functions

_fragmentArgsProvider
Link copied to clipboard
fun <T : Fragment> T._fragmentArgsProvider(): Any?

For internal use only. Public for inline.

_internal
Link copied to clipboard
fun <VM : MavericksViewModel<S>, S : MavericksState> VM._internal(owner: LifecycleOwner?, deliveryMode: DeliveryMode = RedeliverOnStart, action: suspend (S) -> Unit): Job
_internal1
Link copied to clipboard
fun <VM : MavericksViewModel<S>, S : MavericksState, A> VM._internal1(owner: LifecycleOwner?, prop1: KProperty1<S, A>, deliveryMode: DeliveryMode = RedeliverOnStart, action: suspend (A) -> Unit): Job
_internal2
Link copied to clipboard
fun <VM : MavericksViewModel<S>, S : MavericksState, A, B> VM._internal2(owner: LifecycleOwner?, prop1: KProperty1<S, A>, prop2: KProperty1<S, B>, deliveryMode: DeliveryMode = RedeliverOnStart, action: suspend (A, B) -> Unit): Job
_internal3
Link copied to clipboard
fun <VM : MavericksViewModel<S>, S : MavericksState, A, B, C> VM._internal3(owner: LifecycleOwner?, prop1: KProperty1<S, A>, prop2: KProperty1<S, B>, prop3: KProperty1<S, C>, deliveryMode: DeliveryMode = RedeliverOnStart, action: suspend (A, B, C) -> Unit): Job
_internal4
Link copied to clipboard
fun <VM : MavericksViewModel<S>, S : MavericksState, A, B, C, D> VM._internal4(owner: LifecycleOwner?, prop1: KProperty1<S, A>, prop2: KProperty1<S, B>, prop3: KProperty1<S, C>, prop4: KProperty1<S, D>, deliveryMode: DeliveryMode = RedeliverOnStart, action: suspend (A, B, C, D) -> Unit): Job
_internal5
Link copied to clipboard
fun <VM : MavericksViewModel<S>, S : MavericksState, A, B, C, D, E> VM._internal5(owner: LifecycleOwner?, prop1: KProperty1<S, A>, prop2: KProperty1<S, B>, prop3: KProperty1<S, C>, prop4: KProperty1<S, D>, prop5: KProperty1<S, E>, deliveryMode: DeliveryMode = RedeliverOnStart, action: suspend (A, B, C, D, E) -> Unit): Job
_internal6
Link copied to clipboard
fun <VM : MavericksViewModel<S>, S : MavericksState, A, B, C, D, E, F> VM._internal6(owner: LifecycleOwner?, prop1: KProperty1<S, A>, prop2: KProperty1<S, B>, prop3: KProperty1<S, C>, prop4: KProperty1<S, D>, prop5: KProperty1<S, E>, prop6: KProperty1<S, F>, deliveryMode: DeliveryMode = RedeliverOnStart, action: suspend (A, B, C, D, E, F) -> Unit): Job
_internal7
Link copied to clipboard
fun <VM : MavericksViewModel<S>, S : MavericksState, A, B, C, D, E, F, G> VM._internal7(owner: LifecycleOwner?, prop1: KProperty1<S, A>, prop2: KProperty1<S, B>, prop3: KProperty1<S, C>, prop4: KProperty1<S, D>, prop5: KProperty1<S, E>, prop6: KProperty1<S, F>, prop7: KProperty1<S, G>, deliveryMode: DeliveryMode = RedeliverOnStart, action: suspend (A, B, C, D, E, F, G) -> Unit): Job
_internalSF
Link copied to clipboard
fun <VM : MavericksViewModel<S>, S : MavericksState, T> VM._internalSF(owner: LifecycleOwner?, asyncProp: KProperty1<S, Async<T>>, deliveryMode: DeliveryMode = RedeliverOnStart, onFail: suspend (Throwable) -> Unit? = null, onSuccess: suspend (T) -> Unit? = null): Job
activityViewModel
Link copied to clipboard
inline fun <T : Fragment, MavericksView, VM : MavericksViewModel<S>, S : MavericksState> T.activityViewModel(viewModelClass: KClass<VM> = VM::class, noinline keyFactory: () -> String = { viewModelClass.java.name }): MavericksDelegateProvider<T, VM>

fragmentViewModel except scoped to the current Activity. Use this to share state between different Fragments.

appendAt
Link copied to clipboard
fun <T : Any> List<T>.appendAt(other: List<T>?, offset: Int): List<T>

Helper to handle pagination. Use this when you want to append a list of results at a given offset. This is safer than just appending blindly to a list because it guarantees that the data gets added at the offset it was requested at.

args
Link copied to clipboard
fun <V : Any> args(): ReadOnlyProperty<Fragment, V>

Fragment argument delegate that makes it possible to set fragment args without creating a key for each one.

asContextElement
Link copied to clipboard

Wraps StrictMode.ThreadPolicy into ThreadContextElement. The resulting ThreadContextElement maintains the given policy for coroutine regardless of the actual thread its is resumed on.

asMavericksArgs
Link copied to clipboard
fun Parcelable.asMavericksArgs(): Bundle

Takes anything that is Parcelable and creates a Mavericks Fragment argument Bundle.

fun Serializable.asMavericksArgs(): Bundle

Takes anything that is serializable and creates a Mavericks Fragment argument Bundle.

assertMavericksDataClassImmutability
Link copied to clipboard
fun assertMavericksDataClassImmutability(kClass: KClass<*>, allowFunctions: Boolean = false)

Ensures that the state class is immutable. NOTE: Kotlin collections immutability is a compile-time check only and the underlying classes are mutable so it is impossible to detect them here. Kotlin mutability: https://stackoverflow.com/a/33732403/715633

cancelPendingInvalidates
Link copied to clipboard
fun MavericksView.cancelPendingInvalidates()

If any callbacks to run MavericksView.invalidate have been posted with MavericksView.postInvalidate then this cancels them.

existingViewModel
Link copied to clipboard
inline fun <T : Fragment, MavericksView, VM : MavericksViewModel<S>, S : MavericksState> T.existingViewModel(viewModelClass: KClass<VM> = VM::class, crossinline keyFactory: () -> String = { viewModelClass.java.name }): MavericksDelegateProvider<T, VM>

activityViewModel except it will throw IllegalStateException if the ViewModel doesn't already exist. Use this for screens in the middle of a flow that cannot reasonably be an entry point to the flow.

flowWhenStarted
Link copied to clipboard
fun <T> Flow<T>.flowWhenStarted(owner: LifecycleOwner): Flow<T>

Emits values from the source flow only when the owner is started. When the owner transitions to started, the most recent value will be emitted.

fragmentViewModel
Link copied to clipboard
inline fun <T : Fragment, MavericksView, VM : MavericksViewModel<S>, S : MavericksState> T.fragmentViewModel(viewModelClass: KClass<VM> = VM::class, crossinline keyFactory: () -> String = { viewModelClass.java.name }): MavericksDelegateProvider<T, VM>

Gets or creates a ViewModel scoped to this Fragment. You will get the same instance every time for this Fragment, even through rotation, or other configuration changes.

parentFragmentViewModel
Link copied to clipboard
inline fun <T : Fragment, MavericksView, VM : MavericksViewModel<S>, S : MavericksState> T.parentFragmentViewModel(viewModelClass: KClass<VM> = VM::class, crossinline keyFactory: () -> String = { viewModelClass.java.name }): MavericksDelegateProvider<T, VM>

Gets or creates a ViewModel scoped to a parent fragment. This delegate will walk up the parentFragment hierarchy until it finds a Fragment that can provide the correct ViewModel. If no parent fragments can provide the ViewModel, a new one will be created in top-most parent Fragment.

persistMavericksState
Link copied to clipboard
fun <T : MavericksState> persistMavericksState(state: T, validation: Boolean = false): Bundle

Iterates through all member properties annotated with PersistState and parcels them into a bundle that can be saved with savedInstanceState.

restorePersistedMavericksState
Link copied to clipboard
fun <T : MavericksState> restorePersistedMavericksState(bundle: Bundle, initialState: T, validation: Boolean = false): T

Updates the initial state object given state persisted with PersistState in a Bundle.

targetFragmentViewModel
Link copied to clipboard
inline fun <T : Fragment, MavericksView, VM : MavericksViewModel<S>, S : MavericksState> T.targetFragmentViewModel(viewModelClass: KClass<VM> = VM::class, crossinline keyFactory: () -> String = { viewModelClass.java.name }): MavericksDelegateProvider<T, VM>

Gets or creates a ViewModel scoped to a target fragment. Throws IllegalStateException if there is no target fragment.

viewModel
Link copied to clipboard
inline fun <T : ComponentActivity, VM : MavericksViewModel<S>, S : MavericksState> T.viewModel(viewModelClass: KClass<VM> = VM::class, crossinline keyFactory: () -> String = { viewModelClass.java.name }): Lazy<VM>

Gets or creates a ViewModel scoped to the current activity. This is similar to fragmentViewModel and actvityViewModel but used when a view model is accessed directly from an activity itself.

withState
Link copied to clipboard
fun <A : MavericksViewModel<B>, B : MavericksState, C> withState(viewModel1: A, block: (B) -> C): C

Accesses ViewModel state from a single ViewModel synchronously and returns the result of the block.

fun <A : MavericksViewModel<B>, B : MavericksState, C : MavericksViewModel<D>, D : MavericksState, E> withState(viewModel1: A, viewModel2: C, block: (B, D) -> E): E

Accesses ViewModel state from two ViewModels synchronously and returns the result of the block.

fun <A : MavericksViewModel<B>, B : MavericksState, C : MavericksViewModel<D>, D : MavericksState, E : MavericksViewModel<F>, F : MavericksState, G> withState(viewModel1: A, viewModel2: C, viewModel3: E, block: (B, D, F) -> G): G

Accesses ViewModel state from three ViewModels synchronously and returns the result of the block.

fun <A : MavericksViewModel<B>, B : MavericksState, C : MavericksViewModel<D>, D : MavericksState, E : MavericksViewModel<F>, F : MavericksState, G : MavericksViewModel<H>, H : MavericksState, I> withState(viewModel1: A, viewModel2: C, viewModel3: E, viewModel4: G, block: (B, D, F, H) -> I): I

Accesses ViewModel state from four ViewModels synchronously and returns the result of the block.

fun <A : MavericksViewModel<B>, B : MavericksState, C : MavericksViewModel<D>, D : MavericksState, E : MavericksViewModel<F>, F : MavericksState, G : MavericksViewModel<H>, H : MavericksState, I : MavericksViewModel<J>, J : MavericksState, K> withState(viewModel1: A, viewModel2: C, viewModel3: E, viewModel4: G, viewModel5: I, block: (B, D, F, H, J) -> K): K

Accesses ViewModel state from five ViewModels synchronously and returns the result of the block.