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.RecyclerView
|
2023-04-23 11:57:03 +02:00
|
|
|
import com.denizk0461.studip.R
|
2023-04-16 21:41:46 +02:00
|
|
|
import com.denizk0461.studip.databinding.ItemCanteenBinding
|
2023-04-18 21:33:24 +02:00
|
|
|
import com.denizk0461.studip.databinding.ItemCanteenLineBinding
|
|
|
|
|
import com.denizk0461.studip.databinding.ItemIconBinding
|
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 that lays out the offers of a given canteen on a given day.
|
|
|
|
|
*
|
|
|
|
|
* @param offers list of all offers filtered by canteen and day, grouped by category
|
|
|
|
|
*/
|
|
|
|
|
class CanteenOfferItemAdapter(
|
|
|
|
|
private val offers: List<CanteenOfferGroup>
|
|
|
|
|
) : RecyclerView.Adapter<CanteenOfferItemAdapter.OfferViewHolder>() {
|
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 OfferViewHolder(val binding: ItemCanteenBinding) : RecyclerView.ViewHolder(binding.root)
|
|
|
|
|
|
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OfferViewHolder {
|
|
|
|
|
return OfferViewHolder(
|
|
|
|
|
ItemCanteenBinding.inflate(
|
|
|
|
|
LayoutInflater.from(parent.context),
|
|
|
|
|
parent,
|
|
|
|
|
false
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Returns the amount of offers for a given canteen and day
|
2023-04-16 21:41:46 +02:00
|
|
|
override fun getItemCount(): Int = offers.size
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// TODO inflate a view saying "no events" if none are available for a given day
|
2023-04-16 21:41:46 +02:00
|
|
|
override fun onBindViewHolder(holder: OfferViewHolder, position: Int) {
|
2023-04-23 11:57:03 +02:00
|
|
|
// Retrieve item for current position
|
2023-04-16 21:41:46 +02:00
|
|
|
val currentItem = offers[position]
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Set category text / header of the item
|
2023-04-16 21:41:46 +02:00
|
|
|
holder.binding.textCategory.text = currentItem.category
|
2023-04-18 21:33:24 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Create a new view (line) for each item displayed within one category
|
2023-04-20 08:17:23 +02:00
|
|
|
currentItem.offers.forEach { offer ->
|
2023-04-23 11:57:03 +02:00
|
|
|
// Inflate the new line without binding it to the line container
|
2023-04-20 08:17:23 +02:00
|
|
|
val line = ItemCanteenLineBinding.inflate(
|
|
|
|
|
LayoutInflater.from(holder.binding.root.context),
|
|
|
|
|
)
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Set text values
|
2023-04-20 10:05:24 +02:00
|
|
|
line.textContent.text = offer.title
|
|
|
|
|
line.textPrice.text = offer.price
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Check which dietary preferences are met by this item. This is used to set visual
|
|
|
|
|
* icons on each line. Example: a leaf on vegan items.
|
|
|
|
|
*/
|
2023-04-20 08:17:23 +02:00
|
|
|
val indices = arrayListOf<Int>()
|
|
|
|
|
offer.dietaryPreferences.filterIndexed { index, char ->
|
|
|
|
|
if (char == 't') {
|
|
|
|
|
indices.add(index)
|
|
|
|
|
true
|
|
|
|
|
} else false
|
|
|
|
|
}
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
// If no dietary preferences are met, set the view to display a neutral icon
|
2023-04-20 08:17:23 +02:00
|
|
|
if (indices.isEmpty()) indices.add(10)
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
// Iterate through all icons that need to be displayed
|
2023-04-20 08:17:23 +02:00
|
|
|
indices.forEach { index ->
|
2023-04-23 11:57:03 +02:00
|
|
|
// Inflate a new icon holder
|
2023-04-20 08:17:23 +02:00
|
|
|
val img = ItemIconBinding.inflate(LayoutInflater.from(holder.binding.root.context))
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
// Set the appropriate icon
|
|
|
|
|
img.imageView.setImageDrawable(
|
|
|
|
|
holder.binding.root.context.getDrawable(indexToDrawable[index]!!)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Bind the new icon to the line view
|
2023-04-20 08:17:23 +02:00
|
|
|
line.imageViewContainer.addView(img.root)
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Bind the new line to the line container
|
2023-04-20 08:17:23 +02:00
|
|
|
holder.binding.lineContainer.addView(line.root)
|
|
|
|
|
}
|
2023-04-16 21:41:46 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/**
|
|
|
|
|
* Provides a converting function between the index of a dietary preference and its according
|
|
|
|
|
* icon.
|
|
|
|
|
*/
|
|
|
|
|
private val indexToDrawable: Map<Int, Int> = mapOf(
|
|
|
|
|
0 to R.drawable.handshake,
|
|
|
|
|
1 to R.drawable.fish,
|
|
|
|
|
2 to R.drawable.chicken,
|
|
|
|
|
3 to R.drawable.sheep,
|
|
|
|
|
4 to R.drawable.yoga,
|
|
|
|
|
5 to R.drawable.cow,
|
|
|
|
|
6 to R.drawable.pig,
|
|
|
|
|
7 to R.drawable.leaf,
|
|
|
|
|
8 to R.drawable.carrot,
|
|
|
|
|
9 to R.drawable.deer,
|
|
|
|
|
10 to R.drawable.circle,
|
|
|
|
|
)
|
2023-04-16 21:41:46 +02:00
|
|
|
}
|