This article demonstrates how to back a TextInputEditText with MutableLiveData using a DSL.

It is a follow-up to the previous article in whith I was solving a problem stated in the next section. This time I am solving it with a DSL, although the underlying mechanism is identical.

The problem

The problem is how to save all changes made in a form (here: TextInputEditText) immediately into a MutableLiveData<String>, so that it survives screen orientation changes, switching Fragments and so on.

Once your TextInputEditText form fields are backed by instances of MutableLiveData<String>, you may refer to them in your AppCompatActivity to react to [Back] and [Home] button in the way presented in a dedicated article in this blog.

The project

The project I am using it in is Victor-Events, but because it is still work in progress, and it is already rather large now, you will probably want to just focus on the code presented in the present article instead of cloning the whole project.

Introducing a new type

I am introducing a new data type, which I called HotData, that connects MutableLiveData with LifecycleOwner:

data class HotData<T>(val liveData: MutableLiveData<T>, val owner: LifecycleOwner)

operator fun <T> MutableLiveData<T>.plus(owner: LifecycleOwner) = HotData(this, owner)

operator fun <T> LifecycleOwner.plus(liveData: MutableLiveData<T>) = HotData(liveData, this)

The above HotData is created with the + operator by using it either way; MutableLiveData on the left and LifecycleOwner on the right, or the other way round.

+= operator

This is the operator that performs the actual backing:

operator fun TextInputEditText.plusAssign(hotData: HotData<String>) = withLiveData(hotData.owner, hotData.liveData)

It is invoked this way:

name += model.name + this

(model.name in the above code snippet is an instance of MutableLiveData<String>, and this is a LifecycleOwner).

It works identically to the following invocation, already discussed in details in the previous article:

name.withLiveData(this, model.name)

Conclusion

I occasionally enjoy playing with DSLs, although they are nothing more than clever operators, lambdas and so on.

I took the particular idea of combining objects with a + operator from the plus operator in kotlinx.coroutines.

I am not entirely convinced to using operators and new types instead of regular function calls, but I have already performed the refactoring in all of my forms in the project, and I shall see whether I still like it after a couple of weeks.