Commit 8617efc6 authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

added custom error model

parent 19977c81
...@@ -82,7 +82,7 @@ const val CUSTOM_API_ERROR_RESPONSE_CODE_TOUR_NOT_FOUND_MESSAGE_ID=R.string.api_ ...@@ -82,7 +82,7 @@ const val CUSTOM_API_ERROR_RESPONSE_CODE_TOUR_NOT_FOUND_MESSAGE_ID=R.string.api_
@StringRes @StringRes
const val CUSTOM_API_ERROR_DEFAULT_MESSAGE_ID=R.string.api_error_default const val CUSTOM_API_ERROR_DEFAULT_MESSAGE_ID=R.string.api_error_default
sealed class CustomApiException(val code:Int,@StringRes val messageStringId: Int):Exception() { sealed class CustomApiException(val code:Int,@StringRes val messageStringId: Int?, val customMessage:String? = null):Exception() {
class NotAuthorizedException() : CustomApiException(CUSTOM_API_ERROR_RESPONSE_CODE_100 class NotAuthorizedException() : CustomApiException(CUSTOM_API_ERROR_RESPONSE_CODE_100
,messageStringId= CUSTOM_API_ERROR_RESPONSE_CODE_100_MESSAGE_ID) ,messageStringId= CUSTOM_API_ERROR_RESPONSE_CODE_100_MESSAGE_ID)
class TokenSyntaxException() : CustomApiException(CUSTOM_API_ERROR_RESPONSE_CODE_101 class TokenSyntaxException() : CustomApiException(CUSTOM_API_ERROR_RESPONSE_CODE_101
...@@ -129,24 +129,15 @@ sealed class CustomApiException(val code:Int,@StringRes val messageStringId: In ...@@ -129,24 +129,15 @@ sealed class CustomApiException(val code:Int,@StringRes val messageStringId: In
class TourByIdNotFoundException() : CustomApiException( class TourByIdNotFoundException() : CustomApiException(
CUSTOM_API_ERROR_RESPONSE_CODE_TOUR_NOT_FOUND CUSTOM_API_ERROR_RESPONSE_CODE_TOUR_NOT_FOUND
,messageStringId= CUSTOM_API_ERROR_RESPONSE_CODE_TOUR_NOT_FOUND_MESSAGE_ID) ,messageStringId= CUSTOM_API_ERROR_RESPONSE_CODE_TOUR_NOT_FOUND_MESSAGE_ID)
class UnknownCustomApiException(code: Int, @StringRes messageStringId:Int) class UnknownCustomApiException(code: Int, @StringRes messageStringId:Int?, apiMessage:String?)
: CustomApiException(code,messageStringId) : CustomApiException(code,messageStringId,apiMessage)
} }
fun parseException(err: ErrorRaw):CustomApiException{ //as an agreement error message should be correct for user (and localized, if needed) on server-side
return when(err.code){ fun parseException(err: ErrorRaw) =
CUSTOM_API_ERROR_RESPONSE_CODE_100 -> CustomApiException.NotAuthorizedException() CustomApiException.UnknownCustomApiException(err.code, null, err.message)
CUSTOM_API_ERROR_RESPONSE_CODE_101 -> CustomApiException.TokenSyntaxException()
CUSTOM_API_ERROR_RESPONSE_CODE_102 -> CustomApiException.UserWithTokenNotFoundException()
CUSTOM_API_ERROR_RESPONSE_CODE_103 -> CustomApiException.TokenEstimatedException()
CUSTOM_API_ERROR_RESPONSE_CODE_104 -> CustomApiException.UserBannedException()
CUSTOM_API_ERROR_RESPONSE_CODE_111 -> CustomApiException.WrongLoginException()
CUSTOM_API_ERROR_RESPONSE_CODE_112 -> CustomApiException.WrongPasswordException()
else -> CustomApiException.UnknownCustomApiException(err.code, CUSTOM_API_ERROR_DEFAULT_MESSAGE_ID)
}
}
...@@ -74,6 +74,7 @@ class AuthScreenController : ...@@ -74,6 +74,7 @@ class AuthScreenController :
is AuthScreenViewState.Authorization -> render(viewState) is AuthScreenViewState.Authorization -> render(viewState)
is AuthScreenViewState.SignedIn -> render(viewState) is AuthScreenViewState.SignedIn -> render(viewState)
is AuthScreenViewState.SignInError -> render(viewState) is AuthScreenViewState.SignInError -> render(viewState)
is AuthScreenViewState.SomeError -> render(viewState)
} }
} }
...@@ -94,10 +95,14 @@ class AuthScreenController : ...@@ -94,10 +95,14 @@ class AuthScreenController :
} }
private fun render(viewState: AuthScreenViewState.SignInError){ private fun render(viewState: AuthScreenViewState.SignInError){
snackbar.showSnackBar(viewState.message, Snackbar.LENGTH_LONG) viewState.exception.stringId?.let { snackbar.showSnackBar(it) }
// toolBar.hideAll() viewState.exception.message?.let { snackbar.showSnackBar(it, Snackbar.LENGTH_LONG) }
signInButton.isEnabled=true signInButton.isEnabled=true
// snacky.showSnackBar("lul") }
private fun render(viewState: AuthScreenViewState.SomeError){
viewState.exception.stringId?.let { snackbar.showSnackBar(it) }
viewState.exception.message?.let { snackbar.showSnackBar(it, Snackbar.LENGTH_LONG) }
} }
......
package com.biganto.visual.roompark.presentation.screen.auth package com.biganto.visual.roompark.presentation.screen.auth
import com.biganto.visual.roompark.R
import com.biganto.visual.roompark.conductor.BigantoBasePresenter import com.biganto.visual.roompark.conductor.BigantoBasePresenter
import com.biganto.visual.roompark.data.service.network.NoNetworkException
import com.biganto.visual.roompark.domain.custom_exception.CustomApiException
import com.biganto.visual.roompark.domain.interactor.AuthInteractor import com.biganto.visual.roompark.domain.interactor.AuthInteractor
import com.biganto.visual.roompark.util.monades.ExceptionString
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
import timber.log.Timber import timber.log.Timber
...@@ -17,23 +21,47 @@ class AuthScreenPresenter @Inject constructor( ...@@ -17,23 +21,47 @@ class AuthScreenPresenter @Inject constructor(
) )
: BigantoBasePresenter<AuthScreen, AuthScreenViewState>() { : BigantoBasePresenter<AuthScreen, AuthScreenViewState>() {
private fun parseError(t: Throwable): AuthScreenViewState =
when (t) {
is CustomApiException -> onCodeReturn(e = t)
is NoNetworkException -> onNoNetwork(t)
else -> onRandomError(t)
}
private fun onRandomError(t: Throwable): AuthScreenViewState =
AuthScreenViewState.SomeError(
ExceptionString(R.string.unknown_error, null)
)
private fun onNoNetwork(e: NoNetworkException): AuthScreenViewState =
AuthScreenViewState.SomeError(
ExceptionString(R.string.no_network_error, null)
)
private fun onCodeReturn(e: CustomApiException): AuthScreenViewState =
when (e.code) {
else -> AuthScreenViewState.SignInError(ExceptionString(e))
}
override fun bindIntents() { override fun bindIntents() {
val onAuth = intent(AuthScreen::tryAuth) val onAuth = intent(AuthScreen::tryAuth)
.flatMap<AuthScreenViewState>{ model -> .flatMap<AuthScreenViewState> { model ->
interactor.signIn(model.login, model.pwd) interactor.signIn(model.login, model.pwd)
.doOnNext{Timber.d("auth returned $it")} .doOnNext { Timber.d("auth returned $it") }
.map { it } .map { it }
.map<AuthScreenViewState> { AuthScreenViewState.SignInError("") }
.map { AuthScreenViewState.SignedIn() } .map { AuthScreenViewState.SignedIn() }
} }
// .startWith(Observable.just(AuthScreenViewState.Authorization())) // .startWith(Observable.just(AuthScreenViewState.Authorization()))
val state = restoreStateObservable val state = restoreStateObservable
.mergeWith(onAuth) .mergeWith(onAuth)
.doOnError{ Timber.e(it)} .doOnError { Timber.e(it) }
.onErrorReturn(::parseError)
.subscribeOn(Schedulers.single()) .subscribeOn(Schedulers.single())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
......
package com.biganto.visual.roompark.presentation.screen.auth package com.biganto.visual.roompark.presentation.screen.auth
import com.biganto.visual.roompark.conductor.BigantoBaseViewState import com.biganto.visual.roompark.conductor.BigantoBaseViewState
import com.biganto.visual.roompark.util.monades.ExceptionString
/** /**
* Created by Vladislav Bogdashkin on 30.09.2019. * Created by Vladislav Bogdashkin on 30.09.2019.
...@@ -11,5 +12,6 @@ sealed class AuthScreenViewState : BigantoBaseViewState() { ...@@ -11,5 +12,6 @@ sealed class AuthScreenViewState : BigantoBaseViewState() {
class Idle : AuthScreenViewState() class Idle : AuthScreenViewState()
class Authorization : AuthScreenViewState() class Authorization : AuthScreenViewState()
class SignedIn : AuthScreenViewState() class SignedIn : AuthScreenViewState()
class SignInError(val message:String) : AuthScreenViewState() class SignInError(val exception: ExceptionString) : AuthScreenViewState()
class SomeError(val exception: ExceptionString) : AuthScreenViewState()
} }
\ No newline at end of file
package com.biganto.visual.roompark.util.monades
import androidx.annotation.StringRes
import com.biganto.visual.roompark.domain.custom_exception.CustomApiException
/**
* Created by Vladislav Bogdashkin on 12.11.2019.
*/
sealed class Either<out E, out V> {
data class Left<out E>(val left: E) : Either<E, Nothing>()
data class Right<out V>(val right: V) : Either<Nothing, V>()
}
data class ExceptionString(@StringRes var stringId: Int?, var message: String?) {
constructor(e:CustomApiException) : this(
if (e.message!=null) null else e.messageStringId,
if (e.messageStringId!=null) null else e.message
)
init {
assert(stringId!=null && message!=null) { "both values cannot be non-null" }
assert(stringId==null && message==null) { "both values cannot be null" }
}
}
\ No newline at end of file
...@@ -2,6 +2,7 @@ package com.biganto.visual.roompark.util.view_utils.snackbar ...@@ -2,6 +2,7 @@ package com.biganto.visual.roompark.util.view_utils.snackbar
import android.app.Activity import android.app.Activity
import android.view.View import android.view.View
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import com.biganto.visual.roompark.R import com.biganto.visual.roompark.R
import com.biganto.visual.roompark.util.view_utils.snackbar.SnackBarMessageType.* import com.biganto.visual.roompark.util.view_utils.snackbar.SnackBarMessageType.*
...@@ -52,6 +53,10 @@ class SnackBarProvider @Inject constructor(val activity: Activity) : ISnackBarPr ...@@ -52,6 +53,10 @@ class SnackBarProvider @Inject constructor(val activity: Activity) : ISnackBarPr
this.rootView = rootView this.rootView = rootView
} }
override fun showSnackBar(@StringRes stringId: Int) {
showSnackBar(activity.resources.getString(stringId), Snackbar.LENGTH_SHORT)
}
override fun showSnackBar(message: String) { override fun showSnackBar(message: String) {
showSnackBar(message, Snackbar.LENGTH_SHORT) showSnackBar(message, Snackbar.LENGTH_SHORT)
} }
...@@ -78,6 +83,7 @@ interface ISnackBarProvider{ ...@@ -78,6 +83,7 @@ interface ISnackBarProvider{
fun showSnackBar(message: String) fun showSnackBar(message: String)
fun showSnackBar(message: String, length: Int) fun showSnackBar(message: String, length: Int)
fun showSnackBar(message: String, type: SnackBarMessageType, length: Int) fun showSnackBar(message: String, type: SnackBarMessageType, length: Int)
fun showSnackBar(@StringRes stringId: Int)
} }
enum class SnackBarMessageType{ enum class SnackBarMessageType{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment