2023-04-16 21:41:46 +02:00
|
|
|
package com.denizk0461.studip.adapter
|
|
|
|
|
|
2023-04-27 21:58:10 +02:00
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
|
import androidx.fragment.app.FragmentActivity
|
|
|
|
|
import androidx.viewpager2.adapter.FragmentStateAdapter
|
|
|
|
|
import com.denizk0461.studip.fragment.CanteenPageFragment
|
2023-04-20 08:17:23 +02:00
|
|
|
import com.denizk0461.studip.model.CanteenOfferGroup
|
2023-04-16 21:41:46 +02:00
|
|
|
|
2023-04-28 10:30:30 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/**
|
2023-04-27 21:58:10 +02:00
|
|
|
* Custom ViewPager adapter for managing multiple pages of canteen offers.
|
2023-04-23 11:57:03 +02:00
|
|
|
*
|
2023-04-27 21:58:10 +02:00
|
|
|
* @param fragmentActivity parent fragment activity
|
2023-04-23 22:06:29 +02:00
|
|
|
* @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.
|
2023-04-23 17:13:48 +02:00
|
|
|
* @param onClickListener for managing click and long press events
|
2023-04-23 22:06:29 +02:00
|
|
|
* @param displayAllergens whether the user wants allergens to be marked
|
2023-04-23 11:57:03 +02:00
|
|
|
*/
|
|
|
|
|
class CanteenOfferPageAdapter(
|
2023-04-28 10:30:30 +02:00
|
|
|
val fragmentActivity: FragmentActivity,
|
2023-04-23 11:57:03 +02:00
|
|
|
private var offers: List<CanteenOfferGroup>,
|
|
|
|
|
private var daysCovered: Int,
|
2023-04-23 17:13:48 +02:00
|
|
|
private val onClickListener: CanteenOfferItemAdapter.OnClickListener,
|
2023-04-23 22:06:29 +02:00
|
|
|
private val displayAllergens: Boolean,
|
2023-04-27 21:58:10 +02:00
|
|
|
) : FragmentStateAdapter(fragmentActivity) {
|
2023-04-16 21:41:46 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Returns the count of days covered
|
2023-04-16 21:41:46 +02:00
|
|
|
override fun getItemCount(): Int = daysCovered
|
|
|
|
|
|
2023-04-27 21:58:10 +02:00
|
|
|
override fun createFragment(position: Int): Fragment =
|
|
|
|
|
CanteenPageFragment(
|
|
|
|
|
offers,
|
|
|
|
|
onClickListener,
|
|
|
|
|
displayAllergens,
|
|
|
|
|
position,
|
|
|
|
|
)
|
2023-04-16 21:41:46 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/**
|
|
|
|
|
* Update the entire list of items
|
|
|
|
|
*
|
|
|
|
|
* @param items the new set of items
|
|
|
|
|
*/
|
2023-04-20 08:17:23 +02:00
|
|
|
fun setNewItems(items: List<CanteenOfferGroup>, daysCovered: Int) {
|
2023-04-23 11:57:03 +02:00
|
|
|
// Update the count of days covered
|
2023-04-16 21:41:46 +02:00
|
|
|
this.daysCovered = daysCovered
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
// Update items
|
2023-04-16 21:41:46 +02:00
|
|
|
offers = items
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
// Force an update of the view. TODO replace with individual updates, as this is inefficient
|
2023-04-16 21:41:46 +02:00
|
|
|
notifyDataSetChanged()
|
|
|
|
|
}
|
|
|
|
|
}
|