MVVM With Hilt, Rxjava 3, Retrofit, Room, Live Data and View Binding
MVVM With Hilt, Rxjava 3, Retrofit, Room, Live Data and View Binding
EDITOR'S PICKS TOP 10 STORIES FOR WRITERS SUBMIT STRAIGHT INTO YOUR INBOX
You have 2 free member-only stories left this month. Sign up for Medium and get an extra one
In this article, we will see how to implement MVVM architecture with Hilt,
RxJava, Retrofit, Room, Live Data, and View Binding.
In this project, we will fetch details form a REST API and then use Room to
save it offline. Since a number of concepts are being used here we will look
at each of them one by one.
If you want to learn about dagger first you can check out this article. It will
be helpful for you to understand hilt more easily.
Project structure:
To setup hilt, you can also find the instructions from here.
1 package com.example.pokemon.network;
2
3 import com.example.pokemon.model.PokemonResponse;
4
5 import javax.annotation.Generated;
6
7 import io.reactivex.rxjava3.core.Observable;
8 import retrofit2.http.GET;
9
10 public interface PokeApiService {
11
12 @GET("pokemon")
13 Observable<PokemonResponse> getPokemons();
14 }
As you can see the return type is Observable will see more about RxJava
later in the article.
1 package com.example.pokemon.model;
2
3 import androidx.room.Entity;
4 import androidx.room.PrimaryKey;
5
6 import io.reactivex.rxjava3.schedulers.Schedulers;
7
8 /**
9 * Created by Abhinav Singh on 17,June,2020
10 */
11 public class Pokemon {
12
13 private int id;
14 private String name;
15
16 private String url;
17
18 public Pokemon(String name, String url) {
19 this.name = name;
20 this.url = url;
21 }
22
23 public String getName() {
24 return name;
25 }
26
27 public void setName(String name) {
28 this.name = name;
29 }
30
31 public String getUrl() {
32 return url;
33 }
34
35 public void setUrl(String url) {
36 this.url = url;
37 }
38
39 public int getId() {
40 return id;
41 }
42
43 public void setId(int id) {
44 this.id = id;
45 }
46 }
1 package com.example.pokemon.model;
2
3 import java.util.ArrayList;
4
5
6 public class PokemonResponse {
7 private Integer count;
8 private String next,previous;
9 private ArrayList<Pokemon> results;
10
11 public PokemonResponse(Integer count, String next, String previous, ArrayList<Pokemo
12 this.count = count;
13 this.next = next;
14 this.previous = previous;
15 this.results = results;
16 }
17
18 public Integer getCount() {
19 return count;
20 }
21
22 public void setCount(Integer count) {
23 this.count = count;
24 }
25
26 public String getNext() {
27 return next;
28 }
29
30 public void setNext(String next) {
31 this.next = next;
32 }
33
34 public String getPrevious() {
35 return previous;
36 }
37
38 public void setPrevious(String previous) {
39 this.previous = previous;
40 }
41
42 public ArrayList<Pokemon> getResults() {
43 return results;
44 }
45
46 public void setResults(ArrayList<Pokemon> results) {
47 this.results = results;
48 }
49 }
Setting up Hilt
Base Application class: This class is necessary for the hilt and you should
annotate it with @HiltAndroidApp. Don't forget to add it to the manifest
file in the application tag.
<application
android:name=".BaseApplication"
1 package com.example.pokemon;
2
3 import android.app.Application;
4
5 import dagger.hilt.android.HiltAndroidApp;
6
7 /**
8 * Created by Abhinav Singh on 17,June,2020
9 */
10 @HiltAndroidApp
11 public class BaseApplication extends Application {
12 }
Our app will have two fragments one to show the list of pokemon fetched
from the API and the second fragment will show the favorites pokemon
which we have saved using Room and we will use a button to switch
between these fragments.
Setting up Repository
Setting up ViewModel
Think of observable as some entity emitting data but what is the benefit of
observable emitting data if no one is there to observe it so we have
Observers which observes data emitted by the Observables. Operators are
something that manipulates the data or transforms the data and passes it to
the subscribers.
Setting up Room
For room setup, we need three things Entity, Dao, and Database.
Now we will create the abstract database class PokemonDB for storing our
favorites pokemon.
Now we will create DatabaseModule which will help hilt to provide us with
the instance if this database.
We have added the @Module and @InstallIn annotation above this class to
tell hilt that this is a module and it is required for application
cope.@Singleton is used to have a single instance of this database over the
whole app.
Till now we have completed setting up Room, Hilt, and the ViewModel.
Now we will set up the fragments and activity.
Favorites Fragment
We have done the same thing that we have done in the home fragment just
change the swiping direction in ItemTouchHelper.
Main Activity
In the activity, we have used the button to change the fragments. As you can
see we have used view binding here if you are not familiar with view
binding check this it will help you to get your hands on view binding.
abhinav0612/Pokemon
Contribute to abhinav0612/Pokemon development by creating an account on GitHub.
github.com
Now you are able to implement MVVM with Hilt, RxJava, Retrofit, Room
LiveData, and View Binding.
If you want to dive further check out my movie’s info app build using
Navigation Component and all the tech stacks explained above.
If you find this article useful please share it with your friends.
Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read
stories — delivered straight into your inbox, once a week. Take a look.
By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information
about our privacy practices.
Android AndroidDev Android App Development Clean Code Mobile App Development
839 2
WRITTEN BY
Android Developer