Saturday, February 29, 2020

An efficient way to use Uniflow


An efficient way to use Uniflow

Photo from Max Bender

TL;DR

This article is about how we can use uniflow, what is the benefit of using this library and how easy it is to use it. Don’t worry we will cover the testing. We will see how easy it is to test our ViewModel

Uniflow

Uniflow help you write your app with a simple unidirectional data flow approach, to ensure consistency through the time and this with Kotlin Coroutines.

Let’s create a sample app using Uniflow

Most application have a profile screen let’s focus on this use-case. What benefits will uniflow give us in our use-case? There are many benefits, let me point them out :

  • Easy way to test
  • We just need to write States & Events
  • Single source of truth
  • We can use coroutines
  • A smart way to write a Data flow in pure Kotlin
Click Me

State

Let’s write a class we will use to represent a state in our application. We will call it UserProfileState. All we need is a data class. We need to define only this data class because we will use generic Loading/Retry states

What is a state: A state is something that we need to remain/keep the UI logical data. This is the reason why our state has only a name, an email and a mobile, to have to feed our UI.

Event

What is an event: An event is a “one-shot” action, we don’t need to retain it.

We will have a few events:

  • RetryView it will appear to the user when we don’t have data — in this example when we don’t have anything to display
  • Loading it will appear to the user when we are waiting for the result
  • OpenEmail it will open another activity with is EmailActivity
  • OpenMobileNumber it will open another activity with is MobileNumberActivity

View Model

Now let’s create our ViewModel class that extends AndroidDataFlow

When we create our UserProfileViewModel we want to set the initial state because when we are opening our screen we don’t yet have data to set. That’s why we are sending our event to set the loading state.

Let’s focus on our next method because it will be important. Without this state, we may not have the possibility to make another action.

Well, this is important because we are setting our state and mostly because it allows the user to take actions. Without this state, we can’t take actions like OpenEmail or OpenMobileNumber, let’s take a look at those methods

Probably now you’re wondering about fromState. This is a method that will prevent you from running the logic inside the block if the state is different from UserProfileState. Doing this will give us an easy way to control the states. Guaranteeing that we are always in the expected state before starting the next one

But don’t worry it is also easy to catch error in fromState. One way offered by Uniflow is to make this “try/catch” block for you by offering a fallback lambda function, to let you handle your action in case of error

Activity

By now we’re almost done with all our logic so let’s take a look on how to implement our UI. Function onStates in our activity allows us to consume the incoming state.

As you can see, the event will trigger a method called openFragment. This method will check if the fragment currently displayed to the user is not the same as the fragment that we want to set. If true, we want to replace the current fragment for UserProfileFragment. Why do we want to do that, let’s see our sample below

Fragment

Fragments allow us to have a more flexible UI and to have the logic clearly encapsulated. As you can see it’s pretty much the same logic as in our Activity plus. We don’t need to worry about getting the last state for this fragment and update UI because Uniflow will take care of the rest.

Testing

To don’t forget about the important part which is testing let’s focus on that.

What we will need to set before we run tests:

When everything is set, we can start to write our test

That’s how easy it is to test with Uniflow. Your ViewModel has one state at a time. Testing will focus on checking state/event sequences and you can replay any state and mockup any scenario!



from Hacker News https://ift.tt/2wUaHOZ

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.