Photo credit by pixabay

Introducing Slick, The Reactive MVP Framework

A Simple and Slick to use MVP Framework

M. Reza Nasirloo
ProAndroidDev
Published in
4 min readApr 6, 2018

--

Hi, I’m M. Reza Nasirloo, This is my first Medium post 😊 I’m going to show you how Slick Framework works and how it will help you write better apps.

A little bit of backstory: Through the years that I’ve been developing Android apps I’ve used lots of MVP libraries, none of them satisfied me, Always there was something wrong with them, either I had to extend their base Activity or View interface, some of them use Reflection or other dark magic which results in bad performance and funny issues with the app or the IDE and building and debugging processes, and some of them were simply too complicated for the task in hand. I’ve read so many good articles about software architecture, However, their implementation wasn’t perfect enough! So I decided to write my own, I wrote Slick ~1.5 years ago, Now it is pretty much mature.

Enough talking let’s dive into code. I assume you are familiar with the MVP concept.

Create your View interface:

Create a Presenter, You need to extend the generic SlickPresenter<V> with your view interface:

Finally the View class:

That’s it :)

A class is generated with the name of your Presenter +” _Slick” suffix, pretty much like the Binding framework which generates a class with the “Binding” suffix.

The usage is the same for Fragments, and Conductor Controllers for Custom Views the procedure is a bit different, It’s described in the docs.

Dagger

Of course, if Dagger is your thing you can use it to Inject your Presenters, Slick supports Dagger out of the box. 🔪

Reactive Features

That was the standard base functionality, let’s take a look at the good stuff. Reactive programming is the new standard for writing apps and I’ve integrated it into Slick Framework, It’s an optional package. The reactive part is called Slick Unidirectional Data Flow, it is inspired by MVI and Elm Architecture.

To use this feature you need to pull its package and extend the SlickPresenterUni<V, S> :

First of all! what is the ViewStateActivity? It’s a simple Immutable POJO that holds the view’s state, and it doesn't contain any Android-specific dependency.

Imagine a simple view that is responsible to show movie details and the user’s comments about the movie. It needs the movie details, e.g: title, poster, ratings, comments, and is it on the favorite list? I just write the last two:

Dead simple, Also there are two abstract methods start() and render(), the start() method is called only once for the entire view lifecycle, it is called when the presenter’s onViewUp() is called, we use this method to create our ViewState stream.

The second one is the render() method, this method is called every time there’s a new ViewState, also it gets called on every onViewUp() call to deliver the last ViewState, e.g: Screen orientation

Let’s fill up these methods, I begin with the start() method:

There are two key methods here: command() and reduce(), I will dedicate another post to explain what's going on under the hood, but for now, I just explain what they do.

How Commands and Reduce works

So what is a command? Commands are given to execute, e.g: A General commands its soldiers to march or fire or whatever that they do.

The same analogy here, Your app has commands too, e.g: Post a picture, Like a comment. These commands need to be executed and their result should be delivered to View.

But every command’s result is a subset of this ViewState class, so we need another class to represent the partial data updates. Meet the PartialViewState<ViewState> interface, every command’s result should return an instance of this interface, here is an implementation for movie favorite state:

As you can see we create a new ViewState and change only the part which has been updated. Using AutoValue is highly recommended here, or a data class if you’re a Kotlin fan.

And here is the ViewActivity interface:

The first two are command stream methods that we saw earlier.

Here is a simple implementation of likeMovie() :

And finally, the last part is rendering the ViewState to View:

Did you notice we didn’t bother ourselves with any View or Presenter lifecycle spaghetti code!? We don’t even need to check for getView() == null in our Presenters anymore, Bonus tip, that was just the Hollywood Principle: “Don't call us! We call you!”

Conclusion

We learned how to use the Slick simple features, and also how we can create a unidirectional data flow with immutable view state in our presenter with the help of Slick without worrying about managing subscription of RxJava streams or Views and Presenters lifecycle.

You can find the Slick Github page here.

--

--