Skip to content

Commit b99b513

Browse files
author
Kaushik Gopal
committed
1. use ViewObservable for the clicks vs attaching a listener on the button
2. remove _tapCount as it's unnecessary since buffer does the accumulation for us already 3. add comment to indicate there's a much better way to do this as demonstrated in Event Bus 3 demo. Thanks to @vanniktech for kickstarting these changes https://github.com/kaushikgopal/Android-RxJava/pull/14/files
1 parent 96ef263 commit b99b513

File tree

1 file changed

+54
-74
lines changed

1 file changed

+54
-74
lines changed
Lines changed: 54 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,41 @@
11
package com.morihacky.android.rxjava;
22

3-
import android.content.Context;
43
import android.os.Bundle;
54
import android.os.Handler;
65
import android.os.Looper;
76
import android.support.annotation.Nullable;
87
import android.view.LayoutInflater;
98
import android.view.View;
109
import android.view.ViewGroup;
11-
import android.widget.ArrayAdapter;
1210
import android.widget.Button;
1311
import android.widget.ListView;
1412
import butterknife.ButterKnife;
1513
import butterknife.InjectView;
16-
import com.morihacky.android.rxjava.R;
14+
import com.morihacky.android.rxjava.wiring.LogAdapter;
1715
import java.util.ArrayList;
1816
import java.util.List;
1917
import java.util.concurrent.TimeUnit;
20-
import rx.Observable;
2118
import rx.Observer;
22-
import rx.Subscriber;
2319
import rx.Subscription;
2420
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;
2624
import timber.log.Timber;
2725

2826
/**
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.
3128
*
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
3439
*/
3540
public class BufferDemoFragment
3641
extends BaseFragment {
@@ -40,14 +45,13 @@ public class BufferDemoFragment
4045

4146
private LogAdapter _adapter;
4247
private List<String> _logs;
43-
private int _tapCount = 0;
4448

4549
private Subscription _subscription;
4650

4751
@Override
48-
public void onResume() {
49-
super.onResume();
50-
_subscription = _getBufferedObservable().subscribe(_getObserver());
52+
public void onStart() {
53+
super.onStart();
54+
_subscription = _getBufferedSubscription();
5155
}
5256

5357
@Override
@@ -74,65 +78,49 @@ public View onCreateView(LayoutInflater inflater,
7478
// -----------------------------------------------------------------------------------
7579
// Main Rx entities
7680

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+
})
9491
.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+
});
129117
}
130118

131119
// -----------------------------------------------------------------------------------
132120
// Methods that help wiring up the example (irrelevant to RxJava)
133121

134122
private void _setupLogger() {
135-
_logs = new ArrayList<String>();
123+
_logs = new ArrayList<>();
136124
_adapter = new LogAdapter(getActivity(), new ArrayList<String>());
137125
_logsList.setAdapter(_adapter);
138126
}
@@ -161,12 +149,4 @@ public void run() {
161149
private boolean _isCurrentlyOnMainThread() {
162150
return Looper.myLooper() == Looper.getMainLooper();
163151
}
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-
}
172152
}

0 commit comments

Comments
 (0)