From a0f42b017643aa022fbfc8caf57e95602c97a889 Mon Sep 17 00:00:00 2001 From: Chiara Chiappini Date: Mon, 1 Dec 2025 21:32:54 +0000 Subject: [PATCH] Adds code for custom rotary --- .../example/wear/snippets/m3/rotary/Rotary.kt | 65 ++++++++++++++++++- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/wear/src/main/java/com/example/wear/snippets/m3/rotary/Rotary.kt b/wear/src/main/java/com/example/wear/snippets/m3/rotary/Rotary.kt index 4cff56480..7089f5149 100644 --- a/wear/src/main/java/com/example/wear/snippets/m3/rotary/Rotary.kt +++ b/wear/src/main/java/com/example/wear/snippets/m3/rotary/Rotary.kt @@ -29,9 +29,11 @@ import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.State import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue @@ -42,8 +44,11 @@ import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.input.pointer.pointerInteropFilter import androidx.compose.ui.input.rotary.onRotaryScrollEvent import androidx.compose.ui.unit.dp +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewmodel.compose.viewModel import androidx.wear.compose.foundation.lazy.ScalingLazyColumn import androidx.wear.compose.foundation.lazy.ScalingLazyColumnDefaults +import androidx.wear.compose.foundation.lazy.TransformingLazyColumn import androidx.wear.compose.foundation.lazy.rememberScalingLazyListState import androidx.wear.compose.foundation.lazy.rememberTransformingLazyColumnState import androidx.wear.compose.material3.Button @@ -57,6 +62,7 @@ import androidx.wear.compose.material3.Text import androidx.wear.compose.material3.rememberPickerState import androidx.wear.compose.ui.tooling.preview.WearPreviewDevices import androidx.wear.compose.ui.tooling.preview.WearPreviewFontScales +import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.launch @Composable @@ -105,7 +111,8 @@ fun TimePicker() { // [END_EXCLUDE] Picker( readOnly = selectedColumn != 0, - modifier = Modifier.size(64.dp, 100.dp) + modifier = Modifier + .size(64.dp, 100.dp) .onRotaryScrollEvent { coroutineScope.launch { hourState.scrollBy(it.verticalScrollPixels) @@ -134,7 +141,8 @@ fun TimePicker() { // [END_EXCLUDE] Picker( readOnly = selectedColumn != 1, - modifier = Modifier.size(64.dp, 100.dp) + modifier = Modifier + .size(64.dp, 100.dp) .onRotaryScrollEvent { coroutineScope.launch { minuteState.scrollBy(it.verticalScrollPixels) @@ -213,6 +221,59 @@ fun PositionScrollIndicator() { // [END android_wear_rotary_position_indicator] } +@Composable +fun VolumeScreen() { + // [START android_wear_rotary_custom_ui] + val focusRequester: FocusRequester = remember { FocusRequester() } + val volumeViewModel: VolumeViewModel.MyViewModel = + viewModel() + val volumeState by volumeViewModel.volumeState + + TransformingLazyColumn( + modifier = Modifier + .fillMaxSize() + .onRotaryScrollEvent { + volumeViewModel.onVolumeChangeByScroll(it.verticalScrollPixels) + true + } + .focusRequester(focusRequester) + .focusable(), + ) { + // You can use volumeState here, for example: + item { + Text("Volume: $volumeState") + } + } + // [END android_wear_rotary_custom_ui] +} + +// [START android_wear_rotary_custom_model] +class VolumeRange( + val max: Int = 10, + val min: Int = 0 +) + +private object VolumeViewModel { + class MyViewModel : ViewModel() { + private val _volumeState = mutableIntStateOf(0) + val volumeState: State + get() = _volumeState + + // ... + fun onVolumeChangeByScroll(pixels: Float) { + _volumeState.value = when { + pixels > 0 -> minOf(volumeState.value + 1, VolumeRange().max) + pixels < 0 -> maxOf(volumeState.value - 1, VolumeRange().min) + else -> volumeState.value + } + } + } +} +// [END android_wear_rotary_custom_model] + + + + @WearPreviewDevices @WearPreviewFontScales @Composable