Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS. ComposeUIViewController. Dispose composition on viewDidDisappear #747

Merged
merged 6 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 8 additions & 12 deletions compose/mpp/demo/src/uikitMain/kotlin/NativePopupExample.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,11 @@ private fun NativeModalWithNavigation() {
Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
val viewController = LocalUIViewController.current
Button(onClick = {
viewController.get()?.presentViewController(
UINavigationController(
rootViewController = ComposeUIViewController {
NativeNavigationPage()
}
),
animated = true,
completion = null
)
val navigationController = UINavigationController(rootViewController = ComposeUIViewController {
NativeNavigationPage()
})

viewController.presentViewController(navigationController, true, null)
}) {
Text("Present popup")
}
Expand All @@ -54,11 +50,11 @@ private fun NativeModalWithNavigation() {

@Composable
private fun NativeNavigationPage() {
val viewController = LocalUIViewController.current

Column(Modifier.fillMaxSize(), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally) {
val navigationController = LocalUIViewController.current.navigationController

Button(onClick = {
viewController.get()?.navigationController?.pushViewController(
navigationController?.pushViewController(
ComposeUIViewController {
NativeNavigationPage()
}, true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,10 @@ internal class ComposeLayer(

fun dispose() {
check(!isDisposed)

isDisposed = true

scene.close()
layer.detach()
scene.close()
_initContent = null
isDisposed = true
}

fun setSize(width: Int, height: Int) {
Expand Down Expand Up @@ -166,6 +164,7 @@ internal class ComposeLayer(
content = content
)
}

initContent()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,13 @@
package androidx.compose.ui.interop

import androidx.compose.runtime.staticCompositionLocalOf
import kotlin.native.ref.WeakReference
import platform.UIKit.UIViewController

/**
* public value to get UIViewController of Compose window for library authors.
* Maybe useful for features, like VideoPlayer and Bottom menus.
* Please use it careful and don't remove another views.
*
* _IMPORTANT NOTE_:
*
* Never capture the unwrapped strong reference in any closure inside Composition. Doing so will cause
* a memory leak.
*
* Unwrap it only in the place of usage, for example:
* ```
* @Composable
* private fun Foo() {
* // can't use LocalUIViewController.current inside onClick because onClick is not @Composable
* val viewController = LocalUIViewController.current
*
* Column {
* Button(onClick = {
* viewController.get()?.presentViewController(...)
* }) {
* Text("Push")
* }
* }
* }
* ```
*/
val LocalUIViewController = staticCompositionLocalOf<WeakReference<UIViewController>> {
val LocalUIViewController = staticCompositionLocalOf<UIViewController> {
error("CompositionLocal UIViewController not provided")
}