Nerd For Tech
Published in

Nerd For Tech

MVVM Architecture in Android(Using Retrofit, LiveData)

Photo by cottonbro from Pexels

What is Architecture and why do we need one?

Architecture is the way you structure your android code and files. It is very important to make your project scalable and maintain. Developers spend more time maintain a project than creating it and hence architecture should be clean and easy to maintain. Usually, it requires months to create a project from scratch but various additions in the form of bug fixes and new features are added to it over the years depending on the requirements.

MVC VS MVVM?

MVC is android’s default architecture where the view is the xml layout files and the controller is Activity class. In MVVM both activity and xml files are treated as views and Viewmodel class consist of all of your business logic. There is no link between the app’s UI with business logic.

MVP VS MVVM?

In MVP, the presenter knows about the view and view knows about the presenter. They interact with each other through an interface. In MVVM, only the view knows about the view-model. The viewmodel has no idea about the view.

What's the Objective over here?

It’s a common mistake to write all your code in an Activity or a Fragment. These UI-based classes should only contain logic that handles UI and operating system interactions. By keeping these classes as lean as possible, you can avoid many lifecycle-related problems.

The most important principle to follow is separation of concerns, i.e. your business logic, UI, and data models should live at different places. Another is the decoupling of code: Every piece of code should act as a black box so that changing anything in a class should not have any effect on another part of your codebase.

What is MVVM?

MVVM architecture is a Model-View-ViewModel architecture that removes the tight coupling between each component. Most importantly, in this architecture, the children don’t have the direct reference to the parent, they only have the reference by observables.

Model:
A model represents the data and business logic of the app. One of the recommended implementations of this is to expose its data through observable. Unlike a regular observable, LiveData respects the lifecycle of other app components, such as activities and fragments.

ViewModel:
ViewModel class is designed to store and manage UI-related data in a lifecycle-conscious way. ViewModel should not be aware of the view which is interacting. The ViewModel class allows data to survive configuration changes such as screen rotations

View:
The View refers to the UI component of the application i.e activity and fragments. The role of View in this pattern is to observe a ViewModel observable to get data to update UI elements accordingly. This part is implemented in our MainActivity class where we are observing data from ViewModel and set observed data on the adapter.

What are Android repositories?

Most apps save and retrieve data, either from local storage or a remote server. Android repositories are classes that decide whether data should come from a server or local storage, decoupling your storage logic from outside classes.

Now that we have familiarized ourselves with various terms that we are going to use in the project. Let's see the code in action

Implementation:

In this example, we will show the MVVM concept using a simple project where we will get our data from API using retrofit and display it in our application

First Step

We will create the model class for our response. You also know this as POJO class. This represents the response from the API

We are also using ResponseAPI class which you’ll see later on.

Second Step:
For getting data from API we will be using a retrofit. You can find the implementation in one of my previous post here

Third step:
Now we will create our model view class. LiveData class is designed to store and manage UI-related data in a lifecycle-conscious way. Notice how we are using the init method in our model view class to fetch a list of customers from API.So whenever we initialize our CustomerViewModel class it will automatically fetch the data from the server.
Repository class is where we are making our API calls and using Livedata for observing our API responses

One thing to note, that we are using postValue method to send update to our observer.
one has two options i.e. either you can update the data on the Main thread or you can update the data using the background thread. So, the use-case of setValue and postValue depends on these two situations only.

While using the Main thread to change the data, you should use the setValue method of the MutableLiveData class and while using the background thread to change the LiveData, you should use the postValue method of the MutableLiveData class.

Fourth step:

Now let's look at our activity class where we will be declaring our livedata object and displaying data in UI

That’s it for now!!
Thanks for reading and don’t forget to share with your fellow developers :)
This post was originally posted on
CodeTheraphy.com.

For more articles follow me on Medium and CodeTheraphy.com you can also connect me on LinkedIn

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store