2023-04-08 13:43:26 +02:00
|
|
|
package com.denizk0461.studip.adapter
|
|
|
|
|
|
2023-04-25 15:38:30 +02:00
|
|
|
import android.content.res.ColorStateList
|
2023-04-08 13:43:26 +02:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
|
import android.view.ViewGroup
|
2023-04-28 11:18:05 +02:00
|
|
|
import androidx.recyclerview.widget.DiffUtil
|
2023-04-08 13:43:26 +02:00
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2023-04-28 11:18:05 +02:00
|
|
|
import com.denizk0461.studip.data.AppDiffUtilCallback
|
2023-04-25 15:38:30 +02:00
|
|
|
import com.google.android.material.R
|
|
|
|
|
import com.denizk0461.studip.data.getThemedColor
|
2023-04-24 15:43:42 +02:00
|
|
|
import com.denizk0461.studip.data.parseToMinutes
|
2023-04-08 18:53:51 +02:00
|
|
|
import com.denizk0461.studip.model.StudIPEvent
|
2023-04-08 13:43:26 +02:00
|
|
|
import com.denizk0461.studip.databinding.ItemEventBinding
|
2023-04-14 13:09:40 +02:00
|
|
|
import java.util.*
|
2023-04-08 13:43:26 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/**
|
2023-04-28 11:18:05 +02:00
|
|
|
* Custom RecyclerView adapter that lays out the Stud.IP events for a given day.
|
2023-04-23 11:57:03 +02:00
|
|
|
*
|
2023-04-28 11:18:05 +02:00
|
|
|
|
|
|
|
|
* @param currentDay current day that is used to show only a given day's events (0 = Monday,
|
2023-04-23 11:57:03 +02:00
|
|
|
* 4 = Friday)
|
|
|
|
|
* @param onClickListener used for listening to clicks and long presses
|
|
|
|
|
*/
|
2023-04-16 21:41:46 +02:00
|
|
|
class StudIPEventItemAdapter(
|
2023-04-23 11:57:03 +02:00
|
|
|
private val currentDay: Int,
|
|
|
|
|
private val onClickListener: OnClickListener,
|
2023-04-16 21:41:46 +02:00
|
|
|
) : RecyclerView.Adapter<StudIPEventItemAdapter.EventViewHolder>() {
|
2023-04-12 10:49:30 +02:00
|
|
|
|
2023-04-28 11:18:05 +02:00
|
|
|
/**
|
2023-04-28 15:14:50 +02:00
|
|
|
* List of all events.
|
2023-04-28 11:18:05 +02:00
|
|
|
*/
|
|
|
|
|
private var events: MutableList<StudIPEvent> = mutableListOf()
|
2023-04-23 11:57:03 +02:00
|
|
|
|
2023-04-28 11:18:05 +02:00
|
|
|
/**
|
2023-04-23 11:57:03 +02:00
|
|
|
* Retrieve an instance of Calendar to check whether the adapter's day matches the current day.
|
2023-04-28 11:18:05 +02:00
|
|
|
* TODO this can be optimised by checking in EventPageFragment.kt and delivering a boolean
|
2023-04-23 11:57:03 +02:00
|
|
|
*/
|
2023-04-14 13:09:40 +02:00
|
|
|
private val currentCalendar = Calendar.getInstance()
|
2023-04-23 11:57:03 +02:00
|
|
|
|
2023-04-28 11:18:05 +02:00
|
|
|
/**
|
2023-04-23 11:57:03 +02:00
|
|
|
* Used to disallow highlighting more than one course at a time, since only a single course
|
|
|
|
|
* could be coming up at a given time.
|
|
|
|
|
*/
|
2023-04-14 13:09:40 +02:00
|
|
|
private var isAnyCourseHighlighted = false
|
2023-04-08 13:43:26 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/**
|
|
|
|
|
* View holder class for parent class
|
|
|
|
|
*
|
|
|
|
|
* @param binding view binding object
|
|
|
|
|
*/
|
2023-04-08 15:44:07 +02:00
|
|
|
class EventViewHolder(val binding: ItemEventBinding) : RecyclerView.ViewHolder(binding.root)
|
2023-04-08 13:43:26 +02:00
|
|
|
|
2023-04-12 10:49:30 +02:00
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EventViewHolder {
|
|
|
|
|
return EventViewHolder(
|
|
|
|
|
ItemEventBinding.inflate(
|
|
|
|
|
LayoutInflater.from(parent.context),
|
|
|
|
|
parent,
|
|
|
|
|
false
|
|
|
|
|
)
|
2023-04-08 13:43:26 +02:00
|
|
|
)
|
2023-04-12 10:49:30 +02:00
|
|
|
}
|
2023-04-08 13:43:26 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Set the amount of items to the size of the filtered list, so only events of the current day
|
2023-04-28 11:18:05 +02:00
|
|
|
override fun getItemCount(): Int = events.size
|
2023-04-08 13:43:26 +02:00
|
|
|
|
|
|
|
|
override fun onBindViewHolder(holder: EventViewHolder, position: Int) {
|
2023-04-23 11:57:03 +02:00
|
|
|
// Retrieve item for current position
|
2023-04-28 11:18:05 +02:00
|
|
|
val currentItem = events[position]
|
2023-04-08 17:30:05 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Set up text fields with corresponding values
|
2023-04-08 15:44:07 +02:00
|
|
|
holder.binding.textTitle.text = currentItem.title
|
2023-04-08 17:30:05 +02:00
|
|
|
holder.binding.textLecturers.text = currentItem.lecturer
|
2023-04-08 15:44:07 +02:00
|
|
|
holder.binding.textRoom.text = currentItem.room
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
// Use parsed timeslot string as defined in StudIPEvent#timeslot()
|
2023-04-10 20:54:15 +02:00
|
|
|
holder.binding.textTimeslot.text = currentItem.timeslot()
|
2023-04-08 17:30:05 +02:00
|
|
|
|
2023-04-26 10:21:16 +02:00
|
|
|
// Get current theme
|
2023-04-25 15:38:30 +02:00
|
|
|
val theme = holder.binding.root.context.theme
|
|
|
|
|
|
2023-04-26 10:21:16 +02:00
|
|
|
/*
|
|
|
|
|
* Highlight the next upcoming course of the day if the day of the adapter matches the
|
2023-04-23 11:57:03 +02:00
|
|
|
* current day, and if no other course has been highlighted, to avoid double highlighting.
|
|
|
|
|
*/
|
2023-04-25 15:38:30 +02:00
|
|
|
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(theme.getThemedColor(R.attr.colorSecondaryContainer))
|
|
|
|
|
|
|
|
|
|
// Hide card stroke
|
|
|
|
|
strokeColor = context.getColor(android.R.color.transparent)
|
|
|
|
|
}
|
2023-04-26 10:21:16 +02:00
|
|
|
// Set the divider colour to the text colour
|
|
|
|
|
holder.binding.divider.dividerColor = theme.getThemedColor(com.denizk0461.studip.R.attr.colorText)
|
2023-04-25 15:38:30 +02:00
|
|
|
// 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(theme.getThemedColor(R.attr.colorSurface))
|
|
|
|
|
|
|
|
|
|
// Set the card's stroke to its default colour
|
2023-04-26 10:21:16 +02:00
|
|
|
strokeColor = theme.getThemedColor(R.attr.colorOutline)
|
2023-04-25 15:38:30 +02:00
|
|
|
}
|
2023-04-26 10:21:16 +02:00
|
|
|
// Set the divider colour to its default value
|
|
|
|
|
holder.binding.divider.dividerColor = theme.getThemedColor(R.attr.colorOutlineVariant)
|
2023-04-25 15:38:30 +02:00
|
|
|
}
|
2023-04-12 10:49:30 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Set up single click listener
|
2023-04-12 22:07:18 +02:00
|
|
|
holder.binding.cardBackground.setOnClickListener {
|
|
|
|
|
onClickListener.onClick(currentItem)
|
|
|
|
|
}
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
// Set up long press listener
|
2023-04-12 22:07:18 +02:00
|
|
|
holder.binding.cardBackground.setOnLongClickListener {
|
|
|
|
|
onClickListener.onLongClick(currentItem)
|
|
|
|
|
}
|
2023-04-08 13:43:26 +02:00
|
|
|
}
|
2023-04-12 22:07:18 +02:00
|
|
|
|
2023-04-28 11:18:05 +02:00
|
|
|
/**
|
|
|
|
|
* Updates the data and calculates the difference between the old dataset and the newly provided
|
|
|
|
|
* dataset.
|
|
|
|
|
*
|
|
|
|
|
* @param newData new dataset to be displayed
|
|
|
|
|
*/
|
|
|
|
|
fun setNewData(newData: List<StudIPEvent>) {
|
|
|
|
|
// Calculate the difference between the old list and the new list
|
|
|
|
|
val diffResult = DiffUtil.calculateDiff(AppDiffUtilCallback(events, newData))
|
|
|
|
|
|
|
|
|
|
// Remove all items from the list
|
|
|
|
|
events.clear()
|
|
|
|
|
|
|
|
|
|
// Add all items from the new list
|
|
|
|
|
events.addAll(newData)
|
|
|
|
|
|
|
|
|
|
// Tell the DiffUtil which items have changed between the two lists
|
|
|
|
|
diffResult.dispatchUpdatesTo(this)
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2023-04-14 13:21:24 +02:00
|
|
|
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())
|
2023-04-14 13:09:40 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2023-04-14 13:09:40 +02:00
|
|
|
private fun Int.toCalendarDay(): Int = when (this) {
|
|
|
|
|
1 -> Calendar.TUESDAY
|
|
|
|
|
2 -> Calendar.WEDNESDAY
|
|
|
|
|
3 -> Calendar.THURSDAY
|
|
|
|
|
4 -> Calendar.FRIDAY
|
|
|
|
|
5 -> Calendar.SATURDAY
|
|
|
|
|
6 -> Calendar.SUNDAY
|
|
|
|
|
else -> Calendar.MONDAY
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/**
|
|
|
|
|
* Interface used to evaluate clicks and long presses on a given item.
|
|
|
|
|
*/
|
2023-04-12 22:07:18 +02:00
|
|
|
interface OnClickListener {
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Executed when an item has been clicked.
|
|
|
|
|
*
|
|
|
|
|
* @param event item that has been clicked
|
|
|
|
|
*/
|
2023-04-12 22:07:18 +02:00
|
|
|
fun onClick(event: StudIPEvent)
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
2023-04-12 22:07:18 +02:00
|
|
|
}
|
2023-04-08 13:43:26 +02:00
|
|
|
}
|