11

Our company has been developing Android application using MVP pattern a while. With MVP, we put all business logic inside the presenter and the Activity/Fragment then just responsible for view update when receiving event callback from presenter.

Now, we decided to try MVVM using Android Databinding. It seems that with MVVM, we can put all the business logic in the ViewModel (just like Presenter in MVP) and also notify the view(s) of any changes to data model, all in one object.

But then, this raise question in our mind, what should we left to be handle by the Activity/Fragment? Since we adopted the MVP pattern to avoid fat-activity/fragment. We don't want to have slim-activity/fragment and then fat-viewmodel.

What we think we can left to be handle by Activity/Fragment so far

  • Request/Check permission
  • Access Context
  • Access Resources

Every correction, comment or suggestion are welcome since I'm fairly new to MVVM, even if it seems to be similar to MVP.

Thank you.

A bit more question

Is it possible and good practice to combine MVVM with listener (like MVP)? For example

public class MainActivityViewModel extends BaseObservable {

    MainActivityViewModelListener listener;
    User user;

    public void setMainActivityViewModelListener(MainActivityViewModelListener listener) {
        this.listener = listener;
    }

    public void refreshUser(View v) {
        // some user update via Internet
        notifyPropertyChanged(BR.userAlias);

        if (listener != null) {
            listener.onUserRefreshed(user);
        }
    }

    @Bindable
    public void getUserAlias() {
        return user.getAlias();
    }
}

public interface MainActivityViewModelListener {
    void onUserRefreshed(User user);
}

public class MainActivity implements MainActivityViewModelListener {

    MainActivityBinding binding;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        binding = DataBindingUtil.setContentView(R.layout.main_activity);


        MainActivityViewModel viewModel = new MainActivityViewModel();
        viewModel.setMainActivityViewModelListener(this);
        binding.setMainActivityViewModel(viewModel);
    }

    @Override
    public void onUserRefreshed(User user) {
        // do some update
    }
}
Tar_Tw45
  • 3,122
  • 6
  • 35
  • 58

3 Answers3

9

Yes you can have all business logic in your ViewModel, Here are some links which i personaly follows to learn MVVM

Approaching Android with MVVM
https://github.com/ivacf/archi
MVVM on Android: What You Need to Know

You can mention all your listeners in ViewModel as well as data which your model will consist.

ViewModel alters some content and notifies the binding framework that content has changed.

Model - Data model containing business and validation logic
View - Defines the structure, layout and appearance of a view on screen
ViewModel - Acts a link between the View and Model, dealing with any view logic

enter image description here

reference

Community
  • 1
  • 1
Ravi
  • 34,851
  • 21
  • 122
  • 183
  • +1 Github link. I went through several tutorials and try one implementation sample so far, the project example will helps a lot! – Tar_Tw45 Nov 08 '16 at 11:42
  • Hey! @RaviRupareliya, the fragment transactions and animations should be done on the Activity? – george_mx Dec 21 '16 at 20:15
0

You should not set the Listener in the Activity.

Logic should be written as far as possible into the ViewModel.

I wrote a demo of MVVM(Databinding) a while ago.

Hope it helps you:

https://github.com/adgvcxz/Dribbble-MVVM

adgvcxz
  • 341
  • 2
  • 6
0

The answer for your question that can you use interface listeners inside mvvm just like you do in mvp? is yes but pattern is little different the code u mentioned

public interface MainActivityViewModelListener {
void onUserRefreshed(User user);

is ok for mvp type designs but for mvvm you should use proper observer register and unregister pattern including notifying observers.

in mvp we directly call an interface function but observer pattern in mvvm is quite different from these simple interfaces. Observer pattern involve Subject registration with client class.

if you want to how exactly Mvvm works see here https://github.com/saksham24/Android-Firebase-Mvp-Mvc-Mvvm-chat

this is a simple application with same functionality but written in three different formats to give a clear idea of difference between mvp mvvm and mvc

saksham
  • 3,173
  • 1
  • 21
  • 23