Skip to content

Instantly share code, notes, and snippets.

View agustarc's full-sized avatar
🎯
Focusing

Leopold agustarc

🎯
Focusing
View GitHub Profile
single.retryWhen { attempts ->
Flowables.zip(
attempts.map { error -> if (predicates.count { it(error) } > 0) error else throw error },
Flowable.interval(interval, unit)
).map { (error, retryCount) -> if (retryCount >= maxRetries) throw error }
}
single.onErrorResumeNext(resumeNext)
internal class ImagesRepositoryImpl(private val source: ImageDataSource) : ImagesRepository {
override fun getImages(params: Map<String, String>): Single<Result<ImagesResponse>> {
return source.getImages(params)
.map { Result.success(it) }
.onErrorResumeNext { Single.just(Result.failure(it)) }
.compose(applyRetryPolicy(TIMEOUT) { Single.just(Result.failure(it)) })
}
}
internal class ImagesRepositoryImpl(private val source: ImageDataSource) : ImagesRepository {
override fun getImages(params: Map<String, String>): Single<Result<ImagesResponse>> {
return source.getImages(params)
.map { Result.success(it) }
.compose(applyRetryPolicy(TIMEOUT, NETWORK, SERVICE_UNAVAILABLE) { Single.just(Result.failure(it)) })
}
}
internal typealias RETRY_PREDICATE = (Throwable) -> Boolean
internal const val MAX_RETRIES = 3L
internal const val DEFAULT_INTERVAL = 1L
internal val TIMEOUT: RETRY_PREDICATE = { it is SocketTimeoutException }
internal val NETWORK: RETRY_PREDICATE = { it is IOException }
internal val SERVICE_UNAVAILABLE: RETRY_PREDICATE = { it is HttpException && it.code() == 503 }
interface ImagesRepository {
fun getImages(params: Map<String, String>): Single<Result<ImagesResponse>>
}
internal class ImagesRepositoryImpl(private val source: ImageDataSource) : ImagesRepository {
override fun getImages(params: Map<String, String>): Single<Result<ImagesResponse>> {
return source.getImages(params)
.map { Result.success(it) }
.compose(applyRetryPolicy(TIMEOUT) { Single.just(Result.failure(it)) })
}
retryWhen { attempts ->
Flowables.zip(
attempts.map { error -> if (error is IOException) error else throw error },
Flowable.interval(1, TimeUnit.SECONDS)
).map { (error, retryCount) -> if (retryCount >= 3) throw error }
}
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final Single<T> retryWhen(Function<? super Flowable<Throwable>, ? extends Publisher<?>> handler) {
return toSingle(toFlowable().retryWhen(handler));
}
class SearchViewModel(private val api: SearchAPI, private val dao: BookmarkDao) : DisposableViewModel() {
private var query: String = ""
get() = if (field.isEmpty()) "MVVM" else field
private val _refreshing: NotNullMutableLiveData<Boolean> = NotNullMutableLiveData(false)
val refreshing: LiveData<Boolean>
get() = _refreshing
private val _items: NotNullMutableLiveData<List<Repository>> = NotNullMutableLiveData(arrayListOf())
val items: LiveData<List<Repository>>
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
Koap.serializer = GsonSerializer(Gson())
Koap.bind(this, SettingPreference, AccountPreference)
}
}
Koap.serializer = GsonSerializer(Gson())