Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- subscribeOnModel(RxView.getScrollObservable(LIMIT, listView, true)
- .subscribeOn(AndroidSchedulers.mainThread())
- .observeOn(Schedulers.io())
- .flatMap(paginationInfo -> threadsInteractor.fetchThreadMessages(paginationInfo.getLimit(), chatInfo))
- .toObservable()
- .observeOn(AndroidSchedulers.mainThread()), pullToRefresh);
- public static Flowable<PaginationInfo> getScrollObservable(int limit, RecyclerView recyclerView, boolean avtoStart) {
- final PaginationInfo paginationInfo = new PaginationInfo(limit);
- return Flowable.create(emitter -> {
- RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
- if (layoutManager instanceof LinearLayoutManager) {
- LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
- RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {
- @Override
- public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
- super.onScrolled(recyclerView, dx, dy);
- if (!emitter.isCancelled()) {
- int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition() + 1;
- int updatePosition = paginationInfo.getLoadedCount();
- if (lastVisibleItemPosition >= updatePosition) {
- emitter.onNext(paginationInfo);
- paginationInfo.addLoadedCount();
- }
- }
- }
- };
- recyclerView.addOnScrollListener(scrollListener);
- emitter.setCancellable(() -> recyclerView.removeOnScrollListener(scrollListener));
- if (avtoStart) {
- emitter.onNext(paginationInfo);
- }
- /*if (adapter.itemCount == emptyListCount)
- emitter.onNext(adapter.itemCount);*/
- } else {
- throw new RuntimeException("layoutManager must be LinearLayoutManager");
- }
- }, BackpressureStrategy.DROP);
- }
- public static class PaginationInfo {
- public static final int EMPTY_LIST_COUNT = 0;
- private final int limit;
- private int loadedCount;
- PaginationInfo(int limit) {
- this.limit = limit;
- }
- public int getLoadedCount() {
- return loadedCount;
- }
- public void addLoadedCount() {
- loadedCount += limit;
- }
- public int getLimit() {
- return limit;
- }
- }
- protected void subscribeOnModel(Observable<M> observable, final boolean pullToRefresh) {
- subscribeOnModel(Schedulers.io(), observable, pullToRefresh);
- }
- protected void subscribeOnModel(Scheduler scheduler, Observable<M> observable, final boolean pullToRefresh) {
- if (modelDisposable != null) {
- compositeDisposable.remove(modelDisposable);
- }
- Observer<M> observer = new Observer<M>() {
- @Override
- public void onComplete() {
- compositeDisposable.remove(modelDisposable);
- MvpLceRxPresenter.this.onComplete();
- }
- @Override
- public void onError(Throwable e) {
- compositeDisposable.remove(modelDisposable);
- MvpLceRxPresenter.this.onError(e, pullToRefresh);
- }
- @Override
- public void onSubscribe(Disposable disposable) {
- modelDisposable = disposable;
- compositeDisposable.add(disposable);
- MvpLceRxPresenter.this.onSubscribe(pullToRefresh);
- }
- @Override
- public void onNext(M m) {
- MvpLceRxPresenter.this.onNext(m);
- }
- };
- observable.subscribeOn(scheduler)
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(observer);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement