Last active
June 18, 2018 09:33
-
-
Save sys1yagi/4d2e21217df8e31f2a3870ea9e186cae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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