From 1895e6d70e17388e2d4478d51f25fa68bc8caf76 Mon Sep 17 00:00:00 2001 From: Dmitry Maltsev-Gorskij Date: Thu, 3 Aug 2023 19:10:44 +0300 Subject: [PATCH] - Capitalized first russian month letter --- .../core/DefaultWheelDatePicker.kt | 126 +++++++++++++----- .../extentions/StringExtentions.kt | 10 ++ 2 files changed, 101 insertions(+), 35 deletions(-) create mode 100644 wheel-picker-compose/src/main/java/com/commandiron/wheel_picker_compose/extentions/StringExtentions.kt diff --git a/wheel-picker-compose/src/main/java/com/commandiron/wheel_picker_compose/core/DefaultWheelDatePicker.kt b/wheel-picker-compose/src/main/java/com/commandiron/wheel_picker_compose/core/DefaultWheelDatePicker.kt index c63b834..3b9b5b4 100644 --- a/wheel-picker-compose/src/main/java/com/commandiron/wheel_picker_compose/core/DefaultWheelDatePicker.kt +++ b/wheel-picker-compose/src/main/java/com/commandiron/wheel_picker_compose/core/DefaultWheelDatePicker.kt @@ -1,20 +1,28 @@ package com.commandiron.wheel_picker_compose.core +import android.os.Build +import android.os.LocaleList import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.size import androidx.compose.material3.LocalContentColor import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface -import androidx.compose.runtime.* +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.TextStyle import androidx.compose.ui.unit.DpSize import androidx.compose.ui.unit.dp +import com.commandiron.wheel_picker_compose.extentions.replaceFirstCharIf import java.text.DateFormatSymbols import java.time.LocalDate +import java.util.Locale @Composable internal fun DefaultWheelDatePicker( @@ -28,19 +36,29 @@ internal fun DefaultWheelDatePicker( textStyle: TextStyle = MaterialTheme.typography.titleMedium, textColor: Color = LocalContentColor.current, selectorProperties: SelectorProperties = WheelPickerDefaults.selectorProperties(), - onSnappedDate : (snappedDate: SnappedDate) -> Int? = { _ -> null } + onSnappedDate: (snappedDate: SnappedDate) -> Int? = { _ -> null } ) { var snappedDate by remember { mutableStateOf(startDate) } var dayOfMonths = calculateDayOfMonths(snappedDate.month.value, snappedDate.year) - val months = (1..12).map { + val months = (1..12).map { monthNumber -> Month( - text = if(size.width / 3 < 55.dp){ - DateFormatSymbols().shortMonths[it - 1] - } else DateFormatSymbols().months[it - 1], - value = it, - index = it - 1 + text = if (size.width / 3 < 55.dp) { + DateFormatSymbols().shortMonths[monthNumber - 1] + } else DateFormatSymbols().months[monthNumber - 1] + .replaceFirstCharIf( + condition = { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + LocaleList.getDefault()[0].language == "ru" + } else { + Locale.getDefault().language == "ru" + } + }, + transform = { it.uppercase() } + ), + value = monthNumber, + index = monthNumber - 1 ) } @@ -52,8 +70,8 @@ internal fun DefaultWheelDatePicker( ) } - Box(modifier = modifier, contentAlignment = Alignment.Center){ - if(selectorProperties.enabled().value){ + Box(modifier = modifier, contentAlignment = Alignment.Center) { + if (selectorProperties.enabled().value) { Surface( modifier = Modifier .size(size.width, size.height / rowCount), @@ -66,7 +84,7 @@ internal fun DefaultWheelDatePicker( //Day of Month WheelTextPicker( size = DpSize( - width = if(yearsRange == null) size.width / 2 else size.width / 3, + width = if (yearsRange == null) size.width / 2 else size.width / 3, height = size.height ), texts = dayOfMonths.map { it.text }, @@ -76,7 +94,7 @@ internal fun DefaultWheelDatePicker( selectorProperties = WheelPickerDefaults.selectorProperties( enabled = false ), - startIndex = dayOfMonths.find { it.value== startDate.dayOfMonth }?.index ?: 0, + startIndex = dayOfMonths.find { it.value == startDate.dayOfMonth }?.index ?: 0, onScrollFinished = { snappedIndex -> val newDayOfMonth = dayOfMonths.find { it.index == snappedIndex }?.value @@ -84,11 +102,11 @@ internal fun DefaultWheelDatePicker( newDayOfMonth?.let { val newDate = snappedDate.withDayOfMonth(newDayOfMonth) - if(!newDate.isBefore(minDate) && !newDate.isAfter(maxDate)) { + 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( @@ -106,7 +124,7 @@ internal fun DefaultWheelDatePicker( //Month WheelTextPicker( size = DpSize( - width = if(yearsRange == null) size.width / 2 else size.width / 3, + width = if (yearsRange == null) size.width / 2 else size.width / 3, height = size.height ), texts = months.map { it.text }, @@ -116,7 +134,7 @@ internal fun DefaultWheelDatePicker( selectorProperties = WheelPickerDefaults.selectorProperties( enabled = false ), - startIndex = months.find { it.value== startDate.monthValue }?.index ?: 0, + startIndex = months.find { it.value == startDate.monthValue }?.index ?: 0, onScrollFinished = { snappedIndex -> val newMonth = months.find { it.index == snappedIndex }?.value @@ -125,13 +143,13 @@ internal fun DefaultWheelDatePicker( val newDate = snappedDate.withMonth(newMonth) - if(!newDate.isBefore(minDate) && !newDate.isAfter(maxDate)) { + if (!newDate.isBefore(minDate) && !newDate.isAfter(maxDate)) { snappedDate = newDate } dayOfMonths = calculateDayOfMonths(snappedDate.month.value, snappedDate.year) - val newIndex = months.find { it.value == snappedDate.monthValue }?.index + val newIndex = months.find { it.value == snappedDate.monthValue }?.index newIndex?.let { onSnappedDate( @@ -161,7 +179,7 @@ internal fun DefaultWheelDatePicker( selectorProperties = WheelPickerDefaults.selectorProperties( enabled = false ), - startIndex = years.find { it.value == startDate.year }?.index ?:0, + startIndex = years.find { it.value == startDate.year }?.index ?: 0, onScrollFinished = { snappedIndex -> val newYear = years.find { it.index == snappedIndex }?.value @@ -170,13 +188,13 @@ internal fun DefaultWheelDatePicker( val newDate = snappedDate.withYear(newYear) - if(!newDate.isBefore(minDate) && !newDate.isAfter(maxDate)) { + if (!newDate.isBefore(minDate) && !newDate.isAfter(maxDate)) { snappedDate = newDate } dayOfMonths = calculateDayOfMonths(snappedDate.month.value, snappedDate.year) - val newIndex = years.find { it.value == snappedDate.year }?.index + val newIndex = years.find { it.value == snappedDate.year }?.index newIndex?.let { onSnappedDate( @@ -248,19 +266,57 @@ internal fun calculateDayOfMonths(month: Int, year: Int): List { ) } - return when(month){ - 1 -> { month31day } - 2 -> { if(isLeapYear) month29day else month28day } - 3 -> { month31day } - 4 -> { month30day } - 5 -> { month31day } - 6 -> { month30day } - 7 -> { month31day } - 8 -> { month31day } - 9 -> { month30day } - 10 -> { month31day } - 11 -> { month30day } - 12 -> { month31day } - else -> { emptyList() } + return when (month) { + 1 -> { + month31day + } + + 2 -> { + if (isLeapYear) month29day else month28day + } + + 3 -> { + month31day + } + + 4 -> { + month30day + } + + 5 -> { + month31day + } + + 6 -> { + month30day + } + + 7 -> { + month31day + } + + 8 -> { + month31day + } + + 9 -> { + month30day + } + + 10 -> { + month31day + } + + 11 -> { + month30day + } + + 12 -> { + month31day + } + + else -> { + emptyList() + } } } \ No newline at end of file diff --git a/wheel-picker-compose/src/main/java/com/commandiron/wheel_picker_compose/extentions/StringExtentions.kt b/wheel-picker-compose/src/main/java/com/commandiron/wheel_picker_compose/extentions/StringExtentions.kt new file mode 100644 index 0000000..df21c62 --- /dev/null +++ b/wheel-picker-compose/src/main/java/com/commandiron/wheel_picker_compose/extentions/StringExtentions.kt @@ -0,0 +1,10 @@ +package com.commandiron.wheel_picker_compose.extentions + +inline fun String.replaceFirstCharIf( + condition: () -> Boolean, + transform: (Char) -> CharSequence +): String = if (condition()) { + replaceFirstChar(transform) +} else { + this +} \ No newline at end of file