Motivation

Binding view models

To create a ViewModel instance we normally end up doing the same thing over and over again.

mViewModel = ViewModelProviders.of(this, mViewModelFactory)[MyViewModel::class.java]

Exposing async state

Also to expose the state from an async operation from repository level to view level one very common pattern also recommended by Google is to use a Resource<T>class that has three states: loading, error and success.

Then in an Activity or Fragment we can observer the state like this

mViewModel.liveData.observe(this, Observer { res ->
    res?.let {
        if (it.success()) {
            renderData(it.data)
        } else if (it.error()) {
            renderError()
        } else {
            renderLoading()
        }
    }
})

To avoid writing code like these over and over again in large code bases Scimitar uses annotation processing to generate this code in compile.

Last updated