alexfinnarn / swole-nation-android

Make America Swole Again
0 stars 0 forks source link

Add WorkoutList ViewModel #11

Open alexfinnarn opened 5 years ago

alexfinnarn commented 5 years ago

Now that the main fragment can link to other fragments, data needs to be loaded in the fragments navigated to.

Android suggests creating ViewModels for this purpose. You can use listeners in the main activity to load data when the fragments are switched, but it is cleaner to use a ViewModel.

I am only creating one for the list of Workouts so I can figure out the entire flow before I try to do the same thing for Exercises.

https://developer.android.com/topic/libraries/architecture/viewmodel.html

alexfinnarn commented 5 years ago

The first thing I did was take out all the references to OnListFragmentInteractionListener. Using that interface, the fragment could communicate to other fragments and also provided context to MyListWorkoutRecyclerViewAdapter when interactions were made.

After removing the references, the navigation still works as expected.

alexfinnarn commented 5 years ago

But wait...I put that back in after struggling to convert the generated RecyclerView.Adapter into a ListAdapter.

The ListAdapter takes care of animations and is more performant. I actually noticed this on the DummyContent example included in the generated code. It was choppy when scrolling since I think the whole list was being re-drawn with every scroll.