Added further information to be shown in AllergenSheet.kt

This commit is contained in:
denizk0461
2023-04-24 11:21:52 +02:00
parent 6f5df7dd55
commit b938352c53
8 changed files with 164 additions and 42 deletions
@@ -2,13 +2,14 @@ package com.denizk0461.studip.adapter
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.content.res.AppCompatResources
import androidx.recyclerview.widget.RecyclerView
import com.denizk0461.studip.R
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
import com.denizk0461.studip.model.DietaryPreferences
/**
* Custom RecyclerView adapter that lays out the offers of a given canteen on a given day.
@@ -23,24 +24,6 @@ class CanteenOfferItemAdapter(
private val displayAllergens: Boolean,
) : RecyclerView.Adapter<CanteenOfferItemAdapter.OfferViewHolder>() {
/**
* 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
*
@@ -102,7 +85,10 @@ class CanteenOfferItemAdapter(
// Set the appropriate icon
img.imageView.setImageDrawable(
holder.binding.root.context.getDrawable(indexToDrawable[index]!!)
AppCompatResources.getDrawable(
holder.binding.root.context,
DietaryPreferences.indexToDrawable[index]!!,
)
)
// Bind the new icon to the line view
@@ -111,7 +97,7 @@ class CanteenOfferItemAdapter(
// Set up single click listener
line.root.setOnClickListener {
onClickListener.onClick(offer)
onClickListener.onClick(offer, currentItem.category)
}
// Set up long press listener
@@ -153,7 +139,7 @@ class CanteenOfferItemAdapter(
*
* @param offer item that has been clicked
*/
fun onClick(offer: CanteenOfferGroupElement)
fun onClick(offer: CanteenOfferGroupElement, category: String)
/**
@@ -279,8 +279,8 @@ class CanteenFragment : AppFragment(), CanteenOfferItemAdapter.OnClickListener {
*
* @param offer item that has been clicked
*/
override fun onClick(offer: CanteenOfferGroupElement) {
openBottomSheet(AllergenSheet(offer))
override fun onClick(offer: CanteenOfferGroupElement, category: String) {
openBottomSheet(AllergenSheet(offer, category))
}
/**
@@ -1,5 +1,7 @@
package com.denizk0461.studip.model
import com.denizk0461.studip.R
/**
* Enumeration that can be used in conjunction with DietaryPrefObject.kt to more concisely look up,
* specify, and iterate through dietary preferences. Order of these elements matches the order used
@@ -19,4 +21,45 @@ enum class DietaryPreferences(val value: String) {
VEGAN("isVegan"),
VEGETARIAN("isVegetarian"),
GAME("isGame"),
;
companion object {
/**
* Provides a converting function between the index of a dietary preference and its according
* icon.
* TODO this seems inefficient
*/
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,
)
/**
* Provides a converting function between the index of a dietary preference and its
* according localised text string.
* TODO this seems inefficient
*/
val indexToString: Map<Int, Int> = mapOf(
0 to R.string.pref_fair,
1 to R.string.pref_fish,
2 to R.string.pref_poultry,
3 to R.string.pref_lamb,
4 to R.string.pref_vital,
5 to R.string.pref_beef,
6 to R.string.pref_pork,
7 to R.string.pref_vegan,
8 to R.string.pref_vegetarian,
9 to R.string.pref_game,
10 to 0, // shouldn't occur
)
}
}
@@ -1,14 +1,25 @@
package com.denizk0461.studip.sheet
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.content.res.AppCompatResources.getDrawable
import com.denizk0461.studip.R
import com.denizk0461.studip.data.viewBinding
import com.denizk0461.studip.databinding.ItemSheetPreferenceBinding
import com.denizk0461.studip.databinding.SheetAllergenBinding
import com.denizk0461.studip.model.Allergens
import com.denizk0461.studip.model.CanteenOfferGroupElement
import com.denizk0461.studip.model.DietaryPreferences
class AllergenSheet(private val offer: CanteenOfferGroupElement) : AppSheet(R.layout.sheet_allergen) {
/**
* This class is used to display further information on a canteen offer to the user. Unlike the name
* implies, it displays more than just allergens.
*/
class AllergenSheet(
private val offer: CanteenOfferGroupElement,
private val category: String,
) : AppSheet(R.layout.sheet_allergen) {
private val binding: SheetAllergenBinding by viewBinding(SheetAllergenBinding::bind)
@@ -16,8 +27,46 @@ class AllergenSheet(private val offer: CanteenOfferGroupElement) : AppSheet(R.la
super.onViewCreated(view, savedInstanceState)
binding.apply {
title.text = offer.title
// Category header
textCategory.text = getString(R.string.sheet_category, category)
// Offer text
textTitle.text = offer.title
// Iterate through all dietary preferences
offer.dietaryPreferences.toList().forEachIndexed { index, c ->
// If a preference is met, inflate a view to display it
if (c == 't') {
// Inflate the view
val prefLine = ItemSheetPreferenceBinding.inflate(LayoutInflater.from(context))
// Add the localised text for the preference
prefLine.preferenceText.text = getString(DietaryPreferences.indexToString[index]!!)
// Set the appropriate icon
prefLine.preferenceImage.setImageDrawable(
getDrawable(
context,
DietaryPreferences.indexToDrawable[index]!!,
)
)
// Add the view to the sheet's view container
containerPreferences.addView(prefLine.root)
}
}
// Allergen and additive notice
textContent.text = offer.allergens.compileAllergenString()
// Show a price, if one is available
if (offer.price.isBlank()) {
textPrice.visibility = View.GONE
} else {
textPrice.visibility = View.VISIBLE
textPrice.text = offer.price
}
}
}