Browse Source

Simplify UIKitView example (#3618)

pull/3624/head
dima.avdeev 1 year ago committed by GitHub
parent
commit
499b62830b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      examples/interop/ios-uikit-in-compose/shared/src/iosMain/kotlin/UseUITextField.kt
  2. 11
      examples/interop/ios-uikit-in-compose/shared/src/iosMain/kotlin/main.ios.kt

28
examples/interop/ios-uikit-in-compose/shared/src/iosMain/kotlin/ComposeUITextField.uikit.kt → examples/interop/ios-uikit-in-compose/shared/src/iosMain/kotlin/UseUITextField.kt

@ -1,8 +1,16 @@
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberUpdatedState import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.interop.UIKitView import androidx.compose.ui.interop.UIKitView
import androidx.compose.ui.unit.dp
import kotlinx.cinterop.ExperimentalForeignApi import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.ObjCAction import kotlinx.cinterop.ObjCAction
import platform.CoreGraphics.CGRectMake import platform.CoreGraphics.CGRectMake
@ -10,24 +18,16 @@ import platform.Foundation.NSSelectorFromString
import platform.UIKit.UIControlEventEditingChanged import platform.UIKit.UIControlEventEditingChanged
import platform.UIKit.UITextField import platform.UIKit.UITextField
/**
* Compose wrapper for native UITextField.
* @param value the input text to be shown in the text field.
* @param onValueChange the callback that is triggered when the input service updates the text. An
* updated text comes as a parameter of the callback
* @param modifier a [Modifier] for this text field. Size should be specified in modifier.
*/
@OptIn(ExperimentalForeignApi::class) @OptIn(ExperimentalForeignApi::class)
@Composable @Composable
fun ComposeUITextField(value: String, onValueChange: (String) -> Unit, modifier: Modifier) { fun UseUITextField() {
val latestOnValueChanged by rememberUpdatedState(onValueChange) var message by remember { mutableStateOf("Hello, World!") }
UIKitView( UIKitView(
factory = { factory = {
val textField = object : UITextField(CGRectMake(0.0, 0.0, 0.0, 0.0)) { val textField = object : UITextField(CGRectMake(0.0, 0.0, 0.0, 0.0)) {
@ObjCAction @ObjCAction
fun editingChanged() { fun editingChanged() {
latestOnValueChanged(text ?: "") message = text ?: ""
} }
} }
textField.addTarget( textField.addTarget(
@ -37,9 +37,9 @@ fun ComposeUITextField(value: String, onValueChange: (String) -> Unit, modifier:
) )
textField textField
}, },
modifier = modifier, modifier = Modifier.padding(4.dp).fillMaxWidth().height(30.dp).border(2.dp, Color.Blue),
update = { textField -> update = { textField ->
textField.text = value textField.text = message
} }
) )
} }

11
examples/interop/ios-uikit-in-compose/shared/src/iosMain/kotlin/main.ios.kt

@ -1,10 +1,6 @@
import androidx.compose.foundation.border import androidx.compose.foundation.border
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
@ -33,11 +29,6 @@ fun ComposeEntryPoint(): UIViewController =
Spacer(Modifier.size(20.dp)) Spacer(Modifier.size(20.dp))
var text: String by remember { mutableStateOf("This is iOS UITextField inside Compose") } UseUITextField()
ComposeUITextField(
text,
{ text = it },
Modifier.padding(4.dp).fillMaxWidth().height(30.dp).border(2.dp, Color.Blue)
)
} }
} }

Loading…
Cancel
Save