Fully documented all Kotlin files and added TODOs to functions that need to be updated in some way

This commit is contained in:
denizk0461
2023-04-23 11:57:03 +02:00
parent 2534849329
commit fbad84aa09
33 changed files with 1220 additions and 571 deletions
@@ -2,18 +2,27 @@ package com.denizk0461.studip.adapter
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.widget.ImageViewCompat
import androidx.recyclerview.widget.RecyclerView
import com.denizk0461.studip.data.Misc
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.CanteenOffer
import com.denizk0461.studip.model.CanteenOfferGroup
class CanteenOfferItemAdapter(private val offers: List<CanteenOfferGroup>) : RecyclerView.Adapter<CanteenOfferItemAdapter.OfferViewHolder>() {
/**
* 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
*/
class CanteenOfferItemAdapter(
private val offers: List<CanteenOfferGroup>
) : RecyclerView.Adapter<CanteenOfferItemAdapter.OfferViewHolder>() {
/**
* View holder class for parent class
*
* @param binding view binding object
*/
class OfferViewHolder(val binding: ItemCanteenBinding) : RecyclerView.ViewHolder(binding.root)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OfferViewHolder {
@@ -26,20 +35,32 @@ class CanteenOfferItemAdapter(private val offers: List<CanteenOfferGroup>) : Rec
)
}
// Returns the amount of offers for a given canteen and day
override fun getItemCount(): Int = offers.size
// TODO inflate a view saying "no events" if none are available for a given day
override fun onBindViewHolder(holder: OfferViewHolder, position: Int) {
// Retrieve item for current position
val currentItem = offers[position]
// Set category text / header of the item
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
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') {
@@ -47,25 +68,44 @@ class CanteenOfferItemAdapter(private val offers: List<CanteenOfferGroup>) : Rec
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))
// holder.binding.root.resources.getDrawable(Misc.indexToDrawable[index]!!, holder.binding.root.context.theme)
img.imageView.setImageDrawable(holder.binding.root.context.getDrawable(Misc.indexToDrawable[index]!!))
// 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)
}
// Bind the new line to the line container
holder.binding.lineContainer.addView(line.root)
}
// holder.binding.textCategory.text = currentItem.category
// holder.binding.tempContent.text = "${currentItem.title} • ${currentItem.price}"
// TODO inflate view saying "no offers!" or sth
}
// interface OnClickListener {
// fun onClick(event: StudIPEvent)
// fun onLongClick(event: StudIPEvent)
// }
/**
* 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,
)
}
@@ -7,8 +7,26 @@ import androidx.recyclerview.widget.RecyclerView
import com.denizk0461.studip.databinding.ItemScrollablePageBinding
import com.denizk0461.studip.model.CanteenOfferGroup
class CanteenOfferPageAdapter(private var offers: List<CanteenOfferGroup>, private var daysCovered: Int, private var prefsRegex: Regex) : RecyclerView.Adapter<CanteenOfferPageAdapter.CanteenOfferPageViewHolder>() {
/**
* 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.
*/
class CanteenOfferPageAdapter(
private var offers: List<CanteenOfferGroup>,
private var daysCovered: Int,
) : RecyclerView.Adapter<CanteenOfferPageAdapter.CanteenOfferPageViewHolder>() {
/**
* View holder class for parent class
*
* @param binding view binding object
*/
class CanteenOfferPageViewHolder(val binding: ItemScrollablePageBinding) : RecyclerView.ViewHolder(binding.root)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CanteenOfferPageViewHolder =
@@ -20,22 +38,39 @@ class CanteenOfferPageAdapter(private var offers: List<CanteenOfferGroup>, priva
)
)
// Returns the count of days covered
override fun getItemCount(): Int = daysCovered
override fun onBindViewHolder(holder: CanteenOfferPageViewHolder, position: Int) {
holder.binding.pageRecyclerView.apply {
// Set page to horizontally scroll
layoutManager = LinearLayoutManager(holder.binding.root.context, LinearLayoutManager.VERTICAL, false)
/*
* Create new adapter for every page. Attribute position denotes day that will be set up
* by the newly created adapter (0 = Monday, 4 = Friday)
*/
adapter = CanteenOfferItemAdapter(offers.filter { it.dateId == position }) // TODO check if empty
// Animate creation of new page
scheduleLayoutAnimation()
}
}
/**
* Update the entire list of items
*
* @param items the new set of items
*/
fun setNewItems(items: List<CanteenOfferGroup>, daysCovered: Int) {
// Update the count of days covered
this.daysCovered = daysCovered
// Update items
offers = items
// Force an update of the view. TODO replace with individual updates, as this is inefficient
notifyDataSetChanged()
}
}
@@ -10,14 +10,40 @@ import com.denizk0461.studip.model.StudIPEvent
import com.denizk0461.studip.databinding.ItemEventBinding
import java.util.*
/**
* Custom RecyclerView adapter that lays out the Stud.IP events of a given day.
*
* @param events list of all events (still not filtered by day at this point)
* @param currentDay current day that is used to show only events of a given day (0 = Monday,
* 4 = Friday)
* @param onClickListener used for listening to clicks and long presses
*/
class StudIPEventItemAdapter(
events: List<StudIPEvent>, private val currentDay: Int, private val onClickListener: OnClickListener
events: List<StudIPEvent>,
private val currentDay: Int,
private val onClickListener: OnClickListener,
) : RecyclerView.Adapter<StudIPEventItemAdapter.EventViewHolder>() {
// Filter events to only show those of a given day
private val filteredEvents: List<StudIPEvent> = events.filter { it.day == currentDay }
/*
* Retrieve an instance of Calendar to check whether the adapter's day matches the current day.
* TODO this can be optimised by checking in StudIPEventPageAdapter.kt and delivering a boolean
*/
private val currentCalendar = Calendar.getInstance()
/*
* Used to disallow highlighting more than one course at a time, since only a single course
* could be coming up at a given time.
*/
private var isAnyCourseHighlighted = false
/**
* View holder class for parent class
*
* @param binding view binding object
*/
class EventViewHolder(val binding: ItemEventBinding) : RecyclerView.ViewHolder(binding.root)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EventViewHolder {
@@ -30,57 +56,92 @@ class StudIPEventItemAdapter(
)
}
// Set the amount of items to the size of the filtered list, so only events of the current day
override fun getItemCount(): Int = filteredEvents.size
// TODO inflate a view saying "no events" if none are available for a given day
override fun onBindViewHolder(holder: EventViewHolder, position: Int) {
// Retrieve item for current position
val currentItem = filteredEvents[position]
// Set up text fields with corresponding values
holder.binding.textTitle.text = currentItem.title
holder.binding.textLecturers.text = currentItem.lecturer
holder.binding.textRoom.text = currentItem.room
// Use parsed timeslot string as defined in StudIPEvent#timeslot()
holder.binding.textTimeslot.text = currentItem.timeslot()
// Set up colours used for highlighting and un-highlighting an item.
val colorPrimaryTranslucent = TypedValue()
val colorCardBackground = TypedValue()
val colorTextHintLighter = TypedValue()
val theme = holder.binding.root.context.theme
theme.apply {
// 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)
}
if (!isAnyCourseHighlighted && currentItem.isCurrentCourse(currentCalendar)) { // highlight
/* 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)
}
} else { // un-highlight
// 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 {
onClickListener.onClick(currentItem)
}
// Set up long press listener
holder.binding.cardBackground.setOnLongClickListener {
onClickListener.onLongClick(currentItem)
true
}
// TODO inflate view saying "no events!" or sth
}
/**
* Checks if a given course is the next course coming up. This is done by checking whether the
* day of the course matches the current real world day, as well as by evaluating if the end
* time stamp of the event has already passed. As the course list is ordered chronologically,
* this should always find the 'next' event, since without ordering the data origin, this could
* be used to highlight any event of the day that hasn't already passed.
*
* @param calendar instance of the current calendar
* @return whether the course is coming up next
*/
private fun StudIPEvent.isCurrentCourse(calendar: Calendar): Boolean =
(calendar.get(Calendar.DAY_OF_WEEK) == this.day.toCalendarDay()) &&
((calendar.get(Calendar.MINUTE) + calendar.get(Calendar.HOUR_OF_DAY) * 60) < this.timeslotEnd.parseToMinutes())
// Converts day as defined in StudIPEvent.kt to day from Calendar
/**
* Converts a numeric value as defined in StudIPEvent.kt to a calendar day. Used to convert from
* American convention (where Sunday is the first day, 1 = Sunday, 2 = Monday, 7 = Saturday) to
* European convention and zero-based indexing (0 = Monday, 6 = Sunday).
*
* @return calendar day as numeric value
*/
private fun Int.toCalendarDay(): Int = when (this) {
1 -> Calendar.TUESDAY
2 -> Calendar.WEDNESDAY
@@ -91,13 +152,36 @@ class StudIPEventItemAdapter(
else -> Calendar.MONDAY
}
/**
* Converts a time stamp string to its numeric value in minutes.
* Example: 13:20. (20 minutes) + (13 hours * 60 minutes) = 800 minutes.
*
* @return minute value of the string
*/
private fun String.parseToMinutes(): Int {
val parts = split(":")
return (parts[0].toInt() * 60) + parts[1].toInt()
}
/**
* Interface used to evaluate clicks and long presses on a given item.
*/
interface OnClickListener {
/**
* Executed when an item has been clicked.
*
* @param event item that has been clicked
*/
fun onClick(event: StudIPEvent)
fun onLongClick(event: StudIPEvent)
/**
* Executed when an item has been long-pressed.
*
* @param event item that has been long-pressed
* @return whether the long press was successful
*/
fun onLongClick(event: StudIPEvent): Boolean
}
}
@@ -7,8 +7,23 @@ import androidx.recyclerview.widget.RecyclerView
import com.denizk0461.studip.model.StudIPEvent
import com.denizk0461.studip.databinding.ItemScrollablePageBinding
class StudIPEventPageAdapter(private var events: List<StudIPEvent>, private val onClickListener: StudIPEventItemAdapter.OnClickListener) : RecyclerView.Adapter<StudIPEventPageAdapter.EventPageViewHolder>() {
/**
* Custom RecyclerView adapter for managing multiple pages of Stud.IP events in a RecyclerView or
* ViewPager.
*
* @param events all events (not filtered by day at this point)
* @param onClickListener for managing click and long press events
*/
class StudIPEventPageAdapter(
private var events: List<StudIPEvent>,
private val onClickListener: StudIPEventItemAdapter.OnClickListener
) : RecyclerView.Adapter<StudIPEventPageAdapter.EventPageViewHolder>() {
/**
* View holder class for parent class
*
* @param binding view binding object
*/
class EventPageViewHolder(val binding: ItemScrollablePageBinding) : RecyclerView.ViewHolder(binding.root)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EventPageViewHolder = EventPageViewHolder(
@@ -19,19 +34,39 @@ class StudIPEventPageAdapter(private var events: List<StudIPEvent>, private val
)
)
/*
* Assume that there will always be 5 pages, for 5 days - Monday through Friday.
* TODO is it right to assume that the user only has events on weekdays?
*/
override fun getItemCount(): Int = 5
// Set up page
override fun onBindViewHolder(holder: EventPageViewHolder, position: Int) {
holder.binding.pageRecyclerView.apply {
// Set page to horizontally scroll
layoutManager = LinearLayoutManager(holder.binding.root.context, LinearLayoutManager.VERTICAL, false)
/*
* Create new adapter for every page. Attribute position denotes day that will be set up
* by the newly created adapter (0 = Monday, 4 = Friday)
*/
adapter = StudIPEventItemAdapter(events, currentDay = position, onClickListener) // TODO check if empty
// Animate creation of new page
scheduleLayoutAnimation()
}
}
/**
* Update the entire list of items
*
* @param items the new set of items
*/
fun setNewItems(items: List<StudIPEvent>) {
// Update items
events = items
// Force an update of the view. TODO replace with individual updates, as this is inefficient
notifyDataSetChanged()
}
}