|
10 | 10 | import android.view.View; |
11 | 11 | import android.view.ViewGroup; |
12 | 12 | import android.widget.ArrayAdapter; |
| 13 | +import android.widget.Button; |
13 | 14 | import android.widget.ListView; |
14 | 15 |
|
15 | 16 | import com.morihacky.android.rxjava.app.R; |
|
20 | 21 |
|
21 | 22 | import butterknife.ButterKnife; |
22 | 23 | import butterknife.InjectView; |
23 | | -import butterknife.OnClick; |
24 | 24 | import rx.Observable; |
25 | 25 | import rx.Observer; |
26 | 26 | import rx.Subscriber; |
| 27 | +import rx.Subscription; |
27 | 28 | import rx.android.schedulers.AndroidSchedulers; |
28 | 29 | import rx.schedulers.Schedulers; |
29 | 30 | import timber.log.Timber; |
30 | 31 |
|
| 32 | +/** |
| 33 | + * credit to @tomrozb for this implementation: |
| 34 | + * http://stackoverflow.com/questions/24922610/incorrect-understanding-of-buffer-in-rxjava |
| 35 | + * |
| 36 | + * An alternate mechanism of achieving the same result would be to use a {@link rx.subjects.PublishSubject} |
| 37 | + * as demonstrated in the case of {@link com.morihacky.android.rxjava.SubjectDebounceSearchEmitterFragment} |
| 38 | + */ |
31 | 39 | public class BufferDemoFragment |
32 | 40 | extends Fragment { |
33 | 41 |
|
34 | 42 | @InjectView(R.id.list_threading_log) ListView _logsList; |
| 43 | + @InjectView(R.id.btn_start_operation) Button _tapBtn; |
35 | 44 |
|
36 | 45 | private LogAdapter _adapter; |
37 | 46 | private List<String> _logs; |
38 | 47 | private int _tapCount = 0; |
39 | 48 |
|
40 | | - private Observable<List<Integer>> _bufferedObservable; |
41 | | - private Observer<List<Integer>> _observer; |
| 49 | + private Subscription _subscription; |
42 | 50 |
|
43 | 51 | @Override |
44 | | - public void onActivityCreated(@Nullable Bundle savedInstanceState) { |
45 | | - super.onActivityCreated(savedInstanceState); |
46 | | - _setupLogger(); |
47 | | - |
48 | | - _bufferedObservable = _getBufferedObservable(); |
49 | | - _observer = _getObserver(); |
| 52 | + public void onResume() { |
| 53 | + super.onResume(); |
| 54 | + _subscription = _getBufferedObservable().subscribe(_getObserver()); |
50 | 55 | } |
51 | 56 |
|
52 | | - |
53 | | - @OnClick(R.id.btn_start_operation) |
54 | | - public void onButtonTapped() { |
55 | | - Timber.d("--------- GOT A TAP"); |
56 | | - _tapCount += 1; |
57 | | - _log("GOT A TAP"); |
58 | | - _bufferedObservable.subscribeOn(Schedulers.io()) |
59 | | - .observeOn(AndroidSchedulers.mainThread()) |
60 | | - .subscribe(_observer); |
| 57 | + @Override |
| 58 | + public void onPause() { |
| 59 | + super.onPause(); |
| 60 | + _subscription.unsubscribe(); |
61 | 61 | } |
62 | 62 |
|
63 | 63 | // ----------------------------------------------------------------------------------- |
64 | 64 | // Main Rx entities |
65 | 65 |
|
66 | 66 | private Observable<List<Integer>> _getBufferedObservable() { |
| 67 | + |
67 | 68 | return Observable.create(new Observable.OnSubscribe<Integer>() { |
68 | 69 |
|
69 | 70 |
|
70 | 71 | @Override |
71 | 72 | public void call(Subscriber<? super Integer> subscriber) { |
72 | | - subscriber.onNext(1); |
73 | | - } |
| 73 | + _tapBtn.setOnClickListener(new View.OnClickListener() { |
74 | 74 |
|
75 | | - }).buffer(2, TimeUnit.SECONDS); |
| 75 | + |
| 76 | + @Override |
| 77 | + public void onClick(View v) { |
| 78 | + Timber.d("--------- GOT A TAP"); |
| 79 | + _tapCount += 1; |
| 80 | + _log("GOT A TAP"); |
| 81 | + } |
| 82 | + }); |
| 83 | + } |
| 84 | + }) |
| 85 | + .buffer(2, TimeUnit.SECONDS) |
| 86 | + .subscribeOn(Schedulers.io()) |
| 87 | + .observeOn(AndroidSchedulers.mainThread()); |
76 | 88 | } |
77 | 89 |
|
78 | 90 | private Observer<List<Integer>> _getObserver() { |
@@ -81,8 +93,10 @@ private Observer<List<Integer>> _getObserver() { |
81 | 93 |
|
82 | 94 | @Override |
83 | 95 | public void onCompleted() { |
84 | | - _log(String.format("%d taps", _tapCount)); |
85 | | - _tapCount = 0; |
| 96 | + if (_tapCount > 0) { |
| 97 | + _log(String.format("%d taps", _tapCount)); |
| 98 | + _tapCount = 0; |
| 99 | + } |
86 | 100 | } |
87 | 101 |
|
88 | 102 | @Override |
@@ -94,22 +108,27 @@ public void onError(Throwable e) { |
94 | 108 | @Override |
95 | 109 | public void onNext(List<Integer> integers) { |
96 | 110 | Timber.d("--------- onNext"); |
97 | | - |
98 | 111 | if (integers.size() > 0) { |
99 | 112 | for (int i : integers) { |
100 | 113 | _tapCount += i; |
101 | 114 | } |
102 | | - onCompleted(); |
103 | 115 | } else { |
104 | 116 | Timber.d("--------- No taps received "); |
105 | 117 | } |
| 118 | + onCompleted(); |
106 | 119 | } |
107 | 120 | }; |
108 | 121 | } |
109 | 122 |
|
110 | 123 | // ----------------------------------------------------------------------------------- |
111 | 124 | // Method that help wiring up the example (irrelevant to RxJava) |
112 | 125 |
|
| 126 | + @Override |
| 127 | + public void onActivityCreated(@Nullable Bundle savedInstanceState) { |
| 128 | + super.onActivityCreated(savedInstanceState); |
| 129 | + _setupLogger(); |
| 130 | + } |
| 131 | + |
113 | 132 | @Override |
114 | 133 | public View onCreateView(LayoutInflater inflater, |
115 | 134 | @Nullable ViewGroup container, |
|