-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ViewState has a ViewStatefulEvent.kt as a state descriptor and payload as something that holds the value ViewStateContract.kt data is now renamed to viewState ViewEventContract now doesn't force you to integrate val viewEvent: Flow<ViewEvent> and is also a functional contract Check MVIFragment.kt for sample
- Loading branch information
Showing
11 changed files
with
181 additions
and
138 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
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
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
63 changes: 0 additions & 63 deletions
63
retrofit/src/main/java/com/crazylegend/retrofit/viewstate/event/ViewEvent.kt
This file was deleted.
Oops, something went wrong.
9 changes: 2 additions & 7 deletions
9
retrofit/src/main/java/com/crazylegend/retrofit/viewstate/event/ViewEventContract.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,10 +1,5 @@ | ||
package com.crazylegend.retrofit.viewstate.event | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface ViewEventContract { | ||
//decouple this as well so that a direct flow is not forced, hmm | ||
val viewEvent: Flow<ViewEvent> | ||
|
||
suspend fun provideEvent(viewEvent: ViewEvent) | ||
fun interface ViewEventContract { | ||
suspend fun provideEvent(viewStatefulEvent: ViewStatefulEvent) | ||
} |
85 changes: 85 additions & 0 deletions
85
retrofit/src/main/java/com/crazylegend/retrofit/viewstate/event/ViewStatefulEvent.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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.crazylegend.retrofit.viewstate.event | ||
|
||
import okhttp3.ResponseBody | ||
|
||
/** | ||
* Created by funkymuse on 11/20/21 to long live and prosper ! | ||
*/ | ||
sealed interface ViewStatefulEvent { | ||
data class Error(val throwable: Throwable) : ViewStatefulEvent | ||
data class ApiError(val errorBody: ResponseBody?, val responseCode: Int) : ViewStatefulEvent | ||
|
||
object Success : ViewStatefulEvent | ||
object Loading : ViewStatefulEvent | ||
object Idle : ViewStatefulEvent | ||
} | ||
|
||
val ViewStatefulEvent.isLoading get() = this is ViewStatefulEvent.Loading | ||
val ViewStatefulEvent.isNotLoading get() = this !is ViewStatefulEvent.Loading | ||
|
||
val ViewStatefulEvent.isIdle get() = this is ViewStatefulEvent.Idle | ||
val ViewStatefulEvent.isNotIdle get() = this !is ViewStatefulEvent.Idle | ||
|
||
val ViewStatefulEvent.isError get() = this is ViewStatefulEvent.Error | ||
val ViewStatefulEvent.isNotError get() = this !is ViewStatefulEvent.Error | ||
|
||
val ViewStatefulEvent.isApiError get() = this is ViewStatefulEvent.ApiError | ||
val ViewStatefulEvent.isNotApiError get() = this !is ViewStatefulEvent.ApiError | ||
|
||
val ViewStatefulEvent.isErrorOrApiError get() = this is ViewStatefulEvent.Error || this is ViewStatefulEvent.ApiError | ||
|
||
val ViewStatefulEvent.isSuccess get() = this is ViewStatefulEvent.Success | ||
val ViewStatefulEvent.isNotSuccess get() = this !is ViewStatefulEvent.Success | ||
|
||
val ViewStatefulEvent.asError get() = (this as ViewStatefulEvent.Error) | ||
val ViewStatefulEvent.asErrorThrowable get() = (this as ViewStatefulEvent.Error).throwable | ||
val ViewStatefulEvent.asApiError get() = (this as ViewStatefulEvent.ApiError) | ||
val ViewStatefulEvent.asApiErrorBody get() = (this as ViewStatefulEvent.ApiError).errorBody | ||
val ViewStatefulEvent.asApiErrorCode get() = (this as ViewStatefulEvent.ApiError).responseCode | ||
|
||
|
||
val ViewStatefulEvent.getAsThrowable: Throwable? get() = if (this is ViewStatefulEvent.Error) throwable else null | ||
val ViewStatefulEvent.getAsApiFailureCode: Int? get() = if (this is ViewStatefulEvent.ApiError) responseCode else null | ||
val ViewStatefulEvent.getAsApiResponseBody: ResponseBody? get() = if (this is ViewStatefulEvent.ApiError) errorBody else null | ||
|
||
fun ViewStatefulEvent.asSuccess() = this as ViewStatefulEvent.Success | ||
fun ViewStatefulEvent.asLoading() = this as ViewStatefulEvent.Loading | ||
fun ViewStatefulEvent.asError() = this as ViewStatefulEvent.Error | ||
fun ViewStatefulEvent.asApiError() = this as ViewStatefulEvent.ApiError | ||
fun ViewStatefulEvent.asIdle() = this as ViewStatefulEvent.Idle | ||
|
||
inline fun ViewStatefulEvent.onError(action: (Throwable) -> Unit): ViewStatefulEvent { | ||
if (this is ViewStatefulEvent.Error) { | ||
action(throwable) | ||
} | ||
return this | ||
} | ||
|
||
inline fun ViewStatefulEvent.onSuccess(action: () -> Unit): ViewStatefulEvent { | ||
if (this is ViewStatefulEvent.Success) { | ||
action() | ||
} | ||
return this | ||
} | ||
|
||
inline fun ViewStatefulEvent.onIdle(action: () -> Unit): ViewStatefulEvent { | ||
if (this is ViewStatefulEvent.Idle) { | ||
action() | ||
} | ||
return this | ||
} | ||
|
||
inline fun ViewStatefulEvent.onLoading(action: () -> Unit): ViewStatefulEvent { | ||
if (this is ViewStatefulEvent.Loading) { | ||
action() | ||
} | ||
return this | ||
} | ||
|
||
inline fun ViewStatefulEvent.onApiError(action: (errorBody: ResponseBody?, responseCode: Int) -> Unit): ViewStatefulEvent { | ||
if (this is ViewStatefulEvent.ApiError) { | ||
action(errorBody, responseCode) | ||
} | ||
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
3 changes: 2 additions & 1 deletion
3
retrofit/src/main/java/com/crazylegend/retrofit/viewstate/state/ViewStateContract.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
Oops, something went wrong.