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/CanteenOfferItemAdapter.kt
T

167 lines
5.9 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.RecyclerView
import com.denizk0461.studip.R
2023-04-16 21:41:46 +02:00
import com.denizk0461.studip.databinding.ItemCanteenBinding
import com.denizk0461.studip.databinding.ItemCanteenLineBinding
import com.denizk0461.studip.databinding.ItemIconBinding
import com.denizk0461.studip.model.CanteenOfferGroup
import com.denizk0461.studip.model.CanteenOfferGroupElement
2023-04-16 21:41:46 +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
* @param onClickListener used for listening to clicks and long presses
* @param displayAllergens whether the user wants allergens to be marked
*/
class CanteenOfferItemAdapter(
private val offers: List<CanteenOfferGroup>,
private val onClickListener: OnClickListener,
private val displayAllergens: Boolean,
) : RecyclerView.Adapter<CanteenOfferItemAdapter.OfferViewHolder>() {
2023-04-16 21:41:46 +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,
)
/**
* 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
)
)
}
// 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
// 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) {
// Retrieve item for current position
2023-04-16 21:41:46 +02:00
val currentItem = offers[position]
// Set category text / header of the item
2023-04-16 21:41:46 +02:00
holder.binding.textCategory.text = currentItem.category
// Create a new view (line) for each item displayed within one category
currentItem.offers.forEach { offer ->
// Inflate the new line without binding it to the line container
val line = ItemCanteenLineBinding.inflate(
LayoutInflater.from(holder.binding.root.context),
)
// Set text values
line.textContent.text = offer.title.addAllergenNotice(displayAllergens, offer.allergens)
line.textPrice.text = offer.price
/*
* 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.
*/
val indices = arrayListOf<Int>()
offer.dietaryPreferences.filterIndexed { index, char ->
if (char == 't') {
indices.add(index)
true
} else false
}
// If no dietary preferences are met, set the view to display a neutral icon
if (indices.isEmpty()) indices.add(10)
// Iterate through all icons that need to be displayed
indices.forEach { index ->
// Inflate a new icon holder
val img = ItemIconBinding.inflate(LayoutInflater.from(holder.binding.root.context))
// Set the appropriate icon
img.imageView.setImageDrawable(
holder.binding.root.context.getDrawable(indexToDrawable[index]!!)
)
// Bind the new icon to the line view
line.imageViewContainer.addView(img.root)
}
// Set up single click listener
line.root.setOnClickListener {
onClickListener.onClick(offer)
}
// Set up long press listener
line.root.setOnLongClickListener {
onClickListener.onLongClick(offer)
}
// Bind the new line to the line container
holder.binding.lineContainer.addView(line.root)
}
2023-04-16 21:41:46 +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*"
}
/**
* Interface used to evaluate clicks and long presses on a given item.
*/
interface OnClickListener {
/**
* Executed when an item has been clicked.
*
* @param offer item that has been clicked
*/
fun onClick(offer: CanteenOfferGroupElement)
/**
* 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
}