Here are some simple examples how to use Mosby to build an MVP based architecture. We assume that you already know how “MVP - passive view”, if not you should consider to read the MVP section to understand the basics of MVP before continue.
Hello MVP World
In this very simple example we assume that the view displays a TextView displaying either “Hello “ and a random number or “Goodbye” and a random number. If “Hello” is displayed the text color will be red, otherwise “Goodbye” will be displayed with blue text color. Generating the greeting text is done in an AsyncTask and we assume that generating the greeting text (random number computed and concatenated at the end of the greeting text) is super cpu expensive and take 2 seconds.
As you can see HelloWorldActivity now contains only code related to UI components. No business logic, no big Activity with 1000+ lines of spaghetti code. With this clear separation of concerns between View (HelloWorldActivity), Presenter (HelloWorldPresenter) and business logic (GreetingGeneratorTask) you are able to write maintainable, loosely coupled and testable code. That’s what MVP is all about.
The following example is a little bit more complex and closer to a real world application. Assume we want to display a list of countries in a RecyclerView. The list of countries are loaded from a web service (async) and takes a few seconds. We will use Retrofit as http networking library. While loading we want to display a ProgressBar. If an error occurs we want to display an error message. Furthermore, we want to use a SwipeRefreshLayout so that the user refresh the list of countries. We call this kind of View LCE-View (Loading-Content-Error) since the View has three states: displaying loading, displaying content or displaying an error view. Mosby provides a template for such Views: MvpLceActivity or MvpLceFragment. Those template base classes already handle changing the views state (with a fade animation).
LCE ViewState example
What happens in the previous LCE example if the user rotates the screen? A new Fragment gets created that an the UI starts again with showing the ProgressBar even if before the screen orientation change we already were displaying data (list of countries). Mosby offers a feature called ViewState to handle screen orientation changes. Check the ViewState section for details and how it works. Here in this “Getting Started” section we just want to show how to add ViewState support to the previous LCE example so that your app will still be in the state as before screen orientation changes i.e. showing list of countries in portrait and still displaying list of countries in landscape. All we have to do is to extend from MvpLceViewStateFragment instead of MvpLceFragment and implement createViewState() and getData():
Please note that also Activities and ViewGroup support the ViewState feature.
Kotlin is the new hope in android development. Starting with Mosby 2.0 Kotlin is supported. In this example we will show a very basic LCE example where we generate (async) a list of super heroes and display them in a RecyclerView.
To report bugs, issues or start conceptional design discussions please use the issue tracker on GitHub. Otherwise, don't hesitate to ask any other question in the section below. Syntax highlighting is supported.