horologist icon indicating copy to clipboard operation
horologist copied to clipboard

WearNavScaffold broken after compose 1.0.0-rc01

Open yschimke opened this issue 3 years ago • 3 comments

https://user-images.githubusercontent.com/231923/174258979-52f8811b-50ef-4f0a-a948-63bc7edc67f1.mov

yschimke avatar Jun 17 '22 08:06 yschimke

the difference appears to be that we don't receive state updates from scrollState.centerItemIndex and scrollState.centerItemScrollOffset the first time we display them. However if you navigate away and then back the updates are fired.

If I put some logging into Modifier.fadeAwayScalingLazyList state, I don't see state fired events, but a simple launched effect polling every 2 seconds does show the right values. And they both see the same list state instance.

@ExperimentalHorologistComposeLayoutApi
public fun Modifier.fadeAwayScalingLazyList(
    initialIndex: Int = 1,
    initialOffset: Int = 0,
    scrollStateFn: () -> ScalingLazyListState,
): Modifier =
    composed {
        val scrollState = remember { scrollStateFn() }

        val centerItemIndex = scrollState.centerItemIndex
        val centerItemScrollOffset = scrollState.centerItemScrollOffset

        LaunchedEffect(Unit) {
            while (true) {
                delay(2.seconds)
                println("Manual: ${scrollState.centerItemScrollOffset} ${scrollState.centerItemIndex} $scrollState")
            }
        }

        SideEffect {
            println("State: $centerItemScrollOffset $centerItemIndex $scrollState")
        }

yschimke avatar Jun 17 '22 09:06 yschimke

Is this workaround still required given this issue has been fixed?

luizgrp avatar Aug 08 '22 08:08 luizgrp

Let me check. Thanks!

yschimke avatar Aug 08 '22 08:08 yschimke

I think that code is still an improvement, specifically this should cut down on recomposes.

        val isInitial by remember(scrollState) {
            derivedStateOf { scrollState.centerItemIndex == initialIndex }
        }

yschimke avatar Sep 01 '22 08:09 yschimke