Skip to content

Commit

Permalink
Call setContent once per mediator creation on iOS (#1152)
Browse files Browse the repository at this point in the history
## Proposed Changes

Perform `setContent` only once per ComposeContainer mediator lifetime.

## Testing

Test: scenario described in [the
comment](JetBrains/compose-multiplatform#3698 (comment))
doesn't cause whole app recomposition with remembered state recreation

## Issues Fixed

Fixes:
JetBrains/compose-multiplatform#3698 (comment)
  • Loading branch information
elijah-semyonov authored and igordmn committed Mar 4, 2024
1 parent 9dbdb1e commit bb51450
Showing 1 changed file with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ internal class ComposeContainer(
super.viewWillAppear(animated)

isInsideSwiftUI = checkIfInsideSwiftUI()
setContent(content)
createMediatorIfNeeded()
configuration.delegate.viewWillAppear(animated)
}

Expand Down Expand Up @@ -316,24 +316,27 @@ 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,
windowContext = windowContext,
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() {
Expand Down

0 comments on commit bb51450

Please sign in to comment.