feat: Visually distinguish days with events

This commit is contained in:
Peter Vacho 2024-12-24 16:58:08 +01:00
parent 40ca0eaa13
commit 86bbde2f29
Signed by: school
GPG key ID: 8CFC3837052871B4
3 changed files with 34 additions and 11 deletions

View file

@ -12,10 +12,8 @@ import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.p_vacho.neat_calendar.R
import org.threeten.bp.LocalDate
import org.threeten.bp.YearMonth
import org.threeten.bp.format.TextStyle
import java.time.temporal.WeekFields
import java.util.Locale
class CalendarActivity : AppCompatActivity() {
@ -43,25 +41,28 @@ class CalendarActivity : AppCompatActivity() {
val firstDayOfMonth = yearMonth.atDay(1)
val daysInMonth = yearMonth.lengthOfMonth()
// TODO: Replace with an API call to obtain the events
val eventDays = setOf(3, 15, 20)
// Get localized month name and year
val monthYearText = "${yearMonth.month.getDisplayName(TextStyle.FULL, Locale.getDefault())} ${yearMonth.year}"
tvMonthYear.text = monthYearText
// Calculate the day of the week the month starts on (Monday as first day)
val dayOfWeek = firstDayOfMonth.dayOfWeek.value // 1 (Monday) to 7 (Sunday)
val startOffset = (dayOfWeek - 1 + 7) % 7 // Shift Sunday (7) to the correct position for Monday start
val startOffset = (dayOfWeek - 1 + 7) % 7
// Generate calendar days
val days = mutableListOf<String>()
val days = mutableListOf<Day>()
// Add padding for days of the previous month
for (i in 1..startOffset) {
days.add("") // Empty strings represent padding days
days.add(Day(dayNumber = ""))
}
// Add the actual days of the current month
for (i in 1..daysInMonth) {
days.add(i.toString())
days.add(Day(dayNumber = i.toString(), hasEvents = eventDays.contains(i)))
}
// Set up RecyclerView
@ -70,22 +71,37 @@ class CalendarActivity : AppCompatActivity() {
}
}
class CalendarAdapter(private val days: List<String>) :
data class Day(
val dayNumber: String, // Day of the month (e.g., "1", "2", ...)
val hasEvents: Boolean = false
)
class CalendarAdapter(private val days: List<Day>) :
RecyclerView.Adapter<CalendarAdapter.DayViewHolder>() {
class DayViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val tvDay: TextView = itemView.findViewById(android.R.id.text1)
val tvDay: TextView = itemView.findViewById(R.id.tvDay)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DayViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(android.R.layout.simple_list_item_1, parent, false)
.inflate(R.layout.item_calendar_day, parent, false)
return DayViewHolder(view)
}
override fun onBindViewHolder(holder: DayViewHolder, position: Int) {
holder.tvDay.text = days[position]
val day = days[position]
// Set the day number
holder.tvDay.text = day.dayNumber
// Visually indicate days with events
if (day.hasEvents) {
holder.tvDay.setBackgroundResource(R.drawable.event_indicator_background)
} else {
holder.tvDay.setBackgroundResource(android.R.color.transparent)
}
}
override fun getItemCount(): Int = days.size
}
}

View file

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/event_indicator_color" />
<corners android:radius="20dp" />
</shape>

View file

@ -5,4 +5,6 @@
<color name="splash_light_background">#F5F5F5</color> <!-- Light gray -->
<color name="splash_dark_background">#121212</color> <!-- Dark gray -->
<color name="event_indicator_color">#AA0035D0</color>
</resources>