kizitonwose / Calendar

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

Incorrect date showing on some devices #482

Closed nicemak closed 10 months ago

nicemak commented 11 months ago

Library information:

Question:

Incorrect date showing on some devices,

val daysOfWeek = daysOfWeekFromLocale()

        val currentMonth = YearMonth.now()
        b.calendarView.setup(currentMonth.minusMonths(0), currentMonth.plusMonths(1), daysOfWeek.first())
        b.calendarView.scrollToMonth(currentMonth)
fun daysOfWeekFromLocale() : Array<DayOfWeek>
{
    val firstDayOfWeek = WeekFields.of(Locale.getDefault()).firstDayOfWeek
    var daysOfWeek = DayOfWeek.values()
    // Order `daysOfWeek` array so that firstDayOfWeek is at index 0.
    if (firstDayOfWeek != DayOfWeek.MONDAY)
    {
        val rhs = daysOfWeek.sliceArray(firstDayOfWeek.ordinal..daysOfWeek.indices.last)
        val lhs = daysOfWeek.sliceArray(0 until firstDayOfWeek.ordinal)
        daysOfWeek = rhs + lhs
    }
    return daysOfWeek
}
class MonthViewContainer(view: View) : ViewContainer(view)
        {
            val binding: CalendarDayLegendBinding by lazy { CalendarDayLegendBinding.inflate(layoutInflater) }
            val legendLayout = binding.legendLayout
        }

        b.calendarView.monthHeaderBinder = object : MonthHeaderFooterBinder<MonthViewContainer>
        {
            override fun create(view: View) = MonthViewContainer(view)
            override fun bind(container: MonthViewContainer, month: CalendarMonth)
            {
                // Setup each header day text if we have not done that already.
                if (container.legendLayout.tag == null)
                {
                    container.legendLayout.tag = month.yearMonth
                    container.legendLayout.children.map { it as TextView }
                        .forEachIndexed { index, tv ->
                            tv.text =
                                daysOfWeek[index].getDisplayName(TextStyle.SHORT, Locale.ENGLISH)
                                    .uppercase(Locale.ENGLISH)
                            //tv.setTextColorRes(R.color.example_5_text_grey)
                            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f)
                        }
                    month.yearMonth
                }
            }
        }

Please help me out what i am doing wrong

kizitonwose commented 11 months ago

Version 0.3.5 is from 3 years ago, the library has changed so much since then, so I'd recommend that you update the library and see if the issue persists.

kizitonwose commented 10 months ago

I'll close this issue, feel free to reopen it if the issue persists in the latest version.