-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add transformers and make error and api error delegate the needed values
- Loading branch information
Showing
3 changed files
with
39 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 26 additions & 2 deletions
28
retrofit/src/main/java/com/crazylegend/retrofit/viewstate/ViewEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters