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

upgrade base presenter to hook exceptions by overriding only to simple methods

parent ad925ad9
package com.biganto.visual.roompark.conductor package com.biganto.visual.roompark.conductor
import com.biganto.visual.roompark.R
import com.biganto.visual.roompark.data.service.network.NoNetworkException
import com.biganto.visual.roompark.domain.custom_exception.CustomApiException
import com.biganto.visual.roompark.util.monades.ExceptionString
import com.hannesdorfmann.mosby3.mvi.MviBasePresenter 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
/** /**
* Created by Vladislav Bogdashkin on 30.09.2019. * Created by Vladislav Bogdashkin on 30.09.2019.
*/ */
abstract class BigantoBasePresenter<V : MvpView, VS> abstract class BigantoBasePresenter<V : MvpView, VS>
: MviBasePresenter<V, VS>(){ : MviBasePresenter<V, VS>() {
protected val restoreStateObservable = protected val restoreStateObservable =
PublishRelay.create<VS>() PublishRelay.create<VS>()
fun observableParseError(t: Throwable): Observable<VS> =
Observable.defer { Observable.just(parseError(t)) }
abstract fun vsByCode(code: Int): (ExceptionString) -> VS
abstract fun vsByThrowable(t: Throwable): (ExceptionString) -> VS
open fun parseError(t: Throwable): VS =
when (t) {
is CustomApiException -> parseError(t)
is NoNetworkException -> parseError(t)
else -> parseError(t)
}
private fun parseError(e: CustomApiException) = onCodeReturn(e)
private fun parseError(e: NoNetworkException) = onNoNetwork(e)
private fun parseError(e: Exception) = onRandomError(e)
open fun onRandomError(t: Throwable): VS =
vsByThrowable(t).invoke(
ExceptionString(R.string.unknown_error, null)
)
open fun onNoNetwork(e: NoNetworkException): VS =
vsByThrowable(e).invoke(
ExceptionString(R.string.no_network_error, null)
)
private fun onCodeReturn(e: CustomApiException): VS =
vsByCode(e.code).invoke(ExceptionString(e))
} }
\ No newline at end of file
...@@ -9,7 +9,6 @@ import com.biganto.visual.roompark.conductor.BigantoBaseController ...@@ -9,7 +9,6 @@ import com.biganto.visual.roompark.conductor.BigantoBaseController
import com.biganto.visual.roompark.presentation.screen.home.home_routing.HomeBottomNavigationController import com.biganto.visual.roompark.presentation.screen.home.home_routing.HomeBottomNavigationController
import com.bluelinelabs.conductor.RouterTransaction import com.bluelinelabs.conductor.RouterTransaction
import com.google.android.material.button.MaterialButton import com.google.android.material.button.MaterialButton
import com.google.android.material.snackbar.Snackbar
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
...@@ -69,14 +68,12 @@ class AuthScreenController : ...@@ -69,14 +68,12 @@ 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 as BaseState){ when(viewState){
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)
} }
} }
......
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 com.biganto.visual.roompark.util.monades.ExceptionString
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
...@@ -21,29 +18,14 @@ class AuthScreenPresenter @Inject constructor( ...@@ -21,29 +18,14 @@ class AuthScreenPresenter @Inject constructor(
) )
: BigantoBasePresenter<AuthScreen, AuthScreenViewState>() { : BigantoBasePresenter<AuthScreen, AuthScreenViewState>() {
private fun parseError(t: Throwable): AuthScreenViewState = override fun vsByCode(code: Int): (ExceptionString) -> AuthScreenViewState =
when (t) { when (code) {
is CustomApiException -> onCodeReturn(e = t) 101 -> {e:ExceptionString -> AuthScreenViewState.SignInError(e)}
is NoNetworkException -> onNoNetwork(t) else -> {e:ExceptionString -> AuthScreenViewState.SomeError(e)}
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 vsByThrowable(t: Throwable): (ExceptionString) -> AuthScreenViewState =
{e:ExceptionString -> AuthScreenViewState.SomeError(e)}
override fun bindIntents() { override fun bindIntents() {
......
...@@ -81,7 +81,6 @@ interface ISnackBarProvider{ ...@@ -81,7 +81,6 @@ interface ISnackBarProvider{
val isRootBounded : Boolean val isRootBounded : Boolean
val snackBar : Snackbar? val snackBar : Snackbar?
fun showSnackBar(@StringRes stringId: Int) 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)
......
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