Skip to content

Commit

Permalink
add transformers and make error and api error delegate the needed values
Browse files Browse the repository at this point in the history
  • Loading branch information
FunkyMuse committed Nov 23, 2021
1 parent b7f2302 commit b485f8d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ val retrofitLoading get() = RetrofitResult.Loading
val retrofitIdle get() = RetrofitResult.Idle

fun <T> retrofitSubscribe(response: Response<T>): RetrofitResult<T> =
response.unwrapResponseToModel()?.let { retrofitSuccess(it) }
?: retrofitApiError(response.code(), response.errorBody())
response.unwrapResponseToModel()?.let { retrofitSuccess(it) }
?: retrofitApiError(response.code(), response.errorBody())


//region State
Expand Down Expand Up @@ -93,8 +93,15 @@ suspend fun <T> RetrofitResult<T>.onSuccessSuspend(function: suspend (model: T)
//endregion


fun <VALUE, TRANSFORM> RetrofitResult<VALUE>.transform(transformer: (VALUE) -> TRANSFORM) :TRANSFORM? =
getAsSuccess?.let { value -> transformer(value) }
fun <VALUE, TRANSFORM> RetrofitResult<VALUE>.transformAsSuccess(transformer: (VALUE) -> TRANSFORM): TRANSFORM? =
getAsSuccess?.let { value -> transformer(value) }

fun <VALUE, TRANSFORM> RetrofitResult<VALUE>.transform(transformer: (VALUE) -> TRANSFORM): RetrofitResult<TRANSFORM> =
if (this is RetrofitResult.Success) {
RetrofitResult.Success(transformer(value))
} else {
this as RetrofitResult<TRANSFORM>
}


fun <T> Response<T>.unwrapResponseToModel(): T? = when {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
package com.crazylegend.retrofit.viewstate

import okhttp3.ResponseBody

/**
* Created by funkymuse on 11/20/21 to long live and prosper !
*/
sealed interface ViewEvent {
data class Error(val throwable: Throwable) : ViewEvent
data class ApiError(val errorBody: ResponseBody?, val code: Int) : ViewEvent

object Success : ViewEvent
object Loading : ViewEvent
object Idle : ViewEvent
object Error : ViewEvent
object ApiError : ViewEvent
}

val ViewEvent.isLoading get() = this is ViewEvent.Loading
val ViewEvent.isIdle get() = this is ViewEvent.Idle
val ViewEvent.isError get() = this is ViewEvent.Error
val ViewEvent.isApiError get() = this is ViewEvent.ApiError
val ViewEvent.isSuccess get() = this is ViewEvent.Success

val ViewEvent.asError get() = (this as ViewEvent.Error)
val ViewEvent.asErrorThrowable get() = (this as ViewEvent.Error).throwable
val ViewEvent.asApiError get() = (this as ViewEvent.ApiError)
val ViewEvent.asApiErrorBody get() = (this as ViewEvent.ApiError).errorBody
val ViewEvent.asApiErrorCode get() = (this as ViewEvent.ApiError).code

fun ViewEvent.onError(action: (Throwable) -> Unit): ViewEvent {
if (this is ViewEvent.Error) {
action(throwable)
}
return this
}

fun ViewEvent.onApiError(action: (errorBody: ResponseBody?, responseCode: Int) -> Unit): ViewEvent {
if (this is ViewEvent.ApiError) {
action(errorBody, code)
}
return this
}

Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class ViewState<T>(
dataState.value = retrofitResult
retrofitResult
.onLoading { viewEvents.send(ViewEvent.Loading) }
.onError { viewEvents.send(ViewEvent.Error) }
.onApiError { _, _ -> viewEvents.send(ViewEvent.ApiError) }
.onError { throwable -> viewEvents.send(ViewEvent.Error(throwable)) }
.onApiError { errorBody, code -> viewEvents.send(ViewEvent.ApiError(errorBody, code)) }
.onIdle { viewEvents.send(ViewEvent.Idle) }
.onSuccess { viewEvents.send(ViewEvent.Success) }
}
Expand Down

0 comments on commit b485f8d

Please sign in to comment.