All you need for MVI is Kotlin | by Maciej Sady

On this article, I describe my try and implement a easy state reducer based mostly on Kotlin Flow.

Like all Android developer following the most recent tendencies, I like MVI structure and the unidirectional knowledge move idea. It solves many points out of the field making our code much more bulletproof.

On this article, I received’t go into element about what MVI is, however you could find many nice write-ups about it. Like:

Enjoying with libraries like MVICore, Mobius, or Orbit impressed me to experiment and attempt to implement a move that may carry out state discount.

That’s how StateReducerFlow was born. Let me clarify how I’ve constructed it, the way it works, and the way you need to use it.

Please remember the fact that the next examples are simplified.

Let’s begin with a easy counter. It has one state that may be modified with two occasions: decrement and increment.

Utilizing the above method, we will construction our logic within the following manner:

Occasion -> ViewModel -> State

One situation, although, is that handleEvent might be referred to as from any thread. Having unstructured state updates can result in difficult bugs and race situations. Fortunately, state.update() is already thread-safe, however nonetheless, another logic might be affected.

To unravel that we will introduce a channel that may enable us to course of occasions sequentially, irrespective of from which thread they arrive.

A lot better. Now we course of all occasions sequentially however state updates are nonetheless potential outdoors of the updateState technique.
Ideally, state updates needs to be solely allowed throughout occasion processing.

To realize that we will implement a easy reducer utilizing runningFold.

Now solely the reduceState technique can carry out state transformations.

If you take a look at this instance ViewModel you might discover that solely the reduceState technique comprises vital logic. The whole lot else is simply boilerplate that must be repeated for each new ViewModel.

As all of us like to remain DRY, I wanted to discover a option to extract the generic logic from the ViewModel.

That’s how StateReducerFlow was born.

I wished StateReducerFlow to be a StateFlow that may deal with generic occasions. I began with this definition:

Shifting ahead I extracted my ViewModel logic to the brand new move implementation:

As you’ll be able to see, the one new issues are a number of overrides from StateFlow.
To assemble the move you present the preliminary state, the operate that may scale back it, and the coroutine scope by which the state might be shared.

The final lacking half is a manufacturing facility operate that may create our new move. I’ve determined to go along with ViewModel extension to entry viewModelScope.

Now we will migrate our ViewModel to the brand new StateReducerFlow.

Voilà! The boilerplate is gone.

Now anybody who has entry to StateReducerFlow can ship occasions to it, e.g.

That’s it! Are you curious about the way it works in an actual app or how it may be examined? See my instance undertaking:

Keep impressed!

More Posts