Canteen offers are now grouped; TODO: add a divider to separate different offers. Also, add prices

This commit is contained in:
denizk0461
2023-04-20 08:17:23 +02:00
parent 70a89ac681
commit 5d58d25386
5 changed files with 95 additions and 38 deletions
@@ -10,8 +10,9 @@ import com.denizk0461.studip.databinding.ItemCanteenBinding
import com.denizk0461.studip.databinding.ItemCanteenLineBinding
import com.denizk0461.studip.databinding.ItemIconBinding
import com.denizk0461.studip.model.CanteenOffer
import com.denizk0461.studip.model.CanteenOfferGroup
class CanteenOfferItemAdapter(private val offers: List<CanteenOffer>) : RecyclerView.Adapter<CanteenOfferItemAdapter.OfferViewHolder>() {
class CanteenOfferItemAdapter(private val offers: List<CanteenOfferGroup>) : RecyclerView.Adapter<CanteenOfferItemAdapter.OfferViewHolder>() {
class OfferViewHolder(val binding: ItemCanteenBinding) : RecyclerView.ViewHolder(binding.root)
@@ -30,29 +31,31 @@ class CanteenOfferItemAdapter(private val offers: List<CanteenOffer>) : Recycler
override fun onBindViewHolder(holder: OfferViewHolder, position: Int) {
val currentItem = offers[position]
val line = ItemCanteenLineBinding.inflate(
LayoutInflater.from(holder.binding.root.context),
// holder.binding.root,
// true,
)
holder.binding.textCategory.text = currentItem.category
line.content.text = currentItem.title
val indices = arrayListOf<Int>()
currentItem.dietaryPreferences.filterIndexed { index, char ->
if (char == 't') {
indices.add(index)
true
} else false
}
if (indices.isEmpty()) indices.add(10)
indices.forEach { index ->
val img = ItemIconBinding.inflate(LayoutInflater.from(holder.binding.root.context))
// holder.binding.root.resources.getDrawable(Misc.indexToDrawable[index]!!, holder.binding.root.context.theme)
img.imageView.setImageDrawable(holder.binding.root.context.getDrawable(Misc.indexToDrawable[index]!!))
line.imageViewContainer.addView(img.root)
}
holder.binding.lineContainer.addView(line.root)
currentItem.offers.forEach { offer ->
val line = ItemCanteenLineBinding.inflate(
LayoutInflater.from(holder.binding.root.context),
)
line.content.text = offer.title
val indices = arrayListOf<Int>()
offer.dietaryPreferences.filterIndexed { index, char ->
if (char == 't') {
indices.add(index)
true
} else false
}
if (indices.isEmpty()) indices.add(10)
indices.forEach { index ->
val img = ItemIconBinding.inflate(LayoutInflater.from(holder.binding.root.context))
// holder.binding.root.resources.getDrawable(Misc.indexToDrawable[index]!!, holder.binding.root.context.theme)
img.imageView.setImageDrawable(holder.binding.root.context.getDrawable(Misc.indexToDrawable[index]!!))
line.imageViewContainer.addView(img.root)
}
holder.binding.lineContainer.addView(line.root)
}
// holder.binding.textCategory.text = currentItem.category
// holder.binding.tempContent.text = "${currentItem.title} • ${currentItem.price}"
@@ -7,8 +7,10 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.denizk0461.studip.databinding.ItemScrollablePageBinding
import com.denizk0461.studip.model.CanteenOffer
import com.denizk0461.studip.model.CanteenOfferGroup
import com.denizk0461.studip.model.CanteenOfferGroupElement
class CanteenOfferPageAdapter(private var offers: List<CanteenOffer>, private var daysCovered: Int, private var prefsRegex: Regex) : RecyclerView.Adapter<CanteenOfferPageAdapter.CanteenOfferPageViewHolder>() {
class CanteenOfferPageAdapter(private var offers: List<CanteenOfferGroup>, private var daysCovered: Int, private var prefsRegex: Regex) : RecyclerView.Adapter<CanteenOfferPageAdapter.CanteenOfferPageViewHolder>() {
class CanteenOfferPageViewHolder(val binding: ItemScrollablePageBinding) : RecyclerView.ViewHolder(binding.root)
@@ -31,30 +33,19 @@ class CanteenOfferPageAdapter(private var offers: List<CanteenOffer>, private va
// TODO second filter needs to be worked on; it shouldn't filter out something if two options are selected and only one is met
// Log.d("eek!5", prefsRegex.toString())
val filteredForDate = offers.filter { it.dateId == position }
val filteredForPreferences = if (prefsRegex.toString() == "ffffffffff") {
// show all elements and skip filtering
filteredForDate
} else {
filteredForDate.filter {
Log.d("eek!5", "${it.title}: - ${prefsRegex} vs ${it.dietaryPreferences}")
prefsRegex.matches(it.dietaryPreferences)
}
}
// .filter { it.dietaryPreferences == prefs.deconstruct() }
// val filtered2 = filtered
// offers
adapter = CanteenOfferItemAdapter(filteredForPreferences) // TODO check if empty
adapter = CanteenOfferItemAdapter(offers.filter { it.dateId == position }) // TODO check if empty
scheduleLayoutAnimation()
}
}
fun setNewItems(items: List<CanteenOffer>, daysCovered: Int) {
fun setNewItems(items: List<CanteenOfferGroup>, daysCovered: Int) {
this.daysCovered = daysCovered
offers = items
notifyDataSetChanged()