|
8 | 8 | import android.view.View; |
9 | 9 | import android.view.ViewGroup; |
10 | 10 | import android.widget.ProgressBar; |
11 | | - |
| 11 | +import butterknife.Bind; |
| 12 | +import butterknife.ButterKnife; |
12 | 13 | import com.morihacky.android.rxjava.MainActivity; |
13 | 14 | import com.morihacky.android.rxjava.R; |
14 | 15 | import com.morihacky.android.rxjava.fragments.BaseFragment; |
15 | 16 | import com.morihacky.android.rxjava.rxbus.RxBus; |
16 | | - |
17 | 17 | import java.util.ArrayList; |
18 | 18 | import java.util.List; |
19 | 19 | import java.util.concurrent.TimeUnit; |
20 | | - |
21 | | -import butterknife.Bind; |
22 | | -import butterknife.ButterKnife; |
23 | 20 | import rx.Observable; |
24 | 21 | import rx.Subscription; |
25 | 22 | import rx.android.schedulers.AndroidSchedulers; |
26 | 23 | import rx.subjects.PublishSubject; |
27 | 24 | import rx.subscriptions.CompositeSubscription; |
28 | 25 |
|
29 | | -public class PaginationAutoFragment extends BaseFragment { |
| 26 | +public class PaginationAutoFragment |
| 27 | + extends BaseFragment { |
30 | 28 |
|
31 | 29 | @Bind(R.id.list_paging) RecyclerView _pagingList; |
32 | 30 | @Bind(R.id.progress_paging) ProgressBar _progressBar; |
33 | 31 |
|
34 | | - private CompositeSubscription _subscriptions; |
35 | 32 | private PaginationAdapter _adapter; |
36 | 33 | private RxBus _bus; |
37 | 34 | private PublishSubject<Integer> _paginator; |
38 | 35 | private boolean _requestUnderWay = false; |
| 36 | + private CompositeSubscription _subscriptions; |
| 37 | + |
| 38 | + @Override |
| 39 | + public View onCreateView(LayoutInflater inflater, |
| 40 | + @Nullable ViewGroup container, |
| 41 | + @Nullable Bundle savedInstanceState) { |
| 42 | + View layout = inflater.inflate(R.layout.fragment_pagination, container, false); |
| 43 | + ButterKnife.bind(this, layout); |
| 44 | + return layout; |
| 45 | + } |
39 | 46 |
|
40 | 47 | @Override |
41 | 48 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { |
@@ -59,28 +66,31 @@ public void onStart() { |
59 | 66 | _subscriptions = new CompositeSubscription(); |
60 | 67 |
|
61 | 68 | Subscription s2 =// |
62 | | - _paginator |
63 | | - .onBackpressureDrop() |
64 | | - .doOnNext(i -> _requestUnderWay = true) |
| 69 | + _paginator.onBackpressureDrop() |
| 70 | + .doOnNext(i -> { |
| 71 | + _requestUnderWay = true; |
| 72 | + _progressBar.setVisibility(View.VISIBLE); |
| 73 | + }) |
65 | 74 | .concatMap(nextPage -> _itemsFromNetworkCall(nextPage + 1, 10)) |
66 | 75 | .observeOn(AndroidSchedulers.mainThread()) |
67 | 76 | .map(items -> { |
68 | 77 | int start = _adapter.getItemCount() - 1; |
69 | 78 |
|
70 | 79 | _adapter.addItems(items); |
71 | 80 | _adapter.notifyItemRangeInserted(start, 10); |
72 | | - |
| 81 | + return null; |
| 82 | + }) |
| 83 | + .doOnNext(i -> { |
73 | 84 | _requestUnderWay = false; |
74 | 85 | _progressBar.setVisibility(View.INVISIBLE); |
75 | | - return null; |
76 | 86 | }) |
77 | 87 | .subscribe(); |
78 | 88 |
|
79 | | - // I'm using an Rxbus purely to hear from a nested button click |
| 89 | + // I'm using an RxBus purely to hear from a nested button click |
80 | 90 | // we don't really need Rx for this part. it's just easy ¯\_(ツ)_/¯ |
81 | 91 | Subscription s1 =// |
82 | | - _bus.asObservable() |
83 | | - .filter(o -> !_requestUnderWay) |
| 92 | + _bus.asObservable()// |
| 93 | + .filter(o -> !_requestUnderWay)// |
84 | 94 | .subscribe(event -> { |
85 | 95 | if (event instanceof PaginationAdapter.ItemBtnViewHolder.PageEvent) { |
86 | 96 |
|
@@ -104,10 +114,9 @@ public void onStop() { |
104 | 114 | * Fake Observable that simulates a network call and then sends down a list of items |
105 | 115 | */ |
106 | 116 | private Observable<List<String>> _itemsFromNetworkCall(int start, int count) { |
107 | | - return Observable.just(true) |
108 | | - .observeOn(AndroidSchedulers.mainThread()) |
109 | | - .doOnNext(dummy -> _progressBar.setVisibility(View.VISIBLE)) |
110 | | - .delay(2, TimeUnit.SECONDS) |
| 117 | + return Observable.just(true)// |
| 118 | + .observeOn(AndroidSchedulers.mainThread())// |
| 119 | + .delay(2, TimeUnit.SECONDS)// |
111 | 120 | .map(dummy -> { |
112 | 121 | List<String> items = new ArrayList<>(); |
113 | 122 | for (int i = 0; i < count; i++) { |
@@ -116,19 +125,4 @@ private Observable<List<String>> _itemsFromNetworkCall(int start, int count) { |
116 | 125 | return items; |
117 | 126 | }); |
118 | 127 | } |
119 | | - |
120 | | - |
121 | | - // ----------------------------------------------------------------------------------- |
122 | | - // WIRING up the views required for this example |
123 | | - |
124 | | - @Override |
125 | | - public View onCreateView(LayoutInflater inflater, |
126 | | - @Nullable ViewGroup container, |
127 | | - @Nullable Bundle savedInstanceState) { |
128 | | - View layout = inflater.inflate(R.layout.fragment_pagination, container, false); |
129 | | - ButterKnife.bind(this, layout); |
130 | | - return layout; |
131 | | - } |
132 | | - |
133 | | - |
134 | 128 | } |