Skip to content
Merged
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
65 changes: 63 additions & 2 deletions wear/src/main/java/com/example/wear/snippets/m3/rotary/Rotary.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<Int>
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
Expand Down