Skip to content

Commit

Permalink
Dispose old compositions when hosting within Compose UI (#1626)
Browse files Browse the repository at this point in the history
Also other remember correctness changes.
  • Loading branch information
JakeWharton authored Oct 24, 2023
1 parent 955ad61 commit f2e090a
Showing 1 changed file with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package app.cash.redwood.composeui
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -44,7 +45,9 @@ public fun RedwoodContent(
provider: Widget.Provider<@Composable () -> Unit>,
content: @Composable () -> Unit,
) {
var viewportSize by remember { mutableStateOf(Size.Zero) }
// If the provider or content change, reset any assumption about the rendered size.
var viewportSize by remember(provider, content) { mutableStateOf(Size.Zero) }

val density = LocalDensity.current
val uiConfiguration = UiConfiguration(
darkMode = isSystemInDarkTheme(),
Expand All @@ -60,8 +63,10 @@ public fun RedwoodContent(
val scope = rememberCoroutineScope()
val onBackPressedDispatcher = platformOnBackPressedDispatcher()
val saveableStateRegistry = LocalSaveableStateRegistry.current
val children = remember(content) { ComposeWidgetChildren() }
LaunchedEffect(provider, content) {

// For simplicity, a new provider or content lambda gets an entirely new composition and children.
val children = remember(provider, content) { ComposeWidgetChildren() }
DisposableEffect(provider, content) {
val composition = RedwoodComposition(
scope = scope,
provider = provider,
Expand All @@ -71,6 +76,10 @@ public fun RedwoodContent(
uiConfigurations = uiConfigurations,
)
composition.setContent(content)

onDispose {
composition.cancel()
}
}

Box(
Expand Down

0 comments on commit f2e090a

Please sign in to comment.