Created
April 11, 2017 19:57
-
-
Save eliasbagley/68427f4587505ad76801edf9214c8169 to your computer and use it in GitHub Desktop.
Backpressure enabled RxJava resource subscriber for endless scroll
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
package com.example; | |
import java.util.List; | |
import io.reactivex.plugins.RxJavaPlugins; | |
import io.reactivex.subscribers.ResourceSubscriber; | |
import lombok.val; | |
/** | |
* Created by eliasbagley on 4/5/17. | |
*/ | |
public class PagedResourceSubscriber { | |
private PagedResourceSubscriber() { | |
throw new AssertionError("No instances."); | |
} | |
public static <T> ResourceSubscriber<List<T>> create(AppendableAdapter<T> adapter, EndlessScroll endlessScroll) { | |
return new ResourceSubscriber<List<T>>() { | |
@Override | |
public void onStart() { | |
// When the onLoadMore observable fires, request another page of data. | |
val d = endlessScroll.onLoadMore() | |
.subscribe(o -> request(1)); | |
add(d); | |
} | |
@Override | |
public void onNext(List<T> list) { | |
adapter.append(list); | |
endlessScroll.setLoading(false); | |
} | |
//TODO | |
@Override | |
public void onError(Throwable t) { | |
RxJavaPlugins.onError(t); | |
} | |
@Override public void onComplete() {} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment