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

Commit de6a2867 authored by Fabián Kozynski's avatar Fabián Kozynski
Browse files

Remove observing offset in composition

Instead, use derived state as it can cause extra unneeded recompositions

Test: manual
Test: PagerDotsScreenshotTest
Flag: com.android.systemui.qs_ui_refactor_compose_fragment
Bug: 369156081
Change-Id: Id298c00f1943954e99c7362e1913d6b28f4af374
parent 9fdd961e
Loading
Loading
Loading
Loading
+17 −2
Original line number Diff line number Diff line
@@ -46,6 +46,8 @@ import androidx.compose.ui.unit.dp
import kotlin.math.absoluteValue
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import platform.test.motion.compose.values.MotionTestValueKey
import platform.test.motion.compose.values.motionTestValues

@Composable
fun PagerDots(
@@ -93,13 +95,22 @@ fun PagerDots(
    }

    Row(
        modifier = modifier.wrapContentWidth().pagerDotsSemantics(pagerState, coroutineScope),
        modifier =
            modifier
                .motionTestValues { activeMarkerWidth exportAs PagerDotsMotionKeys.indicatorWidth }
                .wrapContentWidth()
                .pagerDotsSemantics(pagerState, coroutineScope),
        horizontalArrangement = spacedBy(spaceSize),
        verticalAlignment = Alignment.CenterVertically,
    ) {
        // This means that the active rounded rect has to be drawn between the current page
        // and the previous one (as we are animating back), or the current one if not transitioning
        val withPrevious = pagerState.currentPageOffsetFraction <= 0 || pagerState.isOverscrolling()
        val withPrevious by
            remember(pagerState) {
                derivedStateOf {
                    pagerState.currentPageOffsetFraction <= 0 || pagerState.isOverscrolling()
                }
            }
        repeat(pagerState.pageCount) { page ->
            Canvas(Modifier.size(dotSize)) {
                val rtl = layoutDirection == LayoutDirection.Rtl
@@ -127,6 +138,10 @@ fun PagerDots(
    }
}

object PagerDotsMotionKeys {
    val indicatorWidth = MotionTestValueKey<Dp>("indicatorWidth")
}

private fun Modifier.pagerDotsSemantics(
    pagerState: PagerState,
    coroutineScope: CoroutineScope,