I was using LocalTextInputService
and TextInputService
to show my own custom input TextField
in app. But they are deprecated in compose 1.7.0 and as result CoreTextField
is stopped receiving LocalTextInputService.current
Before 1.7.0 it was:
val textInputService = LocalTextInputService.current
And after 1.7.0 became:
val legacyTextInputServiceAdapter = remember { createLegacyPlatformTextInputServiceAdapter() }
val textInputService: TextInputService = remember {
TextInputService(legacyTextInputServiceAdapter)
}
Deprecated annotation message mentation that PlatformTextInputModifierNode
should be used instead, but I didn’t find any way how to do it.
Here is some more details of my Previous realisation:
- Container above all app composable:
@Composable
fun MyKeyboardContainer(
modifier: Modifier = Modifier,
closeKeyboardClickOutside: Boolean = true,
content: @Composable ColumnScope.() -> Unit,
) = Column(modifier = modifier) {
val focusManager = LocalFocusManager.current
val clickableModifier = if (closeKeyboardClickOutside) {
Modifier.clickable(
interactionSource = remember { MutableInteractionSource() },
indication = null
) { focusManager.clearFocus(force = true) }
} else {
Modifier
}
val textInputService: TextInputService = MyLocalTextInputService.current
CompositionLocalProvider(LocalTextInputService provides textInputService) {
Column(
modifier = Modifier
.weight(1f)
.fillMaxWidth()
.then(clickableModifier),
content = content,
)
MyKeyboard(windowSize)
}
}
- TextInputService:
/**
* Static composition local variable for holding application built-in [TextInputService]
*/
internal val MyLocalTextInputService =
staticCompositionLocalOf { MyTextInputService(MyPlatformTextInputService()) }
/**
* Extends [TextInputService]. Replaces [PlatformTextInputService] with
* [MyPlatformTextInputService] to show application built-in keyboard instead of
* android system keyboard.
*/
internal class MyTextInputService(
val mPlatformTextInputService: MyPlatformTextInputService
) : TextInputService(mPlatformTextInputService)
/**
* Application text input service. This class is responsible for holding [InputState] and
* [KeyboardState]
*/
internal class MyPlatformTextInputService : PlatformTextInputService {
val softKeyboardState = MutableStateFlow(KeyboardState.HIDDEN)
val inputState = MutableStateFlow<InputState>(InputState.Idle)
override fun hideSoftwareKeyboard() {
softKeyboardState.value = KeyboardState.HIDDEN
}
override fun showSoftwareKeyboard() {
softKeyboardState.value = KeyboardState.SHOWN
}
override fun startInput(
value: TextFieldValue,
imeOptions: ImeOptions,
onEditCommand: (List<EditCommand>) -> Unit,
onImeActionPerformed: (ImeAction) -> Unit,
) {
inputState.value = InputState.Active(
textFieldValue = value,
onEditCommand = onEditCommand,
onImeActionPerformed = onImeActionPerformed,
imeOptions = imeOptions,
)
softKeyboardState.value = KeyboardState.SHOWN
}
override fun stopInput() {
inputState.value = InputState.Idle
}
override fun updateState(oldValue: TextFieldValue?, newValue: TextFieldValue) {
(inputState.value as? InputState.Active)?.let { activeInputState ->
inputState.value = activeInputState.copy(textFieldValue = newValue)
}
}
enum class KeyboardState {
HIDDEN, SHOWN,
}
sealed interface InputState {
object Idle : InputState
data class Active(
val textFieldValue: TextFieldValue,
val onEditCommand: (List<EditCommand>) -> Unit,
val onImeActionPerformed: (ImeAction) -> Unit,
val imeOptions: ImeOptions,
) : InputState
}
}
I want to find how to use my old implementation or a way how I can use PlatformTextInputModifierNode and get similar result
MisterFlyBear is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Based on deprecation and the message you are seeing, you could wrap your TextField with InterceptPlatformTextInput:
InterceptPlatformTextInput(
interceptor = { request, nextHandler ->
// Create a new request to wrap the incoming one with some custom logic.
val modifiedRequest =
object : PlatformTextInputMethodRequest {
override fun createInputConnection(outAttributes: EditorInfo): InputConnection {
val inputConnection = request.createInputConnection(outAttributes)
// After the original request finishes initializing the EditorInfo we can
// customize it. If we needed to we could also wrap the InputConnection
// before
// returning it.
updateEditorInfo(outAttributes)
return inputConnection
}
fun updateEditorInfo(outAttributes: EditorInfo) {
// Your code here, e.g. set some custom properties.
}
}
// Send our wrapping request to the next handler, which could be the system or another
// interceptor up the tree.
nextHandler.startInputMethod(modifiedRequest)
}
) {
BasicTextField(value = text, onValueChange = { text = it })
}
Where you can replace the BasicTextField with your UI.
Example taken from documentation reference
1