-
Notifications
You must be signed in to change notification settings - Fork 500
Integrating MvRx In Your App
This is no longer required as of MvRx 1.3.0
Similar to your Activity, MvRx ships with a BaseMvRxFragment that has the necessary setup code. It is recommended that you make your base fragment just extend this. It adds very little overhead when not using MvRx and ensures that things will be set up correctly.
If this is not practical for your app, you can copy implementation details from that class into your own base Fragment. The reason we don't ship it is because we recommend that apps have their own BaseFragment in which they can put shared code like logging or helper methods. We do, however, recommend that that class extend BaseMvRxFragment
This applies to releases before 2.0.0
MvRx runs a number of debug checks to ensure that your usage of MvRx is correct. In order for that to run, you must create your own base ViewModel that passes in the correct value for debugMode
.
An example of this would look like this:
abstract class MvRxViewModel<S : MvRxState>(initialState: S) : BaseMvRxViewModel<S>(initialState, debugMode = BuildConfig.DEBUG)
All this does is ensure that the debug checks are run at the right time and exposes a new ViewModel for your features to extend.
We can't ship a MvRxViewModel
directly because you have to use the BuildConfig class from your own application.
For releases since 2.0.0
You must configure the MvRx
object with global settings for how ViewModels should be created. The main requirement is that you set a value for MvRx.viewModelConfigFactory
- the MvRxViewModelConfigFactory
specifies how ViewModels are created.
It is fine to use the default implementation of MvRxViewModelConfigFactory
, but you must specify whether it should be created in debug mode or not. If debug mode is enabled MvRx runs a number of debug checks to ensure that your usage of MvRx is correct.
Simple setup can be done from your application initialization like this:
MvRx.viewModelConfigFactory = MvRxViewModelConfigFactory(applicationContext)
This checks whether your application was built as a debuggable build, and if so will enable the debug checks.
If you would like to take advantage of MvRx's mocking system at all you should instead initialize the global settings via the MvRxMocks
object in your application's initialization.
MvRxMocks.install(applicationContext)
This can be done instead of MvRx.viewModelConfigFactory
, as this will set a mockable debug version of MvRxViewModelConfigFactory
if your app was built as a debuggable build. If your app was not built debuggable (ie for production), then MvRxMocks.install
will simply set up a non debug version of MvRxViewModelConfigFactory
for you.