-
-
Save Leviaran/33bbcaa65772faa965e53d0c62181847 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
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