Skip to content

Instantly share code, notes, and snippets.

@sys1yagi
Last active June 18, 2018 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sys1yagi/4d2e21217df8e31f2a3870ea9e186cae to your computer and use it in GitHub Desktop.
Save sys1yagi/4d2e21217df8e31f2a3870ea9e186cae to your computer and use it in GitHub Desktop.
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