Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android MVVM without Databinding

Question : Can I implement android app with MVVM without using Databinding.

Problem I am trying to solve is pretty simple: read a list of items from backend API and show them in a Recylerview.

How I am implementing:

In the View - I have Activity and RecyclerViewAdapter Model : ApiResponse and data models network - retrofit API service, RxJava2

for ViewModel part - I have a ViewModel class(that doesn't derive from anything) that basically calls Retrofit Service and gets data using RxJava calls.

ViewModel has calls such as :

 void getItems();
 void addItemData();
 void removeItem();

which call service with RXJava2 as

 ObServable<ApiResponse> getItems();
 ObServable<ApiResponse> addItemData();
 ObServable<ApiResponse> removeItem();  

View instantiates ViewModel object. ViewModel gets the instance of Adapter object during creation. In the View, clicking a button calls a ClickHandler in the Activity which calls a ViewModel#getItems() method. Since ViewModel has link to Adapter, the viewModel updates the items in the adapter so that RecyclerView is automatically updated.

I am not sure if this is right approach for MVVM.

Databinding seems a bit like spaghetti to me.

Again, can we implement MVVM in android without DataBinding ? Is the approach OK?

like image 906
tinker Avatar asked Jan 17 '18 03:01

tinker


People also ask

Is databinding part of MVVM?

We've already implemented MVVM using Data Binding and covered LiveData and Data Binding in separate tutorials. Today, we'll use LiveData with Data Binding in our MVVM Android Application. We'll see how LiveData makes it easy to update the UI from the ViewModel.

What is data binding in MVVM Android?

This means it can hold a set of data that can be observed from other Android components like Activities , Fragments , and Services . It's lifecycle-aware and mostly used to update UI from the ViewModel in MVVM architecture projects.

Does Android use MVVM?

Note: You can combine Clean Architecture with the model-view-presenter (MVP) architecture as well. But since Android Architecture Components already provides a built-in ViewModel class, we are going with MVVM over MVP—no MVVM framework required!

What is binding in MVVM?

Data Binding allows you to effortlessly communicate across views and data sources. This pattern is important for many Android designs, including model view ViewModel (MVVM), which is currently one of the most common Android architecture patterns.


1 Answers

Yes! You can. But i think your approach can be better. Remember that the view model must not have a reference to your view. ViewModel expose observables, and in your view, you should observe those observables and react over changes. You can have something like this:

Note: This example is with Kotlin and LiveData because, why not? But you can take this and use it with Java & Rx

ItemsViewModel : ViewModel() {
  private val items = MutableLiveData<List<Items>>()

  fun getAllItems() : LiveData<List<Items>> {
      return items
    }

   //..
}

ItemsActivity : Activity() {
  private var itemsAdapter: ItemsAdapter? = null
  private var viewModel: ItemsViewModel? = null

  override fun onCreate(savedInstance: Bundle) {
  // ...

   // Create your Adapter
   itemsAdapter = ItemsAdapter()
   recyclerView.adapter = itemsAdapter

   // Create and observe your view model
   viewModel =  ViewModelProviders.of(this).get(ItemsViewModel::class.java)

   viewModel.getAllItems().observe(this, Observer {
      it?.let {
          adapter?.datasource = it
      }
}

In this case, the view observes view model, and notify the adapter. Then in your adapter, you do the bind as usual, without databinding.

like image 72
Federico Heiland Avatar answered Oct 01 '22 00:10

Federico Heiland