Skip to content

Instantly share code, notes, and snippets.

@davidbilik
Created June 28, 2018 14:04
Show Gist options
  • Save davidbilik/e6acde75483415740c0031eefb8844bc to your computer and use it in GitHub Desktop.
Save davidbilik/e6acde75483415740c0031eefb8844bc to your computer and use it in GitHub Desktop.
fun Fragment.handleErrors(error:Throwable) {
when (error) {
is NoInternetConnectionException -> snackbar(view, R.string.general_no_connection_error)
is GeneralServerException -> snackbar(view, R.string.general_server_error)
else -> throw error
}
}
class LoginFragment : Fragment {
val viewModel: LoginViewModel
var disposables: CompositeDisposable
fun onViewCreated() {
disposables += viewModel.observeLoginResult()
.observeOnMainThread()
.subscribe {
when (it) {
is LoginResult.LoginOk -> finishLogin()
is LoginResult.LoginError -> {
when (it.error) {
is InvalidCredentialsException -> showInvalidCredentialsError()
is ValidationException -> showValidationErrors(it.error.errors)
else -> handleErrors(it.error)
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment