adding extension for acting on textinputlayout text changes

This commit is contained in:
Adam Brown 2022-07-13 13:58:39 +01:00
parent a02141c7c2
commit ffb8b10f66

View file

@ -44,11 +44,18 @@ fun TextInputLayout.content() = editText().text.toString()
fun TextInputLayout.hasContent() = !editText().text.isNullOrEmpty()
fun TextInputLayout.clearErrorOnChange(lifecycleOwner: LifecycleOwner) {
editText().textChanges()
editText()
.textChanges()
.onEach { error = null }
.launchIn(lifecycleOwner.lifecycleScope)
}
fun TextInputLayout.onTextChange(lifecycleOwner: LifecycleOwner, action: (CharSequence) -> Unit) {
editText().textChanges()
.onEach(action)
.launchIn(lifecycleOwner.lifecycleScope)
}
fun TextInputLayout.associateContentStateWith(button: View, enabledPredicate: (String) -> Boolean = { it.isNotEmpty() }) {
button.isEnabled = enabledPredicate(content())
editText().addTextChangedListener(object : SimpleTextWatcher() {