kizitonwose / Calendar

A highly customizable calendar view and compose library for Android.
MIT License
4.5k stars 492 forks source link

Possible to know if Day is in last week of month? #491

Closed hscissors closed 9 months ago

hscissors commented 10 months ago

Library information:

Question:

I need to set border on the bottom of each week, except of the last week of the month. I'm doing this by adding it to every DayContent object. Is it possible to figure when that day is in the last week so I can not set it for those days?

kizitonwose commented 10 months ago

It's a bit tricky but since the header of each month is bound before the days, if you have a month header, you can do this:

class MyHeaderBinder: MonthHeaderFooterBinder<MonthViewContainer> {
    private val store = mutableMapOf<YearMonth, CalendarMonth>()
    override fun create(view: View): MonthViewContainer {
        // ... return header view
    }

    override fun bind(container: MonthViewContainer, data: CalendarMonth) {
        store[data.yearMonth] = data
        /// ... bind header
    }

    fun isLastRow(day: CalendarDay): Boolean {
        return store[day.date.yearMonth]?.weekDays?.lastOrNull().orEmpty().contains(day)
    }
}

// Set the header binder
calendarView.monthHeaderBinder = MyHeaderBinder()

// Day binder
calendarView.dayBinder = object : MonthDayBinder<DayViewContainer> {
    override fun create(view: View) = DayViewContainer(view)
    override fun bind(container: DayViewContainer, data: CalendarDay) {
        val headerBinder = calendarView.monthHeaderBinder as MyHeaderBinder
        if (headerBinder.isLastRow(data)) {
            // last row
        } else {
            // not last row
        }
        // other biding logic
    }
}
hscissors commented 9 months ago

Oh I 'm using Compose not the View-based version.

kizitonwose commented 9 months ago

I think the core idea in the logic applies to the compose library as well. The key is that the month will be bound first, you just need to store the month info somewhere and use it when the day is bound.

kizitonwose commented 9 months ago

Closing this issue as resolved, please reopen if you need further assistance.