Skip to content

Instantly share code, notes, and snippets.

@KonstantinBerkow
Created January 29, 2018 18:56
Show Gist options
  • Save KonstantinBerkow/2d2b607818a3c79e67dda1de65458d11 to your computer and use it in GitHub Desktop.
Save KonstantinBerkow/2d2b607818a3c79e67dda1de65458d11 to your computer and use it in GitHub Desktop.
Login use case
class LoginInteractor(private val api: Api) {
fun login(email: String, password: String): Observable<LoginResult> {
return when {
!EMAIL_PATTERN.matcher(email).matches() -> Observable.just(LoginError(ErrorClass.INVALID_EMAIL))
password.length < 6 -> Observable.just(LoginError(ErrorClass.INVALID_PASSWORD) as LoginResult)
else -> api.login(Credentials(email, password))
.map { response -> LoginSuccess(response.token, response.info) as LoginResult }
.onErrorReturn { throwable ->
when (throwable) {
is HttpException -> convertHttpException(throwable)
is SocketException -> LoginError(ErrorClass.FAILED_TO_CONNECT)
else -> LoginError(ErrorClass.SERVER_NOT_RESPONDING)
}
}
.startWith(PendingLogin)
}
}
// по хорошему неплохо бы словить 400, мол сервер ведь тоже должен валидировать данные,
// но я не придумал как впихнуть это в ошибку, это нужно менять возвращаемый тип от апи
private fun convertHttpException(error: HttpException): LoginResult =
when (error.statusCode) {
HttpURLConnection.HTTP_NOT_FOUND -> LoginError(ErrorClass.USER_NOT_FOUND)
HttpURLConnection.HTTP_UNAUTHORIZED -> LoginError(ErrorClass.WRONG_CREDENTIALS)
else -> LoginError(ErrorClass.SERVER_INTERNAL_ERROR)
}
companion object {
@JvmField
val EMAIL_PATTERN: Pattern = Pattern.compile(
"[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
"\\@" +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
"(" +
"\\." +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
")+"
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment