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