feat: Improve the CalendarDay class & surrounding logic
This commit is contained in:
parent
5620bd4277
commit
b3d9e426a2
|
@ -131,17 +131,18 @@ class CalendarActivity : AppCompatActivity() {
|
|||
val startOffset = firstDayOfMonth.dayOfWeek.ordinal // Gives 0 (Monday) to 6 (Sunday)
|
||||
|
||||
// Generate calendar days
|
||||
val days = mutableListOf<CalendarDay>()
|
||||
val days = mutableListOf<CalendarDay?>()
|
||||
|
||||
// Add padding for days of the previous month
|
||||
// Add padding (placeholder items) for days of the previous month
|
||||
for (i in 1..startOffset) {
|
||||
days.add(CalendarDay(dayNumber = ""))
|
||||
days.add(null)
|
||||
}
|
||||
|
||||
// Add the actual days of the current month
|
||||
for (i in 1..daysInMonth) {
|
||||
val fullDate = yearMonth.atDay(i)
|
||||
val eventsForDay = eventsByDay[i] ?: emptyList()
|
||||
days.add(CalendarDay(dayNumber = i.toString(), eventsForDay))
|
||||
days.add(CalendarDay(date = fullDate, events = eventsForDay))
|
||||
}
|
||||
|
||||
// Set up RecyclerView
|
||||
|
@ -158,19 +159,8 @@ class CalendarActivity : AppCompatActivity() {
|
|||
}
|
||||
|
||||
private fun navigateToDayActivity(day: CalendarDay) {
|
||||
// TODO: Handle the placeholder days in a better way, using an empty string
|
||||
// feels very odd.
|
||||
if (day.dayNumber.isEmpty()) {
|
||||
Log.w("Calendar", "User clicked on placeholder value")
|
||||
return
|
||||
}
|
||||
|
||||
var intent = Intent(this, DayViewActivity::class.java)
|
||||
intent.putParcelableArrayListExtra(
|
||||
"events", ArrayList(day.events)
|
||||
)
|
||||
val formattedDate = currentYearMonth.atDay(day.dayNumber.toInt()).toString()
|
||||
intent.putExtra("date", formattedDate)
|
||||
intent.putExtra("calendarDay", day)
|
||||
startActivity(intent)
|
||||
// Don't finish the current activity, we want to be able to go back here
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ import androidx.recyclerview.widget.RecyclerView
|
|||
import com.p_vacho.neat_calendar.R
|
||||
import com.p_vacho.neat_calendar.adapters.EventAdapter
|
||||
import com.p_vacho.neat_calendar.api.models.EventResponse
|
||||
import com.p_vacho.neat_calendar.models.CalendarDay
|
||||
|
||||
class DayViewActivity : AppCompatActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
|
@ -24,20 +25,18 @@ class DayViewActivity : AppCompatActivity() {
|
|||
insets
|
||||
}
|
||||
|
||||
val date = intent.getStringExtra("date")!!
|
||||
|
||||
// The getParcelableArrayListExtra wants the class as a second argument to
|
||||
// The getParcelableExtra wants the class as a second argument to
|
||||
// be more type-safe, but this is only supported since api 33, which is over
|
||||
// our min api version, so we can ignore this deprecation for now.
|
||||
@Suppress("DEPRECATION")
|
||||
val events = intent.getParcelableArrayListExtra<EventResponse>("events")!!
|
||||
val calendarDay = intent.getParcelableExtra<CalendarDay>("calendarDay")!!
|
||||
|
||||
val tvDate: TextView = findViewById(R.id.tvDate)
|
||||
val rvEvents: RecyclerView = findViewById(R.id.rvEvents)
|
||||
|
||||
// Set up UI
|
||||
tvDate.text = date
|
||||
tvDate.text = calendarDay.date.toString()
|
||||
rvEvents.layoutManager = LinearLayoutManager(this)
|
||||
rvEvents.adapter = EventAdapter(events)
|
||||
rvEvents.adapter = EventAdapter(calendarDay.events)
|
||||
}
|
||||
}
|
|
@ -8,7 +8,7 @@ import androidx.recyclerview.widget.RecyclerView
|
|||
import com.p_vacho.neat_calendar.R
|
||||
import com.p_vacho.neat_calendar.models.CalendarDay
|
||||
|
||||
class CalendarAdapter(private val days: List<CalendarDay>, private val onDayClicked: (CalendarDay) -> Unit) :
|
||||
class CalendarAdapter(private val days: List<CalendarDay?>, private val onDayClicked: (CalendarDay) -> Unit) :
|
||||
RecyclerView.Adapter<CalendarAdapter.DayViewHolder>() {
|
||||
|
||||
class DayViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
|
@ -24,8 +24,15 @@ class CalendarAdapter(private val days: List<CalendarDay>, private val onDayClic
|
|||
override fun onBindViewHolder(holder: DayViewHolder, position: Int) {
|
||||
val day = days[position]
|
||||
|
||||
// Set the day number
|
||||
holder.tvDay.text = day.dayNumber
|
||||
// This is a placeholder, make an empty spot
|
||||
if (day == null) {
|
||||
holder.tvDay.text = ""
|
||||
holder.tvDay.setBackgroundResource(android.R.color.transparent)
|
||||
holder.itemView.setOnClickListener(null) // Disable click
|
||||
return
|
||||
}
|
||||
|
||||
holder.tvDay.text = day.date.dayOfMonth.toString()
|
||||
|
||||
// Visually indicate days with events
|
||||
if (day.events.isNotEmpty()) {
|
||||
|
@ -35,9 +42,7 @@ class CalendarAdapter(private val days: List<CalendarDay>, private val onDayClic
|
|||
}
|
||||
|
||||
// Trigger the click listener with the clicked day
|
||||
holder.itemView.setOnClickListener {
|
||||
onDayClicked(day)
|
||||
}
|
||||
holder.itemView.setOnClickListener { onDayClicked(day) }
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int = days.size
|
||||
|
|
|
@ -3,9 +3,10 @@ package com.p_vacho.neat_calendar.models
|
|||
import android.os.Parcelable
|
||||
import com.p_vacho.neat_calendar.api.models.EventResponse
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import java.time.LocalDate
|
||||
|
||||
@Parcelize
|
||||
data class CalendarDay(
|
||||
val dayNumber: String,
|
||||
val events: List<EventResponse> = emptyList() // Add this property
|
||||
val date: LocalDate,
|
||||
val events: List<EventResponse> = emptyList()
|
||||
): Parcelable
|
Loading…
Reference in a new issue