Android Kaki

Build beautiful, usable products using required Components for Android.

Learn how to construct a time picker with Jetpack Compose


24 hour time choice dialog
@Composable
enjoyable TimerPicker(
onCancel: () -> Unit,
onOk: (Time) -> Unit,
modifier: Modifier = Modifier
)
information class Time(val hour: Int, val minute: Int)
var selectedPart by bear in mind { mutableStateOf(TimePart.Hour) }
var selectedHour by bear in mind { mutableStateOf(0) }
var selectedMinute by bear in mind { mutableStateOf(0) }
val selectedIndex by bear in mind {
derivedStateOf { if (selectedPart == TimePart.Hour) selectedHour else selectedMinute / 5 }
}


val onTime: (Int) -> Unit = bear in mind {
{ if (selectedPart == TimePart.Hour) selectedHour = it else selectedMinute = it * 5 }
}

enum class TimePart { Hour, Minute }
@Composable
enjoyable TimeCard(
time: Int,
isSelected: Boolean,
onClick: () -> Unit
) {
Card(
form = RoundedCornerShape(6.dp),
backgroundColor = if (isSelected) selectedColor else secondaryColor,
modifier = Modifier.clickable { onClick() }
) {
Textual content(
textual content = if (time == 0) "00" else time.toString(),
fontSize = 32.sp,
coloration = if (isSelected) secondaryColor else Coloration.White,
modifier = Modifier.padding(horizontal = 12.dp, vertical = 4.dp)
)
}
}
Row(
modifier = Modifier.align(Alignment.CenterHorizontally)
) {
TimeCard(
time = selectedHour,
isSelected = selectedPart == TimePart.Hour,
onClick = { selectedPart = TimePart.Hour }
)
Textual content(
textual content = ":",
fontSize = 32.sp,
coloration = Coloration.White,
modifier = Modifier.padding(horizontal = 2.dp)
)


TimeCard(
time = selectedMinute,
isSelected = selectedPart == TimePart.Minute,
onClick = { selectedPart = TimePart.Minute }
)
}

@Composable
enjoyable Clock(
index: Int,
modifier: Modifier = Modifier,
content material: @Composable () -> Unit
) {
val localDensity = LocalDensity.present
var radiusPx by bear in mind { mutableStateOf(0) }
val radiusInsidePx by bear in mind { derivedStateOf { (radiusPx * 0.67).toInt() } }


var indexCirclePx by bear in mind { mutableStateOf(36f) }
val padding by bear in mind {
derivedStateOf { with(localDensity) { (indexCirclePx * 0.5).toInt().toDp() } }
}
...
}

enjoyable posX(index: Int) =
((if (index enjoyable posY(index: Int) =
((if (index
non-public const val step = PI * 2 / 12
non-public enjoyable angleForIndex(index: Int) = -PI / 2 + step * index
Field(modifier = modifier) {
Floor(
coloration = primaryColor,
form = CircleShape,
modifier = Modifier.fillMaxSize()
) {}
Structure(
content material = content material,
modifier = Modifier
.padding(padding)
.drawBehind {
val finish = Offset(
x = dimension.width / 2 + posX(time),
y = dimension.peak / 2 + posY(time)
)


drawCircle( // #1
radius = 9f,
coloration = selectedColor,
)


drawLine( // #2
begin = middle,
finish = finish,
coloration = selectedColor,
strokeWidth = 4f
)


drawCircle( // #3
radius = indexCirclePx,
middle = finish,
coloration = selectedColor,
)
}
) { measurables, constraints ->
val placeables = measurables.map { it.measure(constraints) }
assert(placeables.depend() == 12 || placeables.depend() == 24) { "Invalid gadgets: must be 12 or 24, is ${placeables.depend()}" }


indexCirclePx = (constraints.maxWidth * 0.07).toFloat() // #4


structure(constraints.maxWidth, constraints.maxHeight) {
val dimension = constraints.maxWidth
val maxElementSize = maxOf(placeables.maxOf { it.width }, placeables.maxOf { it.peak })


radiusPx = (constraints.maxWidth - maxElementSize) / 2 // #5


placeables.forEachIndexed { index, placeable ->
placeable.place( // #6
dimension / 2 - placeable.width / 2 + posX(index),
dimension / 2 - placeable.peak / 2 + posY(index),
)
}
}
}
}

Clock(
time = selectedTime,
modifier = Modifier
.aspectRatio(1f)
.align(Alignment.CenterHorizontally)
) {
ClockMarks24h(selectedPart, selectedTime, onTime)
}
@Composable
enjoyable ClockMarks24h(selectedPart: TimePart, selectedTime: Int, onTime: (Int) -> Unit) {
if (selectedPart == TimePart.Hour) {
Mark(textual content = "00", index = 0, isSelected = selectedTime == 0, onIndex = onTime)
(1..23).map {
Mark(textual content = it.toString(), index = it, isSelected = selectedTime == it, onIndex = onTime)
}
} else {
Mark(textual content = "00", index = 0, isSelected = selectedTime == 0, onIndex = onTime)
Mark(textual content = "05", index = 1, isSelected = selectedTime == 1, onIndex = onTime)
(2..11).map {
Mark(textual content = (it * 5).toString(), index = it, isSelected = selectedTime == it, onIndex = onTime)
}
}
}
@Composable
enjoyable Mark(
textual content: String,
index: Int, // 0..23
onIndex: (Int) -> Unit,
isSelected: Boolean
) {
Textual content(
textual content = textual content,
coloration = if (isSelected) secondaryColor else Coloration.White,
modifier = Modifier.clickable(
interactionSource = bear in mind { MutableInteractionSource() },
indication = null,
onClick = { onIndex(index) }
)
)
}
@Composable
enjoyable TimerPickerDialog() {
val localContext = LocalContext.present
Dialog(onDismissRequest = { /*TODO*/ }) {
Field(modifier = Modifier.fillMaxWidth()) {
TimerPicker(
onOk = { Toast.makeText(localContext, it.toString(), Toast.LENGTH_SHORT).present() },
onCancel = {},
modifier = Modifier
.fillMaxWidth(0.8f)
.align(Alignment.Heart)
)
}
}
}

John Wick: Chapter 4 (FREE) FULLMOVIE The Super Mario Bros Movie avatar 2 Where To Watch Creed 3 Free At Home Knock at the Cabin (2023) FullMovie Where To Watch Ant-Man 3 and the Wasp: Quantumania Cocaine Bear 2023 (FullMovie) Scream 6 Full Movie

Supply hyperlink

Updated: March 28, 2023 — 4:40 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

androidkaki.com © 2023 Android kaki