Commit ad925ad9 authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

resolve localized exception message as Either

parent 8617efc6
...@@ -125,14 +125,12 @@ sealed class CustomApiException(val code:Int,@StringRes val messageStringId: In ...@@ -125,14 +125,12 @@ sealed class CustomApiException(val code:Int,@StringRes val messageStringId: In
,messageStringId= CUSTOM_API_ERROR_RESPONSE_CODE_231_MESSAGE_ID) ,messageStringId= CUSTOM_API_ERROR_RESPONSE_CODE_231_MESSAGE_ID)
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?, apiMessage:String?) class UnknownCustomApiException(code: Int, @StringRes messageStringId:Int?, apiMessage:String?)
: CustomApiException(code,messageStringId,apiMessage) : CustomApiException(code,messageStringId,apiMessage)
} }
//as an agreement error message should be correct for user (and localized, if needed) on server-side //as an agreement error message should be correct for user (and localized, if needed) on server-side
......
...@@ -69,12 +69,13 @@ class AuthScreenController : ...@@ -69,12 +69,13 @@ class AuthScreenController :
override fun render(viewState: AuthScreenViewState) { override fun render(viewState: AuthScreenViewState) {
super.render(viewState) super.render(viewState)
Timber.d("Render state $viewState") Timber.d("Render state $viewState")
when(viewState){ when(viewState as BaseState){
is AuthScreenViewState.Idle -> render(viewState) is AuthScreenViewState.Idle -> render(viewState)
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) is AuthScreenViewState.SomeError -> render(viewState)
is BaseState.HandleError -> render(viewState)
} }
} }
...@@ -95,14 +96,12 @@ class AuthScreenController : ...@@ -95,14 +96,12 @@ class AuthScreenController :
} }
private fun render(viewState: AuthScreenViewState.SignInError){ private fun render(viewState: AuthScreenViewState.SignInError){
viewState.exception.stringId?.let { snackbar.showSnackBar(it) } viewState.exception.selectHandler(snackbar::showSnackBar,snackbar::showSnackBar)
viewState.exception.message?.let { snackbar.showSnackBar(it, Snackbar.LENGTH_LONG) }
signInButton.isEnabled=true signInButton.isEnabled=true
} }
private fun render(viewState: AuthScreenViewState.SomeError){ private fun render(viewState: AuthScreenViewState.SomeError){
viewState.exception.stringId?.let { snackbar.showSnackBar(it) } viewState.exception.selectHandler(snackbar::showSnackBar,snackbar::showSnackBar)
viewState.exception.message?.let { snackbar.showSnackBar(it, Snackbar.LENGTH_LONG) }
} }
......
...@@ -7,7 +7,6 @@ import com.biganto.visual.roompark.util.monades.ExceptionString ...@@ -7,7 +7,6 @@ import com.biganto.visual.roompark.util.monades.ExceptionString
* Created by Vladislav Bogdashkin on 30.09.2019. * Created by Vladislav Bogdashkin on 30.09.2019.
*/ */
sealed class AuthScreenViewState : BigantoBaseViewState() { sealed class AuthScreenViewState : BigantoBaseViewState() {
class Idle : AuthScreenViewState() class Idle : AuthScreenViewState()
class Authorization : AuthScreenViewState() class Authorization : AuthScreenViewState()
......
...@@ -10,10 +10,21 @@ import com.biganto.visual.roompark.domain.custom_exception.CustomApiException ...@@ -10,10 +10,21 @@ import com.biganto.visual.roompark.domain.custom_exception.CustomApiException
sealed class Either<out E, out V> { sealed class Either<out E, out V> {
data class Left<out E>(val left: E) : Either<E, Nothing>() data class Left<out E>(val left: E) : Either<E, Nothing>()
data class Right<out V>(val right: V) : Either<Nothing, V>() data class Right<out V>(val right: V) : Either<Nothing, V>()
} }
data class ExceptionString(@StringRes var stringId: Int?, var message: String?) { data class ExceptionString(@StringRes private val stringId: Int?, private val message: String?) {
val errorMessage : Either<Int,String>
fun selectHandler(a1:(Int)->Unit,a2:(String)->Unit) =
when(errorMessage){
is Either.Left -> a1(errorMessage.left)
is Either.Right -> a2(errorMessage.right)
}
constructor(e:CustomApiException) : this( constructor(e:CustomApiException) : this(
if (e.message!=null) null else e.messageStringId, if (e.message!=null) null else e.messageStringId,
...@@ -21,8 +32,8 @@ data class ExceptionString(@StringRes var stringId: Int?, var message: String?) ...@@ -21,8 +32,8 @@ data class ExceptionString(@StringRes var stringId: Int?, var message: String?)
) )
init { init {
assert(stringId!=null && message!=null) { "both values cannot be non-null" } assert(stringId==null && message==null) { "both values cannot be null!" }
assert(stringId==null && message==null) { "both values cannot be null" } errorMessage = if (stringId!=null) Either.Left(stringId) else Either.Right(message!!)
} }
} }
\ No newline at end of file
...@@ -80,10 +80,11 @@ interface ISnackBarProvider{ ...@@ -80,10 +80,11 @@ interface ISnackBarProvider{
fun bindRootView(rootView : View?) fun bindRootView(rootView : View?)
val isRootBounded : Boolean val isRootBounded : Boolean
val snackBar : Snackbar? val snackBar : Snackbar?
fun showSnackBar(@StringRes stringId: Int)
fun showSnackBar(erorrMessage: Any?)
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