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

Commit 2e50ffdb authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Snap Slider on finish editing." into main

parents dedd3f0f 1ac4aee9
Loading
Loading
Loading
Loading
+15 −6
Original line number Diff line number Diff line
@@ -109,14 +109,19 @@ constructor(
                },
                userVolumeUpdates,
            ) { isDisabledByZenMode, model, icon, currentVolumeUpdate ->
                val shouldIgnoreUpdates =
                val isInGracePeriod =
                    currentVolumeUpdate != null &&
                        getTimestampMillis() - currentVolumeUpdate.timestampMillis <
                            VOLUME_UPDATE_GRACE_PERIOD
                val isMinimumVolume = model.levelMin == currentVolumeUpdate?.level
                VolumeDialogSliderStateModel(
                    value =
                        if (currentVolumeUpdate != null && shouldIgnoreUpdates) {
                            currentVolumeUpdate.newVolumeLevel
                        if (currentVolumeUpdate != null && isInGracePeriod) {
                            if (isMinimumVolume) {
                                model.levelMin.toFloat()
                            } else {
                                currentVolumeUpdate.volume
                            }
                        } else {
                            model.level.toFloat()
                        },
@@ -131,7 +136,7 @@ constructor(

    init {
        userVolumeUpdates
            .mapNotNull { it?.newVolumeLevel?.roundToInt() }
            .mapNotNull { it?.level }
            .distinctUntilChanged()
            .mapLatest { volume ->
                interactor.setStreamVolume(volume)
@@ -144,7 +149,7 @@ constructor(
        if (fromUser) {
            visibilityInteractor.resetDismissTimeout()
            userVolumeUpdates.value =
                VolumeUpdate(newVolumeLevel = volume, timestampMillis = getTimestampMillis())
                VolumeUpdate(volume = volume, timestampMillis = getTimestampMillis())
        }
    }

@@ -190,5 +195,9 @@ constructor(

    private fun getTimestampMillis(): Long = systemClock.uptimeMillis()

    private data class VolumeUpdate(val newVolumeLevel: Float, val timestampMillis: Long)
    private data class VolumeUpdate(val volume: Float, val timestampMillis: Long) {

        val level: Int
            get() = volume.roundToInt()
    }
}
+15 −0
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.ProgressBarRangeInfo
@@ -83,6 +84,7 @@ fun Slider(
    },
) {
    require(stepDistance >= 0f) { "stepDistance must not be negative" }
    val coroutineScope = rememberCoroutineScope()
    var animationJob: Job? by remember { mutableStateOf(null) }
    val sliderState = remember(valueRange) { SliderState(value = value, valueRange = valueRange) }
    LaunchedEffect(value) {
@@ -123,6 +125,19 @@ fun Slider(
    sliderState.onValueChangeFinished = {
        hapticsViewModel?.onValueChangeEnded()
        onValueChangeFinished?.invoke(sliderState.value)
        if (sliderState.value != value) {
            animationJob?.cancel()
            animationJob =
                coroutineScope.launchTraced("Slider#animateValue") {
                    animate(
                        initialValue = sliderState.value,
                        targetValue = value,
                        animationSpec = animationSpec,
                    ) { animatedValue, _ ->
                        sliderState.value = animatedValue
                    }
                }
        }
    }
    sliderState.onValueChange = valueChange