This repository has been archived on 2026-05-26. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
weserplaner/app/src/main/java/com/denizk0461/studip/adapter/StudIPEventItemAdapter.kt
T

104 lines
4.2 KiB
Kotlin
Raw Normal View History

2023-04-08 13:43:26 +02:00
package com.denizk0461.studip.adapter
import android.content.res.ColorStateList
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
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
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>() {
private val filteredEvents: List<StudIPEvent> = events.filter { it.day == currentDay }
private val currentCalendar = Calendar.getInstance()
private var isAnyCourseHighlighted = false
2023-04-08 13:43:26 +02:00
class EventViewHolder(val binding: ItemEventBinding) : RecyclerView.ViewHolder(binding.root)
2023-04-08 13:43:26 +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-08 13:43:26 +02:00
override fun getItemCount(): Int = filteredEvents.size
2023-04-08 13:43:26 +02:00
override fun onBindViewHolder(holder: EventViewHolder, position: Int) {
val currentItem = filteredEvents[position]
2023-04-08 17:30:05 +02:00
holder.binding.textTitle.text = currentItem.title
2023-04-08 17:30:05 +02:00
holder.binding.textLecturers.text = currentItem.lecturer
holder.binding.textRoom.text = currentItem.room
holder.binding.textTimeslot.text = currentItem.timeslot()
2023-04-08 17:30:05 +02:00
val colorPrimaryTranslucent = TypedValue()
val colorCardBackground = TypedValue()
val colorTextHintLighter = TypedValue()
val theme = holder.binding.root.context.theme
theme.resolveAttribute(R.attr.colorPrimaryTranslucent, colorPrimaryTranslucent, true)
theme.resolveAttribute(R.attr.colorCardBackground, colorCardBackground, true)
theme.resolveAttribute(R.attr.colorTextHintLighter, colorTextHintLighter, true)
if (!isAnyCourseHighlighted && currentItem.isCurrentCourse(currentCalendar)) { // highlight
isAnyCourseHighlighted = true
holder.binding.cardBackground.apply {
backgroundTintList = ColorStateList.valueOf(colorPrimaryTranslucent.data)//R.attr.colorPrimaryTranslucent)
strokeColor = context.getColor(android.R.color.transparent)
}//colorPrimary.data
// holder.binding.cardBackground.strokeWidth = 6 // TODO replace this with proper float -> dp conversion
// R.color.list_card_selected)
} else { // un-highlight
holder.binding.cardBackground.apply {
backgroundTintList = ColorStateList.valueOf(colorCardBackground.data)//context.getColor(R.attr.colorCardBackground))
strokeColor = colorTextHintLighter.data
}
// holder.binding.cardBackground.strokeWidth = 3 // this should (?) be 1dp
}
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())
// 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
}