Skip to content

Commit

Permalink
Added ability to hide date in WheelDatePicker
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiopec committed Feb 14, 2023
1 parent 66305bd commit bfcd7f1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 35 deletions.
Expand Up @@ -17,6 +17,7 @@ import java.time.LocalDate
fun WheelDatePicker(
modifier: Modifier = Modifier,
startDate: LocalDate = LocalDate.now(),
showDate: Boolean = true,
minDate: LocalDate = LocalDate.MIN,
maxDate: LocalDate = LocalDate.MAX,
yearsRange: IntRange? = IntRange(1922, 2122),
Expand All @@ -30,6 +31,7 @@ fun WheelDatePicker(
DefaultWheelDatePicker(
modifier,
startDate,
showDate,
minDate,
maxDate,
yearsRange,
Expand Down
Expand Up @@ -20,6 +20,7 @@ import java.time.LocalDate
internal fun DefaultWheelDatePicker(
modifier: Modifier = Modifier,
startDate: LocalDate = LocalDate.now(),
showDate: Boolean = true,
minDate: LocalDate = LocalDate.MIN,
maxDate: LocalDate = LocalDate.MAX,
yearsRange: IntRange? = IntRange(1922, 2122),
Expand All @@ -31,8 +32,11 @@ internal fun DefaultWheelDatePicker(
onSnappedDate : (snappedDate: SnappedDate) -> Int? = { _ -> null }
) {
var snappedDate by remember { mutableStateOf(startDate) }
var dayOfMonths: List<DayOfMonth> = emptyList()

var dayOfMonths = calculateDayOfMonths(snappedDate.month.value, snappedDate.year)
if(showDate) {
dayOfMonths = calculateDayOfMonths(snappedDate.month.value, snappedDate.year)
}

val months = (1..12).map {
Month(
Expand Down Expand Up @@ -63,50 +67,57 @@ internal fun DefaultWheelDatePicker(
) {}
}
Row {
//Day of Month
WheelTextPicker(
size = DpSize(
width = if(yearsRange == null) size.width / 2 else size.width / 3,
height = size.height
),
texts = dayOfMonths.map { it.text },
rowCount = rowCount,
style = textStyle,
color = textColor,
selectorProperties = WheelPickerDefaults.selectorProperties(
enabled = false
),
startIndex = dayOfMonths.find { it.value== startDate.dayOfMonth }?.index ?: 0,
onScrollFinished = { snappedIndex ->
if(showDate){
//Day of Month
WheelTextPicker(
size = DpSize(
width = if(yearsRange == null) size.width / 2 else size.width / 3,
height = size.height
),
texts = dayOfMonths.map { it.text },
rowCount = rowCount,
style = textStyle,
color = textColor,
selectorProperties = WheelPickerDefaults.selectorProperties(
enabled = false
),
startIndex = dayOfMonths.find { it.value== startDate.dayOfMonth }?.index ?: 0,
onScrollFinished = { snappedIndex ->

val newDayOfMonth = dayOfMonths.find { it.index == snappedIndex }?.value
val newDayOfMonth = dayOfMonths.find { it.index == snappedIndex }?.value

newDayOfMonth?.let {
val newDate = snappedDate.withDayOfMonth(newDayOfMonth)
newDayOfMonth?.let {
val newDate = snappedDate.withDayOfMonth(newDayOfMonth)

if(!newDate.isBefore(minDate) && !newDate.isAfter(maxDate)) {
snappedDate = newDate
}
if(!newDate.isBefore(minDate) && !newDate.isAfter(maxDate)) {
snappedDate = newDate
}

val newIndex = dayOfMonths.find { it.value == snappedDate.dayOfMonth }?.index
val newIndex = dayOfMonths.find { it.value == snappedDate.dayOfMonth }?.index

newIndex?.let {
onSnappedDate(
SnappedDate.DayOfMonth(
localDate = snappedDate,
index = newIndex
)
)?.let { return@WheelTextPicker it }
newIndex?.let {
onSnappedDate(
SnappedDate.DayOfMonth(
localDate = snappedDate,
index = newIndex
)
)?.let { return@WheelTextPicker it }
}
}

return@WheelTextPicker dayOfMonths.find { it.value == snappedDate.dayOfMonth }?.index
}
)
}

return@WheelTextPicker dayOfMonths.find { it.value == snappedDate.dayOfMonth }?.index
}
)
//Month
WheelTextPicker(
size = DpSize(
width = if(yearsRange == null) size.width / 2 else size.width / 3,
width = when {
yearsRange == null && showDate.not() -> size.width
yearsRange == null || showDate.not() -> size.width / 2
else -> size.width / 3
},
height = size.height
),
texts = months.map { it.text },
Expand Down Expand Up @@ -151,7 +162,7 @@ internal fun DefaultWheelDatePicker(
years?.let { years ->
WheelTextPicker(
size = DpSize(
width = size.width / 3,
width = if (showDate) size.width / 3 else size.width / 2,
height = size.height
),
texts = years.map { it.text },
Expand Down

0 comments on commit bfcd7f1

Please sign in to comment.