0

I am building a very simple Shopping List app using Room and MVVM.

Objective: Animate the quantity textView in recycler according to the action, if increased the slide down, if decreased the slide up.

Problem: Do not know to handle the click event of a button in the recycler view item. Right now the button is calling the interface's method which is being handled by Activity, which calls the ViewModels function which increases the values in the Database. On the bases of that, the recycler view gets updated because it is listing to the changes.

Possible Solution I can think of: Add a Channel of events in ViewModel, and when the add or remove method is called, fire a related event. Observers these events in MainActivity, and then from there call the appropriate adapter function which properly handles the addition or subtraction of quantity items. But don't you think this will generate a ton of boilerplate code?

It's more of an architectural problem than a coding problem.

Please help me out with this. Here is the code of my app: https://github.com/waqas-334/MVVM-TODO-Android-app/tree/bugs

Thank you in advance.

Waqas Younis
  • 84
  • 1
  • 11
  • Handle the animation inside the adapter and pass the event to your activity or fragment to handle business logic after event is over – Vivek Gupta Feb 17 '23 at 01:42
  • Thank you but after I do the animation in the adapter and then update the values in the database, the list will be updated again because it's listening to changes from the database. Would cause 2 animations. – Waqas Younis Feb 18 '23 at 14:57

1 Answers1

0
  • Create a Static MutableLiveData shouldUpdate
  • Observe this boolean. you can trigger it from anywhere in your application, using setValue or postValue.
Djoxy
  • 54
  • 4