2023-04-16 21:41:46 +02:00
|
|
|
package com.denizk0461.studip.adapter
|
|
|
|
|
|
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
|
import android.view.ViewGroup
|
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
|
import com.denizk0461.studip.databinding.ItemScrollablePageBinding
|
2023-04-20 08:17:23 +02:00
|
|
|
import com.denizk0461.studip.model.CanteenOfferGroup
|
2023-04-16 21:41:46 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/**
|
|
|
|
|
* Custom RecyclerView adapter for managing multiple pages of canteen offers in a RecyclerView or
|
|
|
|
|
* ViewPager.
|
|
|
|
|
*
|
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(
|
|
|
|
|
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-23 11:57:03 +02:00
|
|
|
) : RecyclerView.Adapter<CanteenOfferPageAdapter.CanteenOfferPageViewHolder>() {
|
2023-04-16 21:41:46 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/**
|
|
|
|
|
* View holder class for parent class
|
|
|
|
|
*
|
|
|
|
|
* @param binding view binding object
|
|
|
|
|
*/
|
2023-04-16 21:41:46 +02:00
|
|
|
class CanteenOfferPageViewHolder(val binding: ItemScrollablePageBinding) : RecyclerView.ViewHolder(binding.root)
|
|
|
|
|
|
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CanteenOfferPageViewHolder =
|
|
|
|
|
CanteenOfferPageViewHolder(
|
|
|
|
|
ItemScrollablePageBinding.inflate(
|
|
|
|
|
LayoutInflater.from(parent.context),
|
|
|
|
|
parent,
|
|
|
|
|
false
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
override fun onBindViewHolder(holder: CanteenOfferPageViewHolder, position: Int) {
|
|
|
|
|
|
|
|
|
|
holder.binding.pageRecyclerView.apply {
|
2023-04-23 11:57:03 +02:00
|
|
|
// Set page to horizontally scroll
|
2023-04-23 22:06:29 +02:00
|
|
|
layoutManager = LinearLayoutManager(
|
|
|
|
|
holder.binding.root.context, LinearLayoutManager.VERTICAL, false
|
|
|
|
|
)
|
2023-04-16 21:41:46 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/*
|
|
|
|
|
* Create new adapter for every page. Attribute position denotes day that will be set up
|
|
|
|
|
* by the newly created adapter (0 = Monday, 4 = Friday)
|
2023-04-23 17:13:48 +02:00
|
|
|
* TODO check if the filtered list is empty, and tell the user if it is
|
2023-04-23 11:57:03 +02:00
|
|
|
*/
|
2023-04-23 17:13:48 +02:00
|
|
|
adapter = CanteenOfferItemAdapter(
|
|
|
|
|
offers.filter { it.dateId == position },
|
2023-04-23 22:06:29 +02:00
|
|
|
onClickListener,
|
|
|
|
|
displayAllergens,
|
2023-04-23 17:13:48 +02:00
|
|
|
)
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
// Animate creation of new page
|
2023-04-16 21:41:46 +02:00
|
|
|
scheduleLayoutAnimation()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
}
|