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
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
}
}
The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations.
In android, we can use ViewModel to share data between various fragments or activities by sharing the same ViewModel among all the fragments and they can access everything defined in the ViewModel. This is one way to have communication between fragments or activities.
Fragments introduce modularity and reusability into your activity's UI by allowing you to divide the UI into discrete chunks. Activities are an ideal place to put global elements around your app's user interface, such as a navigation drawer.
Use activity's scope to create an instance of the ViewModel in fragment so that all the fragments and activity now has a common ViewModel and have access to the same ViewModelStore. If the activity is re-created, it and all its fragments receive the same ViewModel instance that was created by the associated activity.
Model — View — ViewModel (MVVM) is the industry-recognized software architecture pattern that overcomes all drawbacks of MVP and MVC design patterns. MVVM suggests separating the data presentation logic (Views or UI) from the core business logic part of the application. The separate code layers of MVVM are:
Model — View — ViewModel (MVVM) is the industry-recognized software architecture pattern that overcomes all drawbacks of MVP and MVC design patterns. MVVM suggests separating the data presentation logic (Views or UI) from the core business logic part of the application.
The separate code layers of MVVM are: 1 Model: This layer is responsible for the abstraction of the data sources. Model and ViewModel work together to get and save the data. 2 View: The purpose of this layer is to inform the ViewModel about the user’s action. ... 3 ViewModel: It exposes those data streams which are relevant to the View. ...
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
reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With