Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.experimental.channels.ReceiveChannel
import kotlinx.coroutines.experimental.channels.sendBlocking
import kotlinx.coroutines.experimental.launch
object EditTextChannel {
fun empty(editText: EditText): ReceiveChannel<Boolean> = ConflatedChannel<Boolean>().apply {
editText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
// no op
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
// no op
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
offer(s.isNullOrEmpty())
}
})
// initial value
offer(editText.text.isEmpty())
}
}
launch {
EditTextChannel.empty(editForm).consumeEach {
submitButton.isEnabled = !it
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment