Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capitalized first russian month letter #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
- Capitalized first russian month letter
  • Loading branch information
maltsev-gorskij committed Aug 3, 2023
commit 1895e6d70e17388e2d4478d51f25fa68bc8caf76
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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
)
}

Expand All @@ -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),
Expand All @@ -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 },
Expand All @@ -76,19 +94,19 @@ 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

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(
Expand All @@ -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 },
Expand All @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -248,19 +266,57 @@ internal fun calculateDayOfMonths(month: Int, year: Int): List<DayOfMonth> {
)
}

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()
}
}
}
Original file line number Diff line number Diff line change
@@ -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
}