From bb51450c46613dee5f097bb6ef4afddf269e6c3d Mon Sep 17 00:00:00 2001 From: Elijah Semyonov Date: Fri, 1 Mar 2024 12:32:48 +0100 Subject: [PATCH] Call setContent once per mediator creation on iOS (#1152) ## Proposed Changes Perform `setContent` only once per ComposeContainer mediator lifetime. ## Testing Test: scenario described in [the comment](https://github.com/JetBrains/compose-multiplatform/issues/3698#issuecomment-1969572614) doesn't cause whole app recomposition with remembered state recreation ## Issues Fixed Fixes: https://github.com/JetBrains/compose-multiplatform/issues/3698#issuecomment-1969572614 --- .../ui/window/ComposeContainer.uikit.kt | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/compose/ui/ui/src/uikitMain/kotlin/androidx/compose/ui/window/ComposeContainer.uikit.kt b/compose/ui/ui/src/uikitMain/kotlin/androidx/compose/ui/window/ComposeContainer.uikit.kt index 3654901677a95..c1b8d30671cdf 100644 --- a/compose/ui/ui/src/uikitMain/kotlin/androidx/compose/ui/window/ComposeContainer.uikit.kt +++ b/compose/ui/ui/src/uikitMain/kotlin/androidx/compose/ui/window/ComposeContainer.uikit.kt @@ -234,7 +234,7 @@ internal class ComposeContainer( super.viewWillAppear(animated) isInsideSwiftUI = checkIfInsideSwiftUI() - setContent(content) + createMediatorIfNeeded() configuration.delegate.viewWillAppear(animated) } @@ -316,8 +316,14 @@ internal class ComposeContainer( ) } - private fun setContent(content: @Composable () -> Unit) { - val mediator = mediator ?: ComposeSceneMediator( + private fun createMediatorIfNeeded() { + if (mediator == null) { + mediator = createMediator() + } + } + + private fun createMediator(): ComposeSceneMediator { + val mediator = ComposeSceneMediator( container = view, configuration = configuration, focusStack = focusStack, @@ -325,15 +331,12 @@ internal class ComposeContainer( coroutineContext = coroutineDispatcher, renderingUIViewFactory = ::createSkikoUIView, composeSceneFactory = ::createComposeScene, - ).also { - this.mediator = it - } + ) mediator.setContent { - ProvideContainerCompositionLocals(this) { - content() - } + ProvideContainerCompositionLocals(this, content) } mediator.setLayout(SceneLayout.UseConstraintsToFillContainer) + return mediator } private fun dispose() {