DEV Community

Cover image for MVVM architecture for native android app named Top-Rated-Movie-List
Chisthia Khan
Chisthia Khan

Posted on

MVVM architecture for native android app named Top-Rated-Movie-List

This project uses MVVM pattern, Retrofit2-RESTful-api, Live Data, Glide, ModelView, Json and so on.

Find the full project code on my GitHub--> https://github.com/chistia007

At first, you have to create json to pojo object from any website Choose json as source type and Gson as annotation style. Then get the classes from the website and import Json dependency in buld.Gradle(Module:App):implementation'com.squareup.retrofit2:retrofit:2.7.2' implementation 'com.squareup.retrofit2:converter-gson:2.7.2'

1.create classes named Result and MovieModel for Models package under Service package that you got extraxting from josn in the previous step.

public class Result {
    public Boolean getAdult() {
        return adult;
    }
    public void setAdult(Boolean adult) {
        this.adult = adult;
    }
    public String getBackdropPath() {
        return backdropPath;
    }
    public void setBackdropPath(String backdropPath) {
        this.backdropPath = backdropPath;
    }
    public List<Integer> getGenreIds() {
        return genreIds;
    }

    public void setGenreIds(List<Integer> genreIds) {
        this.genreIds = genreIds;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
//  ........................................
//  ........................................

}
Enter fullscreen mode Exit fullscreen mode

Secondly, create an api services and retrofiInstances classes in the network package. Then provide necessary code to call the api.
In the MovieRepository class, we will initialize our repository and fetch data using REST api at the same time according to our need.

public class MovieRepository {
    private static Context mcontext;
    private MovieModel movieModel;
    private List<Result> mResult;
    private static  MovieRepository instance;
    public static MovieRepository getInstance(Context context){
        if (instance==null){
            mcontext=context;
            instance=new MovieRepository();
        }
        return instance;
    }

    public List<Result> getMovieList(){
        ApiServices apiServices= RetrofitInstance.getRetrofitInstance().create(ApiServices.class);
        Call<MovieModel> call= apiServices.getTopRatedMovieList();
        call.enqueue(new Callback<MovieModel>() {
            @Override
            public void onResponse(Call<MovieModel> call, Response<MovieModel> response) {
                movieModel=response.body();
                mResult=movieModel.getResults();
            }

            @Override
            public void onFailure(Call<MovieModel> call, Throwable t) {

            }
        });

        return mResult;
    }

}
Enter fullscreen mode Exit fullscreen mode

Then we will add Mutablelive data in the same class.

public class MovieRepository {
    private static Context mcontext;
    private MovieModel movieModel;
    private List<Result> mResult;
    private MutableLiveData mutableLiveData;
    private static  MovieRepository instance;
    public static MovieRepository getInstance(Context context){
        if (instance==null){
            mcontext=context;
            instance=new MovieRepository();
        }
        return instance;
    }

    public MutableLiveData<List<Result>>  getTopRatedMovieLists(){
        if(mutableLiveData==null){
            mutableLiveData=new MutableLiveData();
        }

        ApiServices apiServices= RetrofitInstance.getRetrofitInstance().create(ApiServices.class);
        Call<MovieModel> call= apiServices.getTopRatedMovieList();
        call.enqueue(new Callback<MovieModel>() {
            @Override
            public void onResponse(Call<MovieModel> call, Response<MovieModel> response) {
                movieModel=response.body();
                mResult=movieModel.getResults();
                mutableLiveData.postValue(mResult);
            }

            @Override
            public void onFailure(Call<MovieModel> call, Throwable t) {

            }
        });

        return mutableLiveData;
    }
}
Enter fullscreen mode Exit fullscreen mode

Again, we fetch the live data in ViewModel class from repository. this MovieListViewModel class will hold data until the activity is finised. it will even preverse data in onResume() mode.

public class MovieListViewModel extends AndroidViewModel {

    private MovieRepository movieRepository;

    public MovieListViewModel(@NonNull Application application) {
        super(application);
        movieRepository= MovieRepository.getInstance(application);
    }
    public LiveData<List<Result>> getTopRatedMovieLists(){
        return movieRepository.getTopRatedMovieLists();
    }
}
Enter fullscreen mode Exit fullscreen mode

Finally, we add an observer in Mainactivity class under View package. if any data in the api gets changed, it will change it in the repository, and ViewModel will catch the changed in data. Moreover, the observer will keep observing. Whenever any data gets changed in the ViewModel, observer will get notified and it will update the UI(REcyclerView In our case) by itself.

movieListViewModel.getTopRatedMovieLists().observe(this, new Observer<List<Result>>() {

            @Override
            public void onChanged(List<Result> results) {

                movieListAdapter=new MovieListAdapter(MainActivity.this,results);
                recyclerView.setAdapter(movieListAdapter);

            }
        });

    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)