2023-04-08 13:43:26 +02:00
|
|
|
package com.denizk0461.studip.adapter
|
|
|
|
|
|
2023-04-20 14:30:48 +02:00
|
|
|
import android.content.res.ColorStateList
|
2023-04-14 13:09:40 +02:00
|
|
|
import android.util.TypedValue
|
2023-04-08 13:43:26 +02:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
|
import android.view.ViewGroup
|
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2023-04-14 13:09:40 +02:00
|
|
|
import com.denizk0461.studip.R
|
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-16 21:41:46 +02:00
|
|
|
class StudIPEventItemAdapter(
|
2023-04-12 22:07:18 +02:00
|
|
|
events: List<StudIPEvent>, 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
|
|
|
|
|
|
|
|
private val filteredEvents: List<StudIPEvent> = events.filter { it.day == currentDay }
|
2023-04-14 13:09:40 +02:00
|
|
|
private val currentCalendar = Calendar.getInstance()
|
|
|
|
|
private var isAnyCourseHighlighted = false
|
2023-04-08 13:43:26 +02:00
|
|
|
|
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-12 10:49:30 +02:00
|
|
|
override fun getItemCount(): Int = filteredEvents.size
|
2023-04-08 13:43:26 +02:00
|
|
|
|
|
|
|
|
override fun onBindViewHolder(holder: EventViewHolder, position: Int) {
|
2023-04-12 10:49:30 +02:00
|
|
|
val currentItem = filteredEvents[position]
|
2023-04-08 17:30:05 +02:00
|
|
|
|
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-10 20:54:15 +02:00
|
|
|
holder.binding.textTimeslot.text = currentItem.timeslot()
|
2023-04-08 17:30:05 +02:00
|
|
|
|
2023-04-20 14:30:48 +02:00
|
|
|
val colorPrimaryTranslucent = TypedValue()
|
|
|
|
|
val colorCardBackground = TypedValue()
|
|
|
|
|
val colorTextHintLighter = TypedValue()
|
2023-04-14 13:09:40 +02:00
|
|
|
val theme = holder.binding.root.context.theme
|
2023-04-20 14:30:48 +02:00
|
|
|
|
2023-04-20 21:35:09 +02:00
|
|
|
theme.apply {
|
|
|
|
|
resolveAttribute(R.attr.colorPrimaryTranslucent, colorPrimaryTranslucent, true)
|
|
|
|
|
resolveAttribute(R.attr.colorCardBackground, colorCardBackground, true)
|
|
|
|
|
resolveAttribute(R.attr.colorTextHintLighter, colorTextHintLighter, true)
|
|
|
|
|
}
|
2023-04-14 13:09:40 +02:00
|
|
|
|
|
|
|
|
if (!isAnyCourseHighlighted && currentItem.isCurrentCourse(currentCalendar)) { // highlight
|
|
|
|
|
isAnyCourseHighlighted = true
|
2023-04-20 14:30:48 +02:00
|
|
|
holder.binding.cardBackground.apply {
|
2023-04-20 21:35:09 +02:00
|
|
|
backgroundTintList = ColorStateList.valueOf(colorPrimaryTranslucent.data)
|
2023-04-20 14:30:48 +02:00
|
|
|
strokeColor = context.getColor(android.R.color.transparent)
|
2023-04-20 21:35:09 +02:00
|
|
|
}
|
2023-04-12 10:49:30 +02:00
|
|
|
} else { // un-highlight
|
2023-04-20 14:30:48 +02:00
|
|
|
holder.binding.cardBackground.apply {
|
2023-04-20 21:35:09 +02:00
|
|
|
backgroundTintList = ColorStateList.valueOf(colorCardBackground.data)
|
2023-04-20 14:30:48 +02:00
|
|
|
strokeColor = colorTextHintLighter.data
|
|
|
|
|
}
|
2023-04-12 10:49:30 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-12 22:07:18 +02:00
|
|
|
holder.binding.cardBackground.setOnClickListener {
|
|
|
|
|
onClickListener.onClick(currentItem)
|
|
|
|
|
}
|
|
|
|
|
holder.binding.cardBackground.setOnLongClickListener {
|
|
|
|
|
onClickListener.onLongClick(currentItem)
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 17:30:05 +02:00
|
|
|
// TODO inflate view saying "no events!" or sth
|
|
|
|
|
|
2023-04-08 13:43:26 +02:00
|
|
|
}
|
2023-04-12 22:07:18 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
|
// Converts day as defined in StudIPEvent.kt to day from Calendar
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun String.parseToMinutes(): Int {
|
|
|
|
|
val parts = split(":")
|
|
|
|
|
return (parts[0].toInt() * 60) + parts[1].toInt()
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 22:07:18 +02:00
|
|
|
interface OnClickListener {
|
|
|
|
|
fun onClick(event: StudIPEvent)
|
|
|
|
|
fun onLongClick(event: StudIPEvent)
|
|
|
|
|
}
|
2023-04-08 13:43:26 +02:00
|
|
|
}
|