A-short-name / G01-Timebanking

0 stars 0 forks source link

Show animation when some changes occured from the db #38

Closed lelexdrugo closed 2 years ago

lelexdrugo commented 2 years ago

Implement the animation that give user hint when changes are retrived from firestore. Use something like this

"Implementiamo una classe che estenda DiffUtil.Callback per far sì di avere le animazioni. Bisogna calcolare la differenza fra le due liste (la vecchia e la nuova ottenuta dal db) tramite l'algoritmo di Myers"

    fun addFilter(on:Boolean){
        filter = on
        val newData = if(filter){
            displayData = data.filter{it.id%2==0}.toMutableList()
        } else
            displayData = data.toMutableList()

        val diffs = DiffUtil.calculateDiff(MyDiffCallback(displayData, newData))

        displayData = newData
        diffs.dispatchUpdatedTo(this)

    }

class MyDiffCallback(val old: List<Item>, val new: List<Item>): DiffUtil.Callback(){
    ... implementazione dei membri...
    old.size
    new.size
    areItems -> possiamo usare gli id o gli indirizzi in memoira
    areContents -> ==
}

So, creating listener (TimeSlotListBySkillViewModel -> setAdvertisementsBySkill and TimeSlotListViewModel -> getAdvertisementList) we have also to calculate diffs using the old value contained in the ViewModel (mAdvList) and the new one (advertisements)

lelexdrugo commented 2 years ago

Slide 28 & 29 from a07-lists_adapters image image

lelexdrugo commented 2 years ago

https://medium.com/android-news/smart-way-to-update-recyclerview-using-diffutil-345941a160e0

It seems like the diffs have to be calculated in the set method of the adpater.

This method change the initial setted value (the list of data that fill the recycler view). Until now we never change this value: because we destroy and recreate a noe recycler view each time a chnage in the observed variable occured. Choose which way to follow and dispatchUpdate for animation accordingly

lelexdrugo commented 2 years ago

For example in the TimeSlotListBySkillFragment

timeSlotListBySkillViewModel.advList.observe(this.viewLifecycleOwner){
            if (it.isEmpty()) {
                recyclerViewAdv.visibility = View.GONE
                emptyAdvText.visibility = View.VISIBLE
            } else {
                recyclerViewAdv.visibility = View.VISIBLE
                emptyAdvText.visibility = View.GONE
            }

            val adapter = AdvertisementAdapter(it, timeSlotDetailsViewModel, true)
            recyclerViewAdv.adapter = adapter
        }

If we create the adapter before and when the observer is triggered we only call the adapter.setSkills(it) I think that it will works