This repository has been archived on 2026-05-26. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
weserplaner/app/src/main/java/com/denizk0461/studip/adapter/CanteenOfferPageAdapter.kt
T

76 lines
2.7 KiB
Kotlin
Raw Normal View History

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
import com.denizk0461.studip.model.CanteenOfferGroup
2023-04-16 21:41:46 +02:00
/**
* Custom RecyclerView adapter for managing multiple pages of canteen offers in a RecyclerView or
* ViewPager.
*
* @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.
*/
class CanteenOfferPageAdapter(
private var offers: List<CanteenOfferGroup>,
private var daysCovered: Int,
) : RecyclerView.Adapter<CanteenOfferPageAdapter.CanteenOfferPageViewHolder>() {
2023-04-16 21:41:46 +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
)
)
// 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 {
// Set page to horizontally scroll
2023-04-16 21:41:46 +02:00
layoutManager = LinearLayoutManager(holder.binding.root.context, LinearLayoutManager.VERTICAL, false)
/*
* Create new adapter for every page. Attribute position denotes day that will be set up
* by the newly created adapter (0 = Monday, 4 = Friday)
*/
adapter = CanteenOfferItemAdapter(offers.filter { it.dateId == position }) // TODO check if empty
// Animate creation of new page
2023-04-16 21:41:46 +02:00
scheduleLayoutAnimation()
}
}
/**
* 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
2023-04-16 21:41:46 +02:00
this.daysCovered = daysCovered
// Update items
2023-04-16 21:41:46 +02:00
offers = items
// Force an update of the view. TODO replace with individual updates, as this is inefficient
2023-04-16 21:41:46 +02:00
notifyDataSetChanged()
}
}