Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. subscribeOnModel(RxView.getScrollObservable(LIMIT, listView, true)
  2. .subscribeOn(AndroidSchedulers.mainThread())
  3. .observeOn(Schedulers.io())
  4. .flatMap(paginationInfo -> threadsInteractor.fetchThreadMessages(paginationInfo.getLimit(), chatInfo))
  5. .toObservable()
  6. .observeOn(AndroidSchedulers.mainThread()), pullToRefresh);
  7.  
  8.  
  9. public static Flowable<PaginationInfo> getScrollObservable(int limit, RecyclerView recyclerView, boolean avtoStart) {
  10. final PaginationInfo paginationInfo = new PaginationInfo(limit);
  11.  
  12. return Flowable.create(emitter -> {
  13. RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
  14. if (layoutManager instanceof LinearLayoutManager) {
  15. LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
  16. RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {
  17. @Override
  18. public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
  19. super.onScrolled(recyclerView, dx, dy);
  20. if (!emitter.isCancelled()) {
  21. int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition() + 1;
  22. int updatePosition = paginationInfo.getLoadedCount();
  23. if (lastVisibleItemPosition >= updatePosition) {
  24. emitter.onNext(paginationInfo);
  25. paginationInfo.addLoadedCount();
  26. }
  27. }
  28. }
  29. };
  30. recyclerView.addOnScrollListener(scrollListener);
  31. emitter.setCancellable(() -> recyclerView.removeOnScrollListener(scrollListener));
  32. if (avtoStart) {
  33. emitter.onNext(paginationInfo);
  34. }
  35. /*if (adapter.itemCount == emptyListCount)
  36. emitter.onNext(adapter.itemCount);*/
  37. } else {
  38. throw new RuntimeException("layoutManager must be LinearLayoutManager");
  39. }
  40. }, BackpressureStrategy.DROP);
  41. }
  42.  
  43. public static class PaginationInfo {
  44. public static final int EMPTY_LIST_COUNT = 0;
  45. private final int limit;
  46. private int loadedCount;
  47.  
  48. PaginationInfo(int limit) {
  49. this.limit = limit;
  50. }
  51.  
  52. public int getLoadedCount() {
  53. return loadedCount;
  54. }
  55.  
  56. public void addLoadedCount() {
  57. loadedCount += limit;
  58. }
  59.  
  60. public int getLimit() {
  61. return limit;
  62. }
  63. }
  64. protected void subscribeOnModel(Observable<M> observable, final boolean pullToRefresh) {
  65. subscribeOnModel(Schedulers.io(), observable, pullToRefresh);
  66. }
  67.  
  68. protected void subscribeOnModel(Scheduler scheduler, Observable<M> observable, final boolean pullToRefresh) {
  69. if (modelDisposable != null) {
  70. compositeDisposable.remove(modelDisposable);
  71. }
  72. Observer<M> observer = new Observer<M>() {
  73.  
  74. @Override
  75. public void onComplete() {
  76. compositeDisposable.remove(modelDisposable);
  77. MvpLceRxPresenter.this.onComplete();
  78. }
  79.  
  80. @Override
  81. public void onError(Throwable e) {
  82. compositeDisposable.remove(modelDisposable);
  83. MvpLceRxPresenter.this.onError(e, pullToRefresh);
  84. }
  85.  
  86. @Override
  87. public void onSubscribe(Disposable disposable) {
  88. modelDisposable = disposable;
  89. compositeDisposable.add(disposable);
  90. MvpLceRxPresenter.this.onSubscribe(pullToRefresh);
  91. }
  92.  
  93. @Override
  94. public void onNext(M m) {
  95. MvpLceRxPresenter.this.onNext(m);
  96. }
  97. };
  98.  
  99. observable.subscribeOn(scheduler)
  100. .observeOn(AndroidSchedulers.mainThread())
  101. .subscribe(observer);
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement