hi-manshu / Kalendar

Kalendar is a powerful and customizable calendar library for Android applications. It provides a flexible and intuitive way to display and interact with calendars in your app. With Kalendar, you can easily render calendar views, handle date selection, pagination, and range selection, and customize the layout to match your app's design.
https://www.himanshoe.com
Apache License 2.0
761 stars 66 forks source link

How to change the month of calendar #164

Open ParkJongJoon7128 opened 9 months ago

ParkJongJoon7128 commented 9 months ago

If I click on the text with the text "<" and ">", I'm implementing the code to move on to the previous month and the next month. In addition, if I click text "<" and ">", I'm trying to implement an automatic change in the days that matches the changed month as well as the previous month and the next month. But I'm asking because it doesn't work out well. How can I modify the code?

var selectedTodo by remember { mutableStateOf<RToDoResponse?>(null) }

    val selectedDate = LocalDate.of(
        year.toInt(), month.toInt(), day.toInt()
    )

    var previousSelectedDate by remember { mutableStateOf(selectedDate) }

    val today = Clock.System.todayIn(currentSystemDefault())
    val displayedMonth = remember { mutableStateOf(today.month) }
    val displayedYear = remember { mutableStateOf(today.year) }
    val currentMonth = displayedMonth.value
    val currentYear = displayedYear.value
    val currentMonthIndex = currentMonth.value.minus(1)

                    Kalendar(
                        currentDay = null,
                        kalendarType = KalendarType.Firey,
                        kalendarHeaderTextKonfig = KalendarTextKonfig(
                            kalendarTextColor = Color.Black, kalendarTextSize = 22.sp
                        ),
                        kalendarColors = KalendarColors(color = List(12) {
                            KalendarColor(
                                backgroundColor = Color.White,
                                dayBackgroundColor = Color(0xFFFFDAB9),
                                headerTextColor = Color.Black
                            )
                        }),
                        daySelectionMode = DaySelectionMode.Single,
                        headerContent = { month, year ->
                            Row(
                                modifier = Modifier.padding(
                                    start = 20.dp,
                                    end = 20.dp,
                                    bottom = 16.dp
                                ),
                                verticalAlignment = Alignment.CenterVertically,
                                horizontalArrangement = Arrangement.Start
                            ) {
                                Image(
                                    modifier = Modifier.size(22.dp), painter = painterResource(
                                        id = R.drawable.headertextemogi
                                    ), contentDescription = null
                                )

                                Spacer(modifier = Modifier.width(4.dp))

                                Text(
                                    text = if (currentMonth.value.toString().length < 2) {
                                        "${currentYear}. 0${currentMonth.value}"
                                    } else {
                                        "${currentYear}. ${currentMonth.value}"
                                    },
                                    fontSize = 22.sp,
                                    lineHeight = 28.6.sp,
                                    fontWeight = FontWeight(700),
                                    color = Color(0xFF000000)
                                )

                                Spacer(modifier = Modifier.weight(1f))

                                Text(
                                    modifier = Modifier.clickable {
                                        displayedYear.value -= if (currentMonth == Month.JANUARY) 1 else 0
                                        displayedMonth.value -= 1
                                    },
                                    text = "<",
                                    fontSize = 22.sp,
                                    lineHeight = 28.6.sp,
                                    fontWeight = FontWeight(700),
                                    color = Color(0xFF000000)
                                )

                                Spacer(modifier = Modifier.padding(horizontal = 8.dp))

                                Text(
                                    modifier = Modifier.clickable {
                                        displayedYear.value -= if (currentMonth == Month.JANUARY) 1 else 0
                                        displayedMonth.value -= 1
                                    },
                                    text = ">",
                                    fontSize = 22.sp,
                                    lineHeight = 28.6.sp,
                                    fontWeight = FontWeight(700),
                                    color = Color(0xFF000000)
                                )
                            }
                        },
                        showLabel = true,
                        dayContent = { date: kotlinx.datetime.LocalDate ->
                            val dayOfWeek = calculateDay(
                                date.dayOfMonth,
                                displayedMonth.value,
                                displayedYear.value
                            ).dayOfWeek
                            Box(
                                modifier = Modifier
                                    .padding(10.dp)
                                    .size(36.dp)
                                    .background(
                                        color = if (selectedDate == date.toJavaLocalDate()) {
                                            Color(0xFFFFDAB9)
                                        } else if (previousSelectedDate == date.toJavaLocalDate()) {
                                            Color(0xffE9E9E9)
                                        } else {
                                            Color.White
                                        },
                                        CircleShape
                                    )
                                    .pointerInput(Unit) {
                                        detectTapGestures(onPress = {
                            }
                        }
                    )

@RequiresApi(Build.VERSION_CODES.O)
private fun calculateDay(
    day: Int,
    currentMonth: Month,
    currentYear: Int
): kotlinx.datetime.LocalDate {
    val monthValue = currentMonth.value.toString().padStart(2, '0')
    val dayValue = day.toString().padStart(2, '0')
    return "$currentYear-$monthValue-$dayValue".toLocalDate()
}

@RequiresApi(Build.VERSION_CODES.O)
private fun getFirstDayOfMonth(firstDayOfMonth: DayOfWeek) = -(firstDayOfMonth.value).minus(2)