|
1 | 1 | package com.morihacky.android.rxjava; |
2 | 2 |
|
3 | | -import android.content.Context; |
4 | 3 | import android.os.Bundle; |
5 | 4 | import android.os.Handler; |
6 | 5 | import android.os.Looper; |
7 | 6 | import android.support.annotation.Nullable; |
8 | 7 | import android.view.LayoutInflater; |
9 | 8 | import android.view.View; |
10 | 9 | import android.view.ViewGroup; |
11 | | -import android.widget.ArrayAdapter; |
12 | 10 | import android.widget.Button; |
13 | 11 | import android.widget.ListView; |
14 | 12 | import butterknife.ButterKnife; |
15 | 13 | import butterknife.InjectView; |
16 | | -import com.morihacky.android.rxjava.R; |
| 14 | +import com.morihacky.android.rxjava.wiring.LogAdapter; |
17 | 15 | import java.util.ArrayList; |
18 | 16 | import java.util.List; |
19 | 17 | import java.util.concurrent.TimeUnit; |
20 | | -import rx.Observable; |
21 | 18 | import rx.Observer; |
22 | | -import rx.Subscriber; |
23 | 19 | import rx.Subscription; |
24 | 20 | import rx.android.schedulers.AndroidSchedulers; |
25 | | -import rx.schedulers.Schedulers; |
| 21 | +import rx.android.view.OnClickEvent; |
| 22 | +import rx.android.view.ViewObservable; |
| 23 | +import rx.functions.Func1; |
26 | 24 | import timber.log.Timber; |
27 | 25 |
|
28 | 26 | /** |
29 | | - * credit to @tomrozb for this implementation: |
30 | | - * http://stackoverflow.com/questions/24922610/incorrect-understanding-of-buffer-in-rxjava |
| 27 | + * This is a demonstration of the `buffer` Observable. |
31 | 28 | * |
32 | | - * An alternate mechanism of achieving the same result would be to use a {@link rx.subjects.PublishSubject} |
33 | | - * as demonstrated in the case of {@link com.morihacky.android.rxjava.SubjectDebounceSearchEmitterFragment} |
| 29 | + * The buffer observable allows taps to be collected only within a time span. So taps outside the |
| 30 | + * 2s limit imposed by buffer will get accumulated in the next log statement. |
| 31 | + * |
| 32 | + * If you're looking for a more foolproof solution that accumulates "continuous" taps vs |
| 33 | + * a more dumb solution as show below (i.e. number of taps within a timespan) |
| 34 | + * look at {@link com.morihacky.android.rxjava.rxbus.RxBusDemo_Bottom3Fragment} where a combo |
| 35 | + * of `publish` and `buffer` is used. |
| 36 | + * |
| 37 | + * Also http://nerds.weddingpartyapp.com/tech/2015/01/05/debouncedbuffer-used-in-rxbus-example/ |
| 38 | + * if you're looking for words instead of code |
34 | 39 | */ |
35 | 40 | public class BufferDemoFragment |
36 | 41 | extends BaseFragment { |
@@ -40,14 +45,13 @@ public class BufferDemoFragment |
40 | 45 |
|
41 | 46 | private LogAdapter _adapter; |
42 | 47 | private List<String> _logs; |
43 | | - private int _tapCount = 0; |
44 | 48 |
|
45 | 49 | private Subscription _subscription; |
46 | 50 |
|
47 | 51 | @Override |
48 | | - public void onResume() { |
49 | | - super.onResume(); |
50 | | - _subscription = _getBufferedObservable().subscribe(_getObserver()); |
| 52 | + public void onStart() { |
| 53 | + super.onStart(); |
| 54 | + _subscription = _getBufferedSubscription(); |
51 | 55 | } |
52 | 56 |
|
53 | 57 | @Override |
@@ -74,65 +78,49 @@ public View onCreateView(LayoutInflater inflater, |
74 | 78 | // ----------------------------------------------------------------------------------- |
75 | 79 | // Main Rx entities |
76 | 80 |
|
77 | | - private Observable<List<Integer>> _getBufferedObservable() { |
78 | | - |
79 | | - return Observable.create(new Observable.OnSubscribe<Integer>() { |
80 | | - |
81 | | - @Override |
82 | | - public void call(Subscriber<? super Integer> subscriber) { |
83 | | - _tapBtn.setOnClickListener(new View.OnClickListener() { |
84 | | - |
85 | | - @Override |
86 | | - public void onClick(View v) { |
87 | | - Timber.d("--------- GOT A TAP"); |
88 | | - _tapCount += 1; |
89 | | - _log("GOT A TAP"); |
90 | | - } |
91 | | - }); |
92 | | - } |
93 | | - }) |
| 81 | + private Subscription _getBufferedSubscription() { |
| 82 | + return ViewObservable.clicks(_tapBtn) |
| 83 | + .map(new Func1<OnClickEvent, Integer>() { |
| 84 | + @Override |
| 85 | + public Integer call(OnClickEvent onClickEvent) { |
| 86 | + Timber.d("--------- GOT A TAP"); |
| 87 | + _log("GOT A TAP"); |
| 88 | + return 1; |
| 89 | + } |
| 90 | + }) |
94 | 91 | .buffer(2, TimeUnit.SECONDS) |
95 | | - .subscribeOn(Schedulers.io()) |
96 | | - .observeOn(AndroidSchedulers.mainThread()); |
97 | | - } |
98 | | - |
99 | | - private Observer<List<Integer>> _getObserver() { |
100 | | - return new Observer<List<Integer>>() { |
101 | | - |
102 | | - @Override |
103 | | - public void onCompleted() { |
104 | | - if (_tapCount > 0) { |
105 | | - _log(String.format("%d taps", _tapCount)); |
106 | | - _tapCount = 0; |
107 | | - } |
108 | | - } |
109 | | - |
110 | | - @Override |
111 | | - public void onError(Throwable e) { |
112 | | - Timber.e(e, "--------- Woops on error!"); |
113 | | - _log(String.format("Dang error. check your logs")); |
114 | | - } |
115 | | - |
116 | | - @Override |
117 | | - public void onNext(List<Integer> integers) { |
118 | | - Timber.d("--------- onNext"); |
119 | | - if (integers.size() > 0) { |
120 | | - for (int i : integers) { |
121 | | - _tapCount += i; |
122 | | - } |
123 | | - } else { |
124 | | - Timber.d("--------- No taps received "); |
125 | | - } |
126 | | - onCompleted(); |
127 | | - } |
128 | | - }; |
| 92 | + .observeOn(AndroidSchedulers.mainThread()) |
| 93 | + .subscribe(new Observer<List<Integer>>() { |
| 94 | + |
| 95 | + @Override |
| 96 | + public void onCompleted() { |
| 97 | + // fyi: you'll never reach here |
| 98 | + Timber.d("----- onCompleted"); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void onError(Throwable e) { |
| 103 | + Timber.e(e, "--------- Woops on error!"); |
| 104 | + _log("Dang error! check your logs"); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void onNext(List<Integer> integers) { |
| 109 | + Timber.d("--------- onNext"); |
| 110 | + if (integers.size() > 0) { |
| 111 | + _log(String.format("%d taps", integers.size())); |
| 112 | + } else { |
| 113 | + Timber.d("--------- No taps received "); |
| 114 | + } |
| 115 | + } |
| 116 | + }); |
129 | 117 | } |
130 | 118 |
|
131 | 119 | // ----------------------------------------------------------------------------------- |
132 | 120 | // Methods that help wiring up the example (irrelevant to RxJava) |
133 | 121 |
|
134 | 122 | private void _setupLogger() { |
135 | | - _logs = new ArrayList<String>(); |
| 123 | + _logs = new ArrayList<>(); |
136 | 124 | _adapter = new LogAdapter(getActivity(), new ArrayList<String>()); |
137 | 125 | _logsList.setAdapter(_adapter); |
138 | 126 | } |
@@ -161,12 +149,4 @@ public void run() { |
161 | 149 | private boolean _isCurrentlyOnMainThread() { |
162 | 150 | return Looper.myLooper() == Looper.getMainLooper(); |
163 | 151 | } |
164 | | - |
165 | | - private class LogAdapter |
166 | | - extends ArrayAdapter<String> { |
167 | | - |
168 | | - public LogAdapter(Context context, List<String> logs) { |
169 | | - super(context, R.layout.item_log, R.id.item_log, logs); |
170 | | - } |
171 | | - } |
172 | 152 | } |