|
| 1 | +package com.example.compose.snippets.components |
| 2 | + |
| 3 | +import androidx.compose.foundation.interaction.MutableInteractionSource |
| 4 | +import androidx.compose.foundation.interaction.collectIsPressedAsState |
| 5 | +import androidx.compose.foundation.layout.Row |
| 6 | +import androidx.compose.foundation.layout.Spacer |
| 7 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 8 | +import androidx.compose.material.icons.Icons |
| 9 | +import androidx.compose.material.icons.filled.Favorite |
| 10 | +import androidx.compose.material.icons.outlined.FastForward |
| 11 | +import androidx.compose.material.icons.outlined.FastRewind |
| 12 | +import androidx.compose.material.icons.outlined.FavoriteBorder |
| 13 | +import androidx.compose.material3.Icon |
| 14 | +import androidx.compose.material3.IconButton |
| 15 | +import androidx.compose.material3.Text |
| 16 | +import androidx.compose.runtime.Composable |
| 17 | +import androidx.compose.runtime.LaunchedEffect |
| 18 | +import androidx.compose.runtime.remember |
| 19 | +import androidx.compose.runtime.rememberUpdatedState |
| 20 | +import androidx.compose.runtime.saveable.rememberSaveable |
| 21 | +import androidx.compose.ui.Alignment |
| 22 | +import androidx.compose.ui.Modifier |
| 23 | +import androidx.compose.ui.graphics.vector.ImageVector |
| 24 | +import androidx.compose.ui.tooling.preview.Preview |
| 25 | +import kotlinx.coroutines.delay |
| 26 | +import androidx.compose.runtime.getValue |
| 27 | +import androidx.compose.runtime.setValue |
| 28 | +import androidx.compose.runtime.mutableStateOf |
| 29 | +import androidx.compose.runtime.mutableIntStateOf |
| 30 | +import androidx.compose.material.icons.filled.FastRewind |
| 31 | +import androidx.compose.material.icons.filled.FastForward |
| 32 | + |
| 33 | +@Preview |
| 34 | +@Composable |
| 35 | +fun ToggleIconButtonExample() { |
| 36 | + // isToggled initial value should be read from a view model or persistent storage. |
| 37 | + var isToggled by rememberSaveable { mutableStateOf(false) } |
| 38 | + |
| 39 | + IconButton( |
| 40 | + onClick = { isToggled = !isToggled } |
| 41 | + ) { |
| 42 | + Icon( |
| 43 | + imageVector = if (isToggled) Icons.Filled.Favorite else Icons.Outlined.FavoriteBorder, |
| 44 | + contentDescription = if (isToggled) "Selected icon button" else "Unselected icon button." |
| 45 | + ) |
| 46 | + } |
| 47 | +} |
| 48 | +@Composable |
| 49 | +fun MomentaryIconButton( |
| 50 | + unselectedImage: ImageVector, |
| 51 | + selectedImage: ImageVector, |
| 52 | + contentDescription: String, |
| 53 | + modifier: Modifier = Modifier, |
| 54 | + stepDelay: Long = 100L, // Minimum value is 1L milliseconds. |
| 55 | + onClick: () -> Unit |
| 56 | +) { |
| 57 | + val interactionSource = remember { MutableInteractionSource() } |
| 58 | + val isPressed by interactionSource.collectIsPressedAsState() |
| 59 | + val pressedListener by rememberUpdatedState(onClick) |
| 60 | + |
| 61 | + LaunchedEffect(isPressed) { |
| 62 | + while (isPressed) { |
| 63 | + delay(stepDelay.coerceIn(1L, Long.MAX_VALUE)) |
| 64 | + pressedListener() |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + IconButton( |
| 69 | + modifier = modifier, |
| 70 | + onClick = onClick, |
| 71 | + interactionSource = interactionSource |
| 72 | + ) { |
| 73 | + Icon( |
| 74 | + imageVector = if (isPressed) selectedImage else unselectedImage, |
| 75 | + contentDescription = contentDescription, |
| 76 | + ) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +@Preview() |
| 81 | +@Composable |
| 82 | +fun MomentaryIconButtonExample() { |
| 83 | + var pressedCount by remember { mutableIntStateOf(0) } |
| 84 | + |
| 85 | + Row( |
| 86 | + modifier = Modifier.fillMaxWidth(), |
| 87 | + verticalAlignment = Alignment.CenterVertically |
| 88 | + ) { |
| 89 | + MomentaryIconButton( |
| 90 | + unselectedImage = Icons.Outlined.FastRewind, |
| 91 | + selectedImage = Icons.Filled.FastRewind, |
| 92 | + stepDelay = 100L, |
| 93 | + onClick = { pressedCount -= 1 }, |
| 94 | + contentDescription = "Decrease count button" |
| 95 | + ) |
| 96 | + Spacer(modifier = Modifier) |
| 97 | + Text("advanced by $pressedCount frames") |
| 98 | + Spacer(modifier = Modifier) |
| 99 | + MomentaryIconButton( |
| 100 | + unselectedImage = Icons.Outlined.FastForward, |
| 101 | + selectedImage = Icons.Filled.FastForward, |
| 102 | + contentDescription = "Increase count button", |
| 103 | + stepDelay = 100L, |
| 104 | + onClick = { pressedCount += 1 } |
| 105 | + ) |
| 106 | + } |
| 107 | +} |
0 commit comments