Skip to content

Instantly share code, notes, and snippets.


Ahmed Rizwan ahmedrizwan

Block or report user

Report or block ahmedrizwan

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View UIState.kt
sealed class UIState<out R> {
object Loading : UIState<Nothing>()
object Retrying : UIState<Nothing>()
object SwipeRefreshing : UIState<Nothing>()
data class Success<T>(val data: T) : UIState<T>()
data class Failure(val exception: Exception) : UIState<Nothing>()
data class SwipeRefreshFailure(val exception: Exception) : UIState<Nothing>()
View Action.kt
sealed class Action {
object Load : Action()
object SwipeRefresh : Action()
object Retry : Action()
View ActionStateLiveData.kt
class ActionStateLiveData<T>(
private val coroutineContext: CoroutineContext,
fetchData: (suspend () -> Response<T>)
) {
private val action = MutableLiveData<Action>()
private var data: T? = null // backing data
val state = action.switchMap {
liveData(context = coroutineContext) {
when (action.value) {
View Retry.kt
Observable.fromPublisher<String> {
it.onNext("Doing a network call!")
Thread.sleep(1000) // Long running process
it.onError(Exception()) // Some error thrown
}.retryWhen { errors ->
errors.zipWith(Observable.range(1, 3) // Zip error observable with a range one
.concatMap { retryCount ->
Observable.timer(retryCount.toLong() * 10, TimeUnit.SECONDS)
You can’t perform that action at this time.