Implemented user setting for displaying allergens next to an offer

This commit is contained in:
denizk0461
2023-04-23 22:06:29 +02:00
parent 8d6a15c4d5
commit d654ba8a03
7 changed files with 94 additions and 36 deletions
@@ -15,10 +15,12 @@ import com.denizk0461.studip.model.CanteenOfferGroupElement
*
* @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 onClickListener: OnClickListener,
private val displayAllergens: Boolean,
) : RecyclerView.Adapter<CanteenOfferItemAdapter.OfferViewHolder>() {
/**
@@ -75,7 +77,7 @@ class CanteenOfferItemAdapter(
)
// Set text values
line.textContent.text = offer.title
line.textContent.text = offer.title.addAllergenNotice(displayAllergens, offer.allergens)
line.textPrice.text = offer.price
/*
@@ -121,6 +123,26 @@ class CanteenOfferItemAdapter(
}
}
/**
* 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.
*/
@@ -11,17 +11,19 @@ import com.denizk0461.studip.model.CanteenOfferGroup
* 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.
* @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.
* @param onClickListener for managing click and long press events
* @param displayAllergens whether the user wants allergens to be marked
*/
class CanteenOfferPageAdapter(
private var offers: List<CanteenOfferGroup>,
private var daysCovered: Int,
private val onClickListener: CanteenOfferItemAdapter.OnClickListener,
private val displayAllergens: Boolean,
) : RecyclerView.Adapter<CanteenOfferPageAdapter.CanteenOfferPageViewHolder>() {
/**
@@ -47,7 +49,9 @@ class CanteenOfferPageAdapter(
holder.binding.pageRecyclerView.apply {
// Set page to horizontally scroll
layoutManager = LinearLayoutManager(holder.binding.root.context, LinearLayoutManager.VERTICAL, false)
layoutManager = LinearLayoutManager(
holder.binding.root.context, LinearLayoutManager.VERTICAL, false
)
/*
* Create new adapter for every page. Attribute position denotes day that will be set up
@@ -56,7 +60,8 @@ class CanteenOfferPageAdapter(
*/
adapter = CanteenOfferItemAdapter(
offers.filter { it.dateId == position },
onClickListener
onClickListener,
displayAllergens,
)
// Animate creation of new page