Skip to content

Instantly share code, notes, and snippets.

@IlyaGulya
Created May 21, 2018 12:24
Show Gist options
  • Save IlyaGulya/0bf8cabbb79b609e56f315242032c090 to your computer and use it in GitHub Desktop.
Save IlyaGulya/0bf8cabbb79b609e56f315242032c090 to your computer and use it in GitHub Desktop.
private abstract class RequestFactoryAdapter<T> {
lateinit var paginator: Paginator<T>
abstract fun loadPage(page: Int)
protected fun onSuccess(data: List<T>) {
paginator.currentState.newData(data)
}
protected fun onError(error: Throwable) {
Timber.e("strange error")
Timber.e(error)
paginator.currentState.fail(error)
}
}
private class RxJava2Adapter<T>(
val requestFactory: (Int) -> io.reactivex.Single<List<T>>
) : RequestFactoryAdapter<T>() {
var disposable: Disposable? = null
override fun loadPage(page: Int) {
disposable?.dispose()
disposable = requestFactory.invoke(page)
.subscribe(
{ onSuccess(it) },
{ onError(it) }
)
}
}
private class RxJavaAdapter<T>(
val requestFactory: (Int) -> Single<List<T>>
) : RequestFactoryAdapter<T>() {
var subscription: Subscription? = null
override fun loadPage(page: Int) {
subscription?.unsubscribe()
subscription = requestFactory.invoke(page)
.subscribe(
{ onSuccess(it) },
{ onError(it) }
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment