Skip to content

Instantly share code, notes, and snippets.

@Leviaran
Created February 3, 2021 06:49
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 Leviaran/33bbcaa65772faa965e53d0c62181847 to your computer and use it in GitHub Desktop.
Save Leviaran/33bbcaa65772faa965e53d0c62181847 to your computer and use it in GitHub Desktop.
fun LoginFragment.view(intents: Channel<Intents>): Pair<Channel<Intents>, SendChannel<UiModel>> {
return intents to lifecycleScope.actor {
for(model in channel) {
updateProgress(model)
updateLoginButton(model, intents)
updateRegisterButton(model, intents)
updateErrorTextView(model)
updateNavigation(model)
}
}
}
private fun LoginFragment.updateNavigation(uiModel: UiModel) {
uiModel.authenticationResponse
?.takeIf { it.success }
?.also {
activity?.finish()
}
}
private fun LoginFragment.updateErrorTextView(uiModel: UiModel) {
errorTextView.text = uiModel.error ?: ""
}
private fun LoginFragment.updateProgress(uiModel: UiModel) {
progressBar.visibility = if(uiModel.progress) View.VISIBLE else View.GONE
}
private fun LoginFragment.updateLoginButton(uiModel: UiModel, intents: SendChannel<Intents>) {
if(uiModel.progress) loginButton.setOnClickListener(null)
else loginButton.setOnClickListener {
lifecycleScope.launch(SupervisorJob() + Dispatchers.IO) {
intents.send(
RequestLogin(
userNameEditText.text.toString(),
passwordEditText.text.toString()
)
)
}
}
}
private fun LoginFragment.updateRegisterButton(uiModel: UiModel, intents: SendChannel<Intents>) {
if (uiModel.progress) registerButton.setOnClickListener(null) else {
lifecycleScope.launch(SupervisorJob() + Dispatchers.IO) {
intents.send(
RequestRegister(
userNameEditText.text.toString(),
passwordEditText.text.toString()
)
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment