2023-04-16 21:41:46 +02:00
|
|
|
package com.denizk0461.studip.adapter
|
|
|
|
|
|
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
|
import android.view.ViewGroup
|
2023-04-24 11:21:52 +02:00
|
|
|
import androidx.appcompat.content.res.AppCompatResources
|
2023-04-16 21:41:46 +02:00
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
|
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-23 17:13:48 +02:00
|
|
|
import com.denizk0461.studip.model.CanteenOfferGroupElement
|
2023-04-24 11:21:52 +02:00
|
|
|
import com.denizk0461.studip.model.DietaryPreferences
|
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
|
2023-04-23 17:13:48 +02:00
|
|
|
* @param onClickListener used for listening to clicks and long presses
|
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 CanteenOfferItemAdapter(
|
2023-04-23 17:13:48 +02:00
|
|
|
private val offers: List<CanteenOfferGroup>,
|
2023-04-23 22:06:29 +02:00
|
|
|
private val onClickListener: OnClickListener,
|
|
|
|
|
private val displayAllergens: Boolean,
|
2023-04-23 11:57:03 +02:00
|
|
|
) : 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-23 22:06:29 +02:00
|
|
|
line.textContent.text = offer.title.addAllergenNotice(displayAllergens, offer.allergens)
|
2023-04-20 10:05:24 +02:00
|
|
|
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(
|
2023-04-24 11:21:52 +02:00
|
|
|
AppCompatResources.getDrawable(
|
|
|
|
|
holder.binding.root.context,
|
|
|
|
|
DietaryPreferences.indexToDrawable[index]!!,
|
|
|
|
|
)
|
2023-04-23 11:57:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Bind the new icon to the line view
|
2023-04-20 08:17:23 +02:00
|
|
|
line.imageViewContainer.addView(img.root)
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-23 17:13:48 +02:00
|
|
|
// Set up single click listener
|
|
|
|
|
line.root.setOnClickListener {
|
2023-04-24 11:21:52 +02:00
|
|
|
onClickListener.onClick(offer, currentItem.category)
|
2023-04-23 17:13:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set up long press listener
|
|
|
|
|
line.root.setOnLongClickListener {
|
|
|
|
|
onClickListener.onLongClick(offer)
|
|
|
|
|
}
|
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 22:06:29 +02:00
|
|
|
/**
|
|
|
|
|
* Adds a star to mark allergens present in an offer. Allergens will be displayed as a star '*'
|
|
|
|
|
* next to the title.
|
|
|
|
|
*
|
|
|
|
|
* @param displayAllergens whether the user wants allergens to be displayed
|
|
|
|
|
* @param allergens allergens present in the offer
|
|
|
|
|
* @return the formatted string
|
|
|
|
|
*/
|
|
|
|
|
private fun String.addAllergenNotice(displayAllergens: Boolean, allergens: String): String {
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Don't add anything to the string if the user does not want a star added, or if the item
|
|
|
|
|
* does not contain any allergens or additives.
|
|
|
|
|
*/
|
|
|
|
|
if (!displayAllergens || allergens.isBlank()) return this
|
|
|
|
|
|
|
|
|
|
// Add a star to the string
|
|
|
|
|
return "$this*"
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/**
|
2023-04-23 17:13:48 +02:00
|
|
|
* Interface used to evaluate clicks and long presses on a given item.
|
2023-04-23 11:57:03 +02:00
|
|
|
*/
|
2023-04-23 17:13:48 +02:00
|
|
|
interface OnClickListener {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Executed when an item has been clicked.
|
|
|
|
|
*
|
|
|
|
|
* @param offer item that has been clicked
|
|
|
|
|
*/
|
2023-04-24 11:21:52 +02:00
|
|
|
fun onClick(offer: CanteenOfferGroupElement, category: String)
|
2023-04-23 17:13:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Executed when an item has been long-pressed.
|
|
|
|
|
*
|
|
|
|
|
* @param offer item that has been long-pressed
|
|
|
|
|
* @return whether the long press was successful
|
|
|
|
|
*/
|
|
|
|
|
fun onLongClick(offer: CanteenOfferGroupElement): Boolean
|
|
|
|
|
}
|
2023-04-16 21:41:46 +02:00
|
|
|
}
|