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

Added a more user friendly time picker #121

Merged
merged 2 commits into from
Aug 13, 2023
Merged
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
182 changes: 0 additions & 182 deletions app/src/main/java/com/bnyro/clock/ui/components/NumberPicker.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.bnyro.clock.ui.components

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.pager.PageSize
import androidx.compose.foundation.pager.PagerDefaults
import androidx.compose.foundation.pager.PagerSnapDistance
import androidx.compose.foundation.pager.VerticalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun ScrollTimePicker(
value: Int,
onValueChanged: (Int) -> Unit,
maxValue: Int
) {
val primary = MaterialTheme.colorScheme.primary
val primaryMuted = MaterialTheme.colorScheme.primary.copy(alpha = 0.3f)
val state = rememberPagerState(initialPage = maxValue * 100 + value - 1)
val currentPage = state.currentPage + 1
LaunchedEffect(currentPage) {
onValueChanged(currentPage % maxValue)
}
VerticalPager(
modifier = Modifier.height(224.dp),
state = state,
pageCount = Int.MAX_VALUE,
pageSpacing = 16.dp,
pageSize = PageSize.Fixed(64.dp),
flingBehavior = PagerDefaults.flingBehavior(
state = state,
pagerSnapDistance = PagerSnapDistance.atMost(60)
)

) { index ->
val number = index % maxValue
Text(
text = String.format("%02d", number),
style = MaterialTheme.typography.displayMedium,
color = if (index == currentPage) primary else primaryMuted
)
}
}

@Preview(showBackground = true)
@Composable
private fun DefaultPreview() {
ScrollTimePicker(value = 0, onValueChanged = {}, maxValue = 60)
}
Loading
Loading