java - What should be done in Activity/Fragment and ViewModel in MVVM -
our company has been developing android application using mvp pattern while. mvp, put business logic inside presenter , activity/fragment responsible view update when receiving event callback presenter.
now, decided try mvvm using android databinding. seems mvvm, can put business logic in viewmodel (just presenter in mvp) , notify view(s) of changes data model, in 1 object.
but then, raise question in our mind, should left handle activity/fragment? since adopted mvp pattern avoid fat-activity/fragment. don't want have slim-activity/fragment , fat-viewmodel.
what think can left handle activity/fragment far
- request/check permission
- access context
- access resources
every correction, comment or suggestion welcome since i'm new mvvm, if seems similar mvp.
thank you.
a bit more question
is possible , practice combine mvvm listener (like mvp)? example
public class mainactivityviewmodel extends baseobservable { mainactivityviewmodellistener listener; user user; public void setmainactivityviewmodellistener(mainactivityviewmodellistener listener) { this.listener = listener; } public void refreshuser(view v) { // 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) { // update } }
yes can have business logic in viewmodel, here links personaly follows learn mvvm
approaching android mvvm
https://github.com/ivacf/archi
mvvm on android: need know
you can mention listeners in viewmodel data model consist.
viewmodel
alters content , notifies binding framework content has changed.
model - data model containing business , validation logic
view - defines structure, layout , appearance of view on screen
viewmodel - acts link between view , model, dealing view logic
Comments
Post a Comment