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

UIKitView. Fix lifetime discrepancy within the composition. #576

Merged
merged 2 commits into from Jun 8, 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
@@ -1,6 +1,8 @@
package androidx.compose.ui.interop

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Modifier
import kotlinx.cinterop.ObjCAction
import platform.CoreGraphics.CGRectMake
Expand All @@ -17,12 +19,14 @@ import platform.UIKit.UITextField
*/
@Composable
fun ComposeUITextField(value: String, onValueChange: (String) -> Unit, modifier: Modifier) {
val latestOnValueChanged by rememberUpdatedState(onValueChange)

UIKitView(
factory = {
val textField = object : UITextField(CGRectMake(0.0, 0.0, 0.0, 0.0)) {
@ObjCAction
fun editingChanged() {
onValueChange(text ?: "")
latestOnValueChanged(text ?: "")
}
}
textField.addTarget(
Expand All @@ -35,13 +39,6 @@ fun ComposeUITextField(value: String, onValueChange: (String) -> Unit, modifier:
modifier = modifier,
update = { textField ->
textField.text = value
},
onRelease = { textField ->
textField.removeTarget(
target = textField,
action = NSSelectorFromString(textField::editingChanged.name),
forControlEvents = UIControlEventEditingChanged
)
},
}
)
}
Expand Up @@ -116,7 +116,7 @@ fun <T : UIView> UIKitView(
}
)

DisposableEffect(factory, onRelease) {
DisposableEffect(Unit) {
componentInfo.component = factory()
componentInfo.container = UIView().apply {
addSubview(componentInfo.component)
Expand All @@ -129,13 +129,15 @@ fun <T : UIView> UIKitView(
onRelease(componentInfo.component)
}
}

LaunchedEffect(background) {
if (background == Color.Unspecified) {
componentInfo.container.backgroundColor = root.backgroundColor
} else {
componentInfo.container.backgroundColor = parseColor(background)
}
}

SideEffect {
componentInfo.updater.update = update
}
Expand Down