Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4d38d711 authored by Juan Sebastian Martinez's avatar Juan Sebastian Martinez
Browse files

Responding to Volume slider changes in discrete steps for haptics.

Calls to the haptics view-model are not performed only on discrete step
changes of the animated slider value. The previous discrete step is also
remembered to avoid calling haptics for the same stepped value.

Test: manual. Verified haptics are played for every discrete step
  changed of the sliders in the volume panel.
Flag: com.android.systemui.haptics_for_compose_sliders
Bug: 382081935

Change-Id: I3cb27d91701b3def3e39f1c083c7bbe5275b4b81
parent 3633fa1c
Loading
Loading
Loading
Loading
+17 −3
Original line number Diff line number Diff line
@@ -37,12 +37,14 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Slider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.CustomAccessibilityAction
@@ -66,6 +68,10 @@ import com.android.systemui.haptics.slider.SliderHapticFeedbackConfig
import com.android.systemui.haptics.slider.compose.ui.SliderHapticsViewModel
import com.android.systemui.lifecycle.rememberViewModel
import com.android.systemui.volume.panel.component.volume.slider.ui.viewmodel.SliderState
import kotlin.math.round
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.map

@Composable
fun VolumeSlider(
@@ -196,9 +202,17 @@ private fun LegacyVolumeSlider(
                )
            }
        }

    // Perform haptics due to UI composition
    hapticsViewModel?.onValueChange(value)
    var lastDiscreteStep by remember { mutableFloatStateOf(round(value)) }
    LaunchedEffect(value) {
        snapshotFlow { value }
            .map { round(it) }
            .filter { it != lastDiscreteStep }
            .distinctUntilChanged()
            .collect { discreteStep ->
                lastDiscreteStep = discreteStep
                hapticsViewModel?.onValueChange(discreteStep)
            }
    }

    PlatformSlider(
        modifier =