Transitioned from a fairly custom solution to using a colour palette generated by the Material Theme Builder to simplify colour management. Also began work on a bottom sheet for displaying allergens and additives in canteen offers.

This commit is contained in:
denizk0461
2023-04-23 17:13:48 +02:00
parent fbad84aa09
commit b957172721
42 changed files with 456 additions and 273 deletions
@@ -8,16 +8,37 @@ 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
/**
* 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
*/
class CanteenOfferItemAdapter(
private val offers: List<CanteenOfferGroup>
private val offers: List<CanteenOfferGroup>,
private val onClickListener: OnClickListener
) : 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
*
@@ -86,26 +107,39 @@ class CanteenOfferItemAdapter(
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)
}
}
/**
* Provides a converting function between the index of a dietary preference and its according
* icon.
* Interface used to evaluate clicks and long presses on a given item.
*/
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,
)
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
}
}
@@ -16,10 +16,12 @@ import com.denizk0461.studip.model.CanteenOfferGroup
* @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
*/
class CanteenOfferPageAdapter(
private var offers: List<CanteenOfferGroup>,
private var daysCovered: Int,
private val onClickListener: CanteenOfferItemAdapter.OnClickListener,
) : RecyclerView.Adapter<CanteenOfferPageAdapter.CanteenOfferPageViewHolder>() {
/**
@@ -50,8 +52,12 @@ class CanteenOfferPageAdapter(
/*
* Create new adapter for every page. Attribute position denotes day that will be set up
* by the newly created adapter (0 = Monday, 4 = Friday)
* TODO check if the filtered list is empty, and tell the user if it is
*/
adapter = CanteenOfferItemAdapter(offers.filter { it.dateId == position }) // TODO check if empty
adapter = CanteenOfferItemAdapter(
offers.filter { it.dateId == position },
onClickListener
)
// Animate creation of new page
scheduleLayoutAnimation()
@@ -1,11 +1,8 @@
package com.denizk0461.studip.adapter
import android.content.res.ColorStateList
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.denizk0461.studip.R
import com.denizk0461.studip.model.StudIPEvent
import com.denizk0461.studip.databinding.ItemEventBinding
import java.util.*
@@ -72,42 +69,43 @@ class StudIPEventItemAdapter(
// Use parsed timeslot string as defined in StudIPEvent#timeslot()
holder.binding.textTimeslot.text = currentItem.timeslot()
// TODO highlighting functionality is currently not supported!
// Set up colours used for highlighting and un-highlighting an item.
val colorPrimaryTranslucent = TypedValue()
val colorCardBackground = TypedValue()
val colorTextHintLighter = TypedValue()
// val colorPrimaryTranslucent = TypedValue()
// val colorCardBackground = TypedValue()
// val colorTextHintLighter = TypedValue()
// Resolve themed attributes to get the right values for day and night modes
holder.binding.root.context.theme.apply {
resolveAttribute(R.attr.colorPrimaryTranslucent, colorPrimaryTranslucent, true)
resolveAttribute(R.attr.colorCardBackground, colorCardBackground, true)
resolveAttribute(R.attr.colorTextHintLighter, colorTextHintLighter, true)
}
// holder.binding.root.context.theme.apply {
// resolveAttribute(com.google.android.material.R.attr.colorSecondaryContainer, colorPrimaryTranslucent, true)
// resolveAttribute(R.attr.colorCardBackground, colorCardBackground, true)
// resolveAttribute(R.attr.colorTextHintLighter, colorTextHintLighter, true)
// }
/* Highlight the next upcoming course of the day if the day of the adapter matches the
* current day, and if no other course has been highlighted, to avoid double highlighting.
*/
if (!isAnyCourseHighlighted && currentItem.isCurrentCourse(currentCalendar)) {
// Ensure that no other course will be highlighted
isAnyCourseHighlighted = true
holder.binding.cardBackground.apply {
// Set the card's background colour to a desaturated shade of the primary colour
backgroundTintList = ColorStateList.valueOf(colorPrimaryTranslucent.data)
// Hide card stroke
strokeColor = context.getColor(android.R.color.transparent)
}
// Otherwise, apply colours to ensure that the item will not be highlighted
} else {
holder.binding.cardBackground.apply {
// Set the card's background colour to its default value
backgroundTintList = ColorStateList.valueOf(colorCardBackground.data)
// Set the card's stroke to its default colour
strokeColor = colorTextHintLighter.data
}
}
// if (!isAnyCourseHighlighted && currentItem.isCurrentCourse(currentCalendar)) {
// // Ensure that no other course will be highlighted
// isAnyCourseHighlighted = true
//
// holder.binding.cardBackground.apply {
// // Set the card's background colour to a desaturated shade of the primary colour
// backgroundTintList = ColorStateList.valueOf(colorPrimaryTranslucent.data)
//
// // Hide card stroke
// strokeColor = context.getColor(android.R.color.transparent)
// }
// // Otherwise, apply colours to ensure that the item will not be highlighted
// } else {
// holder.binding.cardBackground.apply {
// // Set the card's background colour to its default value
// backgroundTintList = ColorStateList.valueOf(colorCardBackground.data)
//
// // Set the card's stroke to its default colour
// strokeColor = colorTextHintLighter.data
// }
// }
// Set up single click listener
holder.binding.cardBackground.setOnClickListener {
@@ -16,7 +16,7 @@ import com.denizk0461.studip.databinding.ItemScrollablePageBinding
*/
class StudIPEventPageAdapter(
private var events: List<StudIPEvent>,
private val onClickListener: StudIPEventItemAdapter.OnClickListener
private val onClickListener: StudIPEventItemAdapter.OnClickListener,
) : RecyclerView.Adapter<StudIPEventPageAdapter.EventPageViewHolder>() {
/**