Hi guys! I have a question regarding MVVM. Let’s ...
# android
j
Hi guys! I have a question regarding MVVM. Let’s say I have a ViewPager that has 3 pages and has automatic scrolling feature. (this viewpager goes back to page 1 when reaching page 3). The auto scrolling feature is PAUSED everytime the viewpager receives a touch action. Now, should the logic of setting view pager’s current item to 1 if it reaches 3, and the pausing of auto scroll when view pager receives touch event RESIDE in ViewModel? In my understanding. Purely UI logic can stay in activity/fragment. Logic that can reside in ViewModel can include enabling/disabling buttons (depending on the input), validating inputs, etc. Any inputs are greatly appreciated!
m
Why no simple smile here?
i
it should be handled by view
m
It can be situational - for example if you’re using databinding and passing VM to assign fields to views, it’s not uncommon VM has view related methods. IMO activity / fragment should handle interaction but not logic of any kind.
👍 1
c
I would personally fire an event from the UI to the view model when the Scroll reaches the bottom and then the view model determining the logic states to publish to the view an event to go up to the view telling it to scroll to the top…that way you can set up unit test on your view model as well as keep all logic in one place
👍 2
j
Good point @Camilo
Thanks for the response guys. Appreciated!