Commit 3dfee915 authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

fix custom exception flow (messagename) and auth method with proper configuration

parent 9678c356
...@@ -8,6 +8,7 @@ import com.hannesdorfmann.mosby3.mvi.MviBasePresenter ...@@ -8,6 +8,7 @@ import com.hannesdorfmann.mosby3.mvi.MviBasePresenter
import com.hannesdorfmann.mosby3.mvp.MvpView import com.hannesdorfmann.mosby3.mvp.MvpView
import com.jakewharton.rxrelay2.PublishRelay import com.jakewharton.rxrelay2.PublishRelay
import io.reactivex.Observable import io.reactivex.Observable
import timber.log.Timber
/** /**
* Created by Vladislav Bogdashkin on 30.09.2019. * Created by Vladislav Bogdashkin on 30.09.2019.
...@@ -27,16 +28,20 @@ abstract class BigantoBasePresenter<V : MvpView, VS> ...@@ -27,16 +28,20 @@ abstract class BigantoBasePresenter<V : MvpView, VS>
open fun vsByCode(code: Int) = defaultErrorViewStateHandler() open fun vsByCode(code: Int) = defaultErrorViewStateHandler()
open fun vsByThrowable(t: Throwable) = defaultErrorViewStateHandler() open fun vsByThrowable(t: Throwable) = defaultErrorViewStateHandler()
open fun parseError(t: Throwable): VS = open fun parseError(t: Throwable) : VS =
when (t) { when (t) {
is CustomApiException -> parseError(t) is CustomApiException ->{
is NoNetworkException -> parseError(t) Timber.d("CustomApiException ${t.messageStringId} / ${t.customMessage}")
else -> parseError(t) parse(t)
}
is NoNetworkException -> parse(t)
else -> parse(t)
} }
private fun parseError(e: CustomApiException) = onCodeReturn(e) private fun parse(e: CustomApiException) = onCodeReturn(e)
private fun parseError(e: NoNetworkException) = onNoNetwork(e) private fun parse(e: NoNetworkException) = onNoNetwork(e)
private fun parseError(e: Exception) = onRandomError(e) private fun parse(e: Throwable) = onRandomError(e)
open fun onRandomError(t: Throwable): VS = open fun onRandomError(t: Throwable): VS =
vsByThrowable(t).invoke( vsByThrowable(t).invoke(
...@@ -48,7 +53,11 @@ abstract class BigantoBasePresenter<V : MvpView, VS> ...@@ -48,7 +53,11 @@ abstract class BigantoBasePresenter<V : MvpView, VS>
ExceptionString(R.string.no_network_error, null) ExceptionString(R.string.no_network_error, null)
) )
private fun onCodeReturn(e: CustomApiException): VS = private fun onCodeReturn(e: CustomApiException): VS {
vsByCode(e.code).invoke(ExceptionString(e)) Timber.d("2 CustomApiException ${e.messageStringId} / ${e.customMessage}")
val errst = ExceptionString(e.messageStringId,e.customMessage)
Timber.d("ExceptionString ${errst} / ${errst}")
return vsByCode(e.code).invoke(errst)
}
} }
\ No newline at end of file
...@@ -3,9 +3,7 @@ package com.biganto.visual.roompark.data.repository.api.retrofit ...@@ -3,9 +3,7 @@ package com.biganto.visual.roompark.data.repository.api.retrofit
import com.biganto.visual.roompark.data.repository.api.retrofit.raw.* import com.biganto.visual.roompark.data.repository.api.retrofit.raw.*
import io.reactivex.Observable import io.reactivex.Observable
import retrofit2.Response import retrofit2.Response
import retrofit2.http.GET import retrofit2.http.*
import retrofit2.http.POST
import retrofit2.http.Query
/** /**
...@@ -133,12 +131,13 @@ interface IRoomParkMobileApi{ ...@@ -133,12 +131,13 @@ interface IRoomParkMobileApi{
@POST("$API_URL$AUTH_METHOD$DELIMITER") @POST("$API_URL$AUTH_METHOD$DELIMITER")
@FormUrlEncoded
fun authoriz( fun authoriz(
@Query(CLIENT_TYPE_PARAM) clientType: String = DEFAULT_CLIENT_TYPE, @Query(CLIENT_TYPE_PARAM) clientType: String = DEFAULT_CLIENT_TYPE,
@Query(CLIENT_VERSION_PARAM) clientVersion: String = DEFAULT_CLIENT_VERSION, @Query(CLIENT_VERSION_PARAM) clientVersion: String = DEFAULT_CLIENT_VERSION,
@Query(API_VERSION_PARAM) apiVersion: String = DEFAULT_API_VERSION, @Query(API_VERSION_PARAM) apiVersion: String = DEFAULT_API_VERSION,
@Query(EMAIL_AUTH_PARAM) email: String, @Field(EMAIL_AUTH_PARAM) email: String,
@Query(PASSWORD_AUTH_PARAM) pwd: String @Field(PASSWORD_AUTH_PARAM) pwd: String
): Observable<Response<AuthRaw>> ): Observable<Response<AuthRaw>>
......
...@@ -2,12 +2,7 @@ package com.biganto.visual.roompark.data.repository.api.retrofit.util ...@@ -2,12 +2,7 @@ package com.biganto.visual.roompark.data.repository.api.retrofit.util
import com.biganto.visual.roompark.data.repository.api.retrofit.raw.ErrorRaw import com.biganto.visual.roompark.data.repository.api.retrofit.raw.ErrorRaw
import com.biganto.visual.roompark.domain.custom_exception.parseException import com.biganto.visual.roompark.domain.custom_exception.parseException
import com.google.gson.Gson import com.google.gson.*
import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import org.json.JSONArray
import org.json.JSONObject
import timber.log.Timber import timber.log.Timber
import java.lang.reflect.Type import java.lang.reflect.Type
...@@ -23,9 +18,9 @@ class CustomExceptionDeserializer<T> : JsonDeserializer<T> { ...@@ -23,9 +18,9 @@ class CustomExceptionDeserializer<T> : JsonDeserializer<T> {
//region valid case //region valid case
when(json){ when(json){
is JSONObject -> if (!json.asJsonObject.keySet().contains("errors")) is JsonObject -> if (!json.asJsonObject.keySet().contains("errors"))
return Gson().newBuilder().create().fromJson(json,typeOfT) return Gson().newBuilder().create().fromJson(json,typeOfT)
is JSONArray -> return Gson().newBuilder().create().fromJson(json,typeOfT) is JsonArray -> return Gson().newBuilder().create().fromJson(json,typeOfT)
} }
//endregion valid case //endregion valid case
...@@ -35,10 +30,10 @@ class CustomExceptionDeserializer<T> : JsonDeserializer<T> { ...@@ -35,10 +30,10 @@ class CustomExceptionDeserializer<T> : JsonDeserializer<T> {
Timber.w("api errorlist: $errorList") Timber.w("api errorlist: $errorList")
when(json) { when(json) {
is JSONArray -> errorList.asJsonArray.forEach{ is JsonArray -> errorList.asJsonArray.forEach{
vals.add(ctx.deserialize<T>(it, ErrorRaw::class.java) as ErrorRaw) vals.add(ctx.deserialize<T>(it, ErrorRaw::class.java) as ErrorRaw)
} }
is JSONObject -> json.asJsonObject.get("errors").asJsonArray.forEach { is JsonObject -> json.asJsonObject.get("errors").asJsonArray.forEach {
vals.add(ctx.deserialize<T>(it, ErrorRaw::class.java) as ErrorRaw) vals.add(ctx.deserialize<T>(it, ErrorRaw::class.java) as ErrorRaw)
} }
else -> throw RuntimeException("Unexpected JSON type: " + json.javaClass) else -> throw RuntimeException("Unexpected JSON type: " + json.javaClass)
......
...@@ -12,6 +12,7 @@ import com.google.android.material.button.MaterialButton ...@@ -12,6 +12,7 @@ import com.google.android.material.button.MaterialButton
import com.google.android.material.textfield.TextInputLayout import com.google.android.material.textfield.TextInputLayout
import com.jakewharton.rxbinding3.view.clicks import com.jakewharton.rxbinding3.view.clicks
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
...@@ -31,25 +32,21 @@ class AuthScreenController : ...@@ -31,25 +32,21 @@ class AuthScreenController :
@BindView(R.id.password_text_input) lateinit var pwdInput:TextInputLayout @BindView(R.id.password_text_input) lateinit var pwdInput:TextInputLayout
@BindView(R.id.sign_in_button) lateinit var signInButton:MaterialButton @BindView(R.id.sign_in_button) lateinit var signInButton:MaterialButton
override fun tryAuth(): Observable<AuthInputModel> = override fun tryAuth(): Observable<AuthInputModel> =
signInButton.clicks() signInButton.clicks()
.doOnNext{signInButton.hideKeyboard()}
.map<AuthInputModel>{ AuthInputModel(loginInput.editText?.text?.toString()?:"" .map<AuthInputModel>{ AuthInputModel(loginInput.editText?.text?.toString()?:""
,pwdInput.editText?.text?.toString()?:"") ,pwdInput.editText?.text?.toString()?:"")
} }
.observeOn(AndroidSchedulers.mainThread())
override fun injectDependencies() { override fun injectDependencies() {
getComponent() getComponent()
} }
@Inject @Inject
override lateinit var injectedPresenter: AuthScreenPresenter override lateinit var injectedPresenter: AuthScreenPresenter
// @Inject // @Inject
// lateinit var snacky:ISnackBarProvider // lateinit var snacky:ISnackBarProvider
...@@ -63,8 +60,6 @@ class AuthScreenController : ...@@ -63,8 +60,6 @@ class AuthScreenController :
// @Inject // @Inject
// lateinit var ac: RoomParkMainActivity // lateinit var ac: RoomParkMainActivity
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")
......
...@@ -30,12 +30,12 @@ class AuthScreenPresenter @Inject constructor( ...@@ -30,12 +30,12 @@ class AuthScreenPresenter @Inject constructor(
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.SignedIn() }
.map { AuthScreenViewState.SignedIn() } .onErrorReturn{parseError(it)}
} }
// .startWith(Observable.just(AuthScreenViewState.Authorization())) // .startWith(Observable.just(AuthScreenViewState.Authorization()))
...@@ -43,7 +43,7 @@ class AuthScreenPresenter @Inject constructor( ...@@ -43,7 +43,7 @@ class AuthScreenPresenter @Inject constructor(
val state = restoreStateObservable val state = restoreStateObservable
.mergeWith(onAuth) .mergeWith(onAuth)
.doOnError { Timber.e(it) } .doOnError { Timber.e(it) }
.onErrorReturn(::parseError) .onErrorReturn(::parseError)
.subscribeOn(Schedulers.single()) .subscribeOn(Schedulers.single())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
......
...@@ -18,7 +18,7 @@ sealed class Either<out E, out V> { ...@@ -18,7 +18,7 @@ sealed class Either<out E, out V> {
data class ExceptionString(@StringRes private val stringId: Int?, private val 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) = fun selectHandler(a1:(Int)->Unit,a2:(String)->Unit) =
when(errorMessage){ when(errorMessage){
...@@ -27,13 +27,16 @@ data class ExceptionString(@StringRes private val stringId: Int?, private val m ...@@ -27,13 +27,16 @@ data class ExceptionString(@StringRes private val stringId: Int?, private val m
} }
constructor(e:CustomApiException) : this( constructor(e:CustomApiException) : this(
if (e.message!=null) null else e.messageStringId, if (e.customMessage!=null) null else e.messageStringId,
if (e.messageStringId!=null) null else e.message e.customMessage
) )
init { val errorMessage : Either<Int,String> = if (stringId!=null) Either.Left(stringId) else Either.Right(message!!)
assert(stringId==null && message==null) { "both values cannot be null!" }
errorMessage = if (stringId!=null) Either.Left(stringId) else Either.Right(message!!) // init {
} // Timber.d(" messages: $stringId / $message")
// if (stringId==null && message==null) throw error{ "both values cannot be null!" }
// errorMessage = if (stringId!=null) Either.Left(stringId) else Either.Right(message!!)
// }
} }
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