Skip to content

Commit

Permalink
chore(Compose bump): migrate to compose 1.6 #338
Browse files Browse the repository at this point in the history
  • Loading branch information
kelianClerc authored Feb 12, 2024
2 parents cf4e195 + a80a3d2 commit bb4b3c4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ androidx-appcompat = "1.6.1"
android-material-version = '1.9.0'

# Compose
compose-bom = "2023.10.01"
compose-bom = "2024.02.00"
compose-kotlin-compiler = "1.5.7"

# Image
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package com.spendesk.grapes.compose.calendar

import androidx.compose.material3.DatePicker
import androidx.compose.material3.DatePickerDefaults
import androidx.compose.material3.DatePickerState
import androidx.compose.material3.DisplayMode
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.SelectableDates
import androidx.compose.material3.rememberDatePickerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.spendesk.grapes.compose.extensions.resetDateToMidnight
Expand Down Expand Up @@ -40,14 +40,22 @@ fun GrapesDatePicker(
maxDate: Date? = null,
onDateSelected: ((Date) -> Unit)? = null
) {
val selectedDate = remember(date, minDate, maxDate) {
DatePickerState(
initialSelectedDateMillis = date?.time,
initialDisplayedMonthMillis = date?.time,
yearRange = DatePickerDefaults.YearRange,
initialDisplayMode = DisplayMode.Picker
)
}
val selectedDate = rememberDatePickerState(
initialSelectedDateMillis = date?.time,
initialDisplayedMonthMillis = date?.time,
yearRange = DatePickerDefaults.YearRange,
initialDisplayMode = DisplayMode.Picker,
selectableDates = object : SelectableDates {
override fun isSelectableDate(utcTimeMillis: Long): Boolean {
val calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")).apply { timeInMillis = utcTimeMillis }

val isAfterMinDate = minDate?.let { calendar.time.after(it.resetDateToMidnight()) } ?: true
val isBeforeMaxDate = maxDate?.let { calendar.time.before(it.resetDateToTomorrowMidnight()) } ?: true

return isAfterMinDate && isBeforeMaxDate
}
}
)

DatePicker(
state = selectedDate,
Expand All @@ -71,14 +79,6 @@ fun GrapesDatePicker(
todayContentColor = GrapesTheme.colors.mainPrimaryNormal,
todayDateBorderColor = GrapesTheme.colors.mainPrimaryNormal
),
dateValidator = { utcDateInMills ->
val calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")).apply { timeInMillis = utcDateInMills }

val isAfterMinDate = minDate?.let { calendar.time.after(it.resetDateToMidnight()) } ?: true
val isBeforeMaxDate = maxDate?.let { calendar.time.before(it.resetDateToTomorrowMidnight()) } ?: true

isAfterMinDate && isBeforeMaxDate
}
)

LaunchedEffect(selectedDate.selectedDateMillis) {
Expand Down

0 comments on commit bb4b3c4

Please sign in to comment.