Hi, from my understanding UIKitViewController shou...
# compose-ios
d
Hi, from my understanding UIKitViewController should rebuild whenever the inputs change. But for me it never rebuilds after the first time, even though
pagerState.calculateCurrentOffsetForPage
changes very frequently. Am I doing something wrong?
Copy code
@Composable
actual fun NativeAppTopBar(
    modifier: Modifier,
    pagerState: PagerState
) {
    val coroutineScope = rememberCoroutineScope()
    val factory = LocalNativeViewFactory.current
    val appTabs = AppTabs.entries.map { it.label.asString() }

    UIKitViewController(
        modifier = modifier.statusBarsPadding(),
        factory = {
            println("rebuilt")
            factory.createAppTopBarView(
                currentOffsetForPage = { pagerState.calculateCurrentOffsetForPage(it) },
                onClick = { coroutineScope.launch { pagerState.animateScrollToPage(it) } },
                appTabs = appTabs
            )
        }
    )
}
v
pagerState.calculateCurrentOffsetForPage(it)
Is called inside of a lambda
currentOffsetForPage = { pagerState.calculateCurrentOffsetForPage(it) },
So, it should only recompose where the lambda is invoked. Try putting something that recomposes directly in the factory = {} lambda instead of an inner one, e.g. right after the println, see how it behaves.
d
something like this:
Copy code
factory = {
            val test = pagerState.currentPageOffsetFraction
            println("rebuilt")
            factory.createAppTopBarView(
                currentOffsetForPage = { test },
                onClick = { coroutineScope.launch { pagerState.animateScrollToPage(it) } },
                appTabs = appTabs
            )
        }
it still doesnt recompose...
I also tried something like this, to make the whole thing recompose ("recomp" gets printed but "rebuilt" doesn't):
Copy code
@Composable
actual fun NativeAppTopBar(
    modifier: Modifier,
    pagerState: PagerState
) {
    NativeAppTopBar(
        modifier = modifier,
        pagerState = pagerState,
        pagerStateCurrentPageOffsetFraction = pagerState.currentPageOffsetFraction
    )
}

@Composable
fun NativeAppTopBar(
    modifier: Modifier,
    pagerState: PagerState,
    pagerStateCurrentPageOffsetFraction: Float
) {
    println("recomp")

    val coroutineScope = rememberCoroutineScope()
    val factory = LocalNativeViewFactory.current
    val appTabs = AppTabs.entries.map { it.label.asString() }

    UIKitViewController(
        modifier = modifier.statusBarsPadding(),
        factory = {
            pagerStateCurrentPageOffsetFraction
            val test = pagerState.currentPageOffsetFraction
            println("rebuilt")
            factory.createAppTopBarView(
                currentOffsetForPage = { test },
                onClick = { coroutineScope.launch { pagerState.animateScrollToPage(it) } },
                appTabs = appTabs
            )
        },
        update = { viewController ->
            println("updated")
            viewController.view.setNeedsDisplay()
        }
    )
}
v
Ohh right, because it's the same as the
AndroidView
, all of the recompositions happen only in the
update
block. So, you have to restructure your code in a way that you would be able to update the AppTopBarView using the
viewController
from the
update
block
d
But when does the
update
block update? Because in my above example,
NativeAppTopBar
gets recomposes ("recomp" gets printed), but "updated" doesn't print.