Edited canteen views to have direct database access in the CanteenPageFragment.kt, allowing for filtering the individual days through the database; filtering now works (with changes animated!!) through updating a LiveData and observing it in the page fragment

This commit is contained in:
denizk0461
2023-04-28 22:44:23 +02:00
parent 509a0434c5
commit 8fb41d6520
11 changed files with 320 additions and 193 deletions
@@ -4,53 +4,29 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.denizk0461.studip.fragment.CanteenPageFragment
import com.denizk0461.studip.model.CanteenOfferGroup
/**
* Custom ViewPager adapter for managing multiple pages of canteen offers.
*
* @param fragmentActivity parent fragment activity
* @param offers all offers for a given canteen, grouped by category (not filtered by day at
* this point)
* @param daysCovered tells for how many days offers are available for.
* Example: if the next two weeks are available, Monday through Friday and
* excluding weekends, this value should be 10.
* @param onClickListener for managing click and long press events
* @param displayAllergens whether the user wants allergens to be marked
*/
class CanteenOfferPageAdapter(
fragmentActivity: FragmentActivity,
private var offers: List<CanteenOfferGroup>,
private var daysCovered: Int,
private val onClickListener: CanteenOfferItemAdapter.OnClickListener,
private val displayAllergens: Boolean,
) : FragmentStateAdapter(fragmentActivity) {
private var count: Int = 0
// Returns the count of days covered
override fun getItemCount(): Int = daysCovered
override fun getItemCount(): Int = count
override fun createFragment(position: Int): Fragment =
CanteenPageFragment(
offers,
onClickListener,
displayAllergens,
position,
)
/**
* Update the entire list of items
*
* @param items the new set of items
*/
fun setNewItems(items: List<CanteenOfferGroup>, daysCovered: Int) {
// Update the count of days covered
this.daysCovered = daysCovered
// Update items
offers = items
// Force an update of the view. TODO replace with individual updates, as this is inefficient
fun setItemCount(newCount: Int) {
count = newCount
notifyDataSetChanged()
}
}