Skip to content

Commit

Permalink
infinite loop feature removed.
Browse files Browse the repository at this point in the history
  • Loading branch information
commandiron committed Sep 12, 2022
1 parent b3b8ba0 commit 3dfe027
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,26 @@ class MainActivity : ComponentActivity() {
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
WheelDateTimePicker { snappedDate, snappedTime -> }
WheelDatePicker { snappedDate -> }
WheelTimePicker { snappedTime -> }
WheelTextPicker(texts = (1..6).map { it.toString() })
WheelPicker(count = 6) { index, snappedIndex ->
Card(Modifier.size(128.dp).padding(8.dp)) {}
}
// WheelDateTimePicker(
// size = DpSize(200.dp, 100.dp),
// textStyle = MaterialTheme.typography.titleSmall,
// textColor = Color(0xFFffc300),
// infiniteLoopEnabled = true,
// selectorEnabled = true,
// selectorShape = RoundedCornerShape(0.dp),
// selectorColor = Color(0xFFf1faee).copy(alpha = 0.2f),
// selectorBorder = BorderStroke(2.dp, Color(0xFFf1faee))
// ) { snappedDate, snappedTime -> }
// WheelDateTimePicker { snappedDate, snappedTime ->
// println(snappedDate)
// println(snappedTime)
// }
// WheelDatePicker { snappedDate -> }
// WheelTimePicker { snappedTime -> }
// WheelTextPicker(texts = (1..6).map { it.toString() })
// WheelPicker(count = 6) { index, snappedIndex ->
// Card(Modifier.size(128.dp).padding(8.dp)) {}
// }
WheelDateTimePicker(
disablePastDate = true,
size = DpSize(200.dp, 100.dp),
textStyle = MaterialTheme.typography.titleSmall,
textColor = Color(0xFFffc300),
selectorEnabled = true,
selectorShape = RoundedCornerShape(0.dp),
selectorColor = Color(0xFFf1faee).copy(alpha = 0.2f),
selectorBorder = BorderStroke(2.dp, Color(0xFFf1faee))
) { snappedDate, snappedTime -> }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
Expand All @@ -22,32 +23,32 @@ import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import java.text.DateFormatSymbols
import java.time.LocalDate
import java.time.LocalTime

@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun WheelDatePicker(
modifier: Modifier = Modifier,
currentDate: LocalDate = LocalDate.now(),
disableBackwards: Boolean = false,
disablePastDate: Boolean = false,
size: DpSize = DpSize(256.dp, 128.dp),
textStyle: TextStyle = MaterialTheme.typography.titleMedium,
textColor: Color = LocalContentColor.current,
infiniteLoopEnabled: Boolean = false,
selectorEnabled: Boolean = true,
selectorShape: Shape = RoundedCornerShape(16.dp),
selectorColor: Color = MaterialTheme.colorScheme.primary.copy(alpha = 0.2f),
selectorBorder: BorderStroke? = BorderStroke(1.dp, MaterialTheme.colorScheme.primary),
onScrollFinished : (snappedDate: LocalDate) -> Unit = {}
onScrollFinished: (snappedDate: LocalDate) -> Unit = {},
) {
val dayTexts = remember { mutableStateOf((1..31).toList().map { it.toString() }) }
val selectedDayOfMonth = remember { mutableStateOf(0)}
val selectedDayOfMonth = remember { mutableStateOf(1)}

val monthTexts: List<String> = if(size.width < 250.dp){
DateFormatSymbols().shortMonths.toList()
}else{
DateFormatSymbols().months.toList()
}
val selectedMonth = remember { mutableStateOf(0)}
val selectedMonth = remember { mutableStateOf(1)}

var yearTexts = listOf<String>()
val yearRange = 100
Expand All @@ -72,21 +73,20 @@ fun WheelDatePicker(
texts = dayTexts.value,
textStyle = textStyle,
textColor = textColor,
infiniteLoopEnabled = infiniteLoopEnabled,
selectorEnabled = false,
startIndex = currentDate.dayOfMonth - 1,
onScrollFinished = { selectedIndex ->
selectedDayOfMonth.value =
if(disableBackwards){
if(selectedIndex < currentDate.dayOfMonth - 1 ) {
currentDate.dayOfMonth
}else{
selectedIndex + 1
try {
selectedDayOfMonth.value = selectedIndex + 1
val selectedDate = LocalDate.of(selectedYear.value, selectedMonth.value, selectedDayOfMonth.value)
val isDateBefore = isDateBefore(selectedDate, currentDate)

if(disablePastDate){
if(isDateBefore){
selectedDayOfMonth.value = currentDate.dayOfMonth
}
}else{
selectedIndex + 1
}
try {

onScrollFinished(
LocalDate.of(
selectedYear.value,
Expand All @@ -97,34 +97,30 @@ fun WheelDatePicker(
}catch (e: Exception){
e.printStackTrace()
}
if(disableBackwards && selectedIndex < currentDate.dayOfMonth - 1 ) {
return@WheelTextPicker currentDate.dayOfMonth - 1
}else{
return@WheelTextPicker selectedIndex
}
return@WheelTextPicker selectedDayOfMonth.value - 1
}
)
WheelTextPicker(
size = DpSize(size.width / 3, size.height),
texts = monthTexts,
textStyle = textStyle,
textColor = textColor,
infiniteLoopEnabled = infiniteLoopEnabled,
selectorEnabled = false,
startIndex = currentDate.month.value - 1,
onScrollFinished = { selectedIndex ->
selectedMonth.value =
if(disableBackwards){
if(selectedIndex < currentDate.month.value - 1 ) {
currentDate.month.value
}else{
selectedIndex + 1
try {
dayTexts.value = calculateMonthDayTexts(selectedIndex + 1, selectedYear.value)

selectedMonth.value = selectedIndex + 1
val selectedDate = LocalDate.of(selectedYear.value, selectedMonth.value, selectedDayOfMonth.value)
val isDateBefore = isDateBefore(selectedDate, currentDate)

if(disablePastDate){
if(isDateBefore){
selectedMonth.value = currentDate.month.value
}
}else{
selectedIndex + 1
}
dayTexts.value = calculateMonthDayTexts(selectedMonth.value, selectedYear.value)
try {

onScrollFinished(
LocalDate.of(
selectedYear.value,
Expand All @@ -135,34 +131,30 @@ fun WheelDatePicker(
}catch (e: Exception){
e.printStackTrace()
}
if(disableBackwards && selectedIndex < currentDate.month.value - 1 ) {
return@WheelTextPicker currentDate.month.value - 1
}else{
return@WheelTextPicker selectedIndex
}
return@WheelTextPicker selectedMonth.value - 1
}
)
WheelTextPicker(
size = DpSize(size.width / 3, size.height),
texts = yearTexts,
textStyle = textStyle,
textColor = textColor,
infiniteLoopEnabled = infiniteLoopEnabled,
selectorEnabled = false,
startIndex = yearRange,
onScrollFinished = { selectedIndex ->
selectedYear.value =
if(disableBackwards){
if(yearTexts[selectedIndex].toInt() < currentDate.year ) {
yearTexts[yearRange].toInt()
}else{
yearTexts[selectedIndex].toInt()
try {
dayTexts.value = calculateMonthDayTexts(selectedMonth.value, yearTexts[selectedIndex].toInt())

selectedYear.value = yearTexts[selectedIndex].toInt()
val selectedDate = LocalDate.of(selectedYear.value, selectedMonth.value, selectedDayOfMonth.value)
val isDateBefore = isDateBefore(selectedDate, currentDate)

if(disablePastDate){
if(isDateBefore){
selectedYear.value = yearTexts[yearRange].toInt()
}
}else{
yearTexts[selectedIndex].toInt()
}
dayTexts.value = calculateMonthDayTexts(selectedMonth.value, selectedYear.value)
try {

onScrollFinished(
LocalDate.of(
selectedYear.value,
Expand All @@ -173,11 +165,8 @@ fun WheelDatePicker(
}catch (e: Exception){
e.printStackTrace()
}
if(disableBackwards && yearTexts[selectedIndex].toInt() < currentDate.year ) {
return@WheelTextPicker yearRange
}else{
return@WheelTextPicker selectedIndex
}

return@WheelTextPicker yearTexts.indexOf(selectedYear.value.toString())
}
)
}
Expand Down Expand Up @@ -209,4 +198,9 @@ private fun calculateMonthDayTexts(month: Int, year: Int): List<String> {
12 -> { month31day }
else -> { emptyList() }
}
}

@RequiresApi(Build.VERSION_CODES.O)
private fun isDateBefore(date: LocalDate, currentDate: LocalDate): Boolean{
return date.isBefore(currentDate)
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ import java.time.LocalTime
fun WheelDateTimePicker(
modifier: Modifier = Modifier,
currentDateTime: LocalDateTime = LocalDateTime.now(),
disableDateBackwards: Boolean = false,
disableTimeBackwards: Boolean = false,
disablePastDate: Boolean = false,
disablePastTime: Boolean = false,
size: DpSize = DpSize(256.dp, 128.dp),
textStyle: TextStyle = MaterialTheme.typography.titleMedium,
textColor: Color = LocalContentColor.current,
infiniteLoopEnabled: Boolean = false,
selectorEnabled: Boolean = true,
selectorShape: Shape = RoundedCornerShape(16.dp),
selectorColor: Color = MaterialTheme.colorScheme.primary.copy(alpha = 0.2f),
Expand All @@ -56,24 +55,21 @@ fun WheelDateTimePicker(
Row {
WheelDatePicker(
currentDate = currentDateTime.toLocalDate(),
disableBackwards = disableDateBackwards,
disablePastDate = disablePastDate,
size = DpSize(size.width * 2 / 3, size.height),
textStyle = textStyle,
textColor = textColor,
infiniteLoopEnabled = infiniteLoopEnabled,
selectorEnabled = false,
onScrollFinished = {
currentDate.value = it
onScrollFinished(currentDate.value, currentTime.value)
}
)
) { snappedDate ->
currentDate.value = snappedDate
onScrollFinished(currentDate.value, currentTime.value)
}
WheelTimePicker(
currentTime = currentDateTime.toLocalTime(),
disableBackwards = disableTimeBackwards,
disablePastTime = disablePastTime,
size = DpSize(size.width / 3, size.height),
textStyle = textStyle,
textColor = textColor,
infiniteLoopEnabled = infiniteLoopEnabled,
selectorEnabled = false,
onScrollFinished = {
currentTime.value = it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ fun WheelPicker(
size: DpSize = DpSize(128.dp, 128.dp),
startIndex: Int = 0,
count: Int,
infiniteLoopEnabled: Boolean = false,
selectorEnabled: Boolean = false,
selectorShape: Shape = RoundedCornerShape(0.dp),
selectorColor: Color = MaterialTheme.colorScheme.primary.copy(alpha = 0.2f),
Expand All @@ -38,11 +37,7 @@ fun WheelPicker(
val snappedItem = layoutInfo.currentItem
snappedItem?.let { snapperLayoutItemInfo ->
if(snapperLayoutItemInfo.offset == 0){
if(infiniteLoopEnabled){
snappedIndex.value = snapperLayoutItemInfo.index % count
}else{
snappedIndex.value = snapperLayoutItemInfo.index
}
snappedIndex.value = snapperLayoutItemInfo.index
}else{
snappedIndex.value = snapperLayoutItemInfo.index + 1
}
Expand Down Expand Up @@ -81,8 +76,7 @@ fun WheelPicker(
lazyListState = lazyListState
)
){
items(if(infiniteLoopEnabled) Int.MAX_VALUE else count){ indexInLazyColumn ->
val index = indexInLazyColumn % count
items(count){ index ->
Box(
modifier = Modifier
.height(size.height / 3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ fun WheelTextPicker(
texts: List<String>,
textStyle: TextStyle = MaterialTheme.typography.titleMedium,
textColor: Color = LocalContentColor.current,
infiniteLoopEnabled: Boolean = false,
selectorEnabled: Boolean = true,
selectorShape: Shape = RoundedCornerShape(16.dp),
selectorColor: Color = MaterialTheme.colorScheme.primary.copy(alpha = 0.2f),
Expand All @@ -33,7 +32,6 @@ fun WheelTextPicker(
size = size,
startIndex = startIndex,
count = texts.size,
infiniteLoopEnabled = infiniteLoopEnabled,
selectorEnabled = selectorEnabled,
selectorShape = selectorShape,
selectorColor = selectorColor,
Expand Down
Loading

1 comment on commit 3dfe027

@dubiao
Copy link

@dubiao dubiao commented on 3dfe027 Aug 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason for disable infiniteLoopEnabled ? It's a good feature.

Please sign in to comment.