|
16 | 16 |
|
17 | 17 | import java.util.ArrayList; |
18 | 18 | import java.util.List; |
| 19 | +import java.util.Locale; |
19 | 20 | import java.util.concurrent.TimeUnit; |
20 | 21 |
|
21 | 22 | import butterknife.Bind; |
22 | 23 | import butterknife.ButterKnife; |
23 | 24 | import butterknife.OnClick; |
24 | 25 | import rx.Observable; |
25 | | -import rx.Scheduler; |
26 | | -import rx.Subscriber; |
27 | | -import rx.Subscription; |
28 | 26 | import rx.functions.Action0; |
29 | 27 | import rx.functions.Action1; |
30 | 28 | import rx.functions.Func1; |
31 | | -import rx.schedulers.Schedulers; |
32 | 29 | import rx.subscriptions.CompositeSubscription; |
33 | 30 | import timber.log.Timber; |
34 | 31 |
|
35 | 32 | public class PollingFragment |
36 | 33 | extends BaseFragment { |
37 | 34 |
|
38 | | - public static final int INITIAL_DELAY = 0; |
39 | | - public static final int POLLING_INTERVAL = 500; |
| 35 | + private static final int INITIAL_DELAY = 0; |
| 36 | + private static final int POLLING_INTERVAL = 1000; |
| 37 | + |
40 | 38 | @Bind(R.id.list_threading_log) ListView _logsList; |
41 | 39 |
|
42 | 40 | private LogAdapter _adapter; |
43 | 41 | private List<String> _logs; |
44 | | - private CompositeSubscription _subscriptions; |
| 42 | + private CompositeSubscription _subscriptions = new CompositeSubscription(); |
45 | 43 | private int _counter = 0; |
46 | | - private Scheduler.Worker _worker; |
47 | 44 |
|
48 | 45 | @Override |
49 | 46 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { |
50 | 47 | super.onActivityCreated(savedInstanceState); |
51 | | - _subscriptions = new CompositeSubscription(); |
52 | | - _worker = Schedulers.newThread() |
53 | | - .createWorker(); |
54 | 48 | _setupLogger(); |
55 | 49 | } |
56 | 50 |
|
@@ -72,8 +66,6 @@ public void onDestroy() { |
72 | 66 |
|
73 | 67 | @OnClick(R.id.btn_start_simple_polling) |
74 | 68 | public void onStartSimplePollingClicked() { |
75 | | - _setupLogger(); |
76 | | - |
77 | 69 | _subscriptions.add(// |
78 | 70 | Observable.interval(INITIAL_DELAY, POLLING_INTERVAL, TimeUnit.MILLISECONDS) |
79 | 71 | .map(new Func1<Long, String>() { |
@@ -100,44 +92,89 @@ public void call(String s) { |
100 | 92 | @OnClick(R.id.btn_start_increasingly_delayed_polling) |
101 | 93 | public void onStartIncreasinglyDelayedPolling() { |
102 | 94 | _setupLogger(); |
103 | | - _log(String.format("Increasingly delayed String polling - %s", _counter)); |
104 | | - continueIncreasinglyDelayedPolling(1000, 10); |
| 95 | + |
| 96 | + final int pollingInterval = POLLING_INTERVAL; |
| 97 | + final int repeatLimit = 5; |
| 98 | + |
| 99 | + _log(String.format(Locale.US, "Start increasingly delayed polling now time: [xx:%02d]", |
| 100 | + _getSecondHand())); |
| 101 | + |
| 102 | + _subscriptions.add(// |
| 103 | + Observable.just(1) |
| 104 | + .repeatWhen(new RepeatWithDelay(repeatLimit, pollingInterval)) |
| 105 | + .subscribe(new Action1<Object>() { |
| 106 | + @Override |
| 107 | + public void call(Object o) { |
| 108 | + _log(String.format(Locale.US, "Executing polled task now time : [xx:%02d]", |
| 109 | + _getSecondHand())); |
| 110 | + } |
| 111 | + }, new Action1<Throwable>() { |
| 112 | + @Override |
| 113 | + public void call(Throwable e) { |
| 114 | + Timber.d(e, "arrrr. Error"); |
| 115 | + } |
| 116 | + }) |
| 117 | + ); |
105 | 118 | } |
106 | 119 |
|
107 | | - private void continueIncreasinglyDelayedPolling(final int delay, final int limit) { |
108 | | - _subscriptions = _unsubscribeAndGetNewCompositeSub(_subscriptions); |
109 | | - Observable.create( |
110 | | - new Observable.OnSubscribe<String>() { |
111 | | - @Override |
112 | | - public void call(final Subscriber<? super String> subscriber) { |
113 | | - |
114 | | - Subscription subscription = _worker.schedule(new Action0() { |
115 | | - @Override |
116 | | - public void call() { |
117 | | - subscriber.onNext(_doNetworkCallAndGetStringResult()); |
118 | | - } |
119 | | - }, delay, TimeUnit.MILLISECONDS); |
120 | | - subscriber.add(subscription); |
121 | | - } |
122 | | - }) |
123 | | - .take(limit) |
124 | | - .subscribe(new Action1<String>() { |
125 | | - @Override |
126 | | - public void call(String s) { |
127 | | - continueIncreasinglyDelayedPolling(delay + 1000, limit - 1); |
128 | | - Timber.d("delay of %d", delay); |
129 | | - _log(String.format("String polling - %s", s)); |
| 120 | + // ----------------------------------------------------------------------------------- |
| 121 | + |
| 122 | + // CAUTION: |
| 123 | + // -------------------------------------- |
| 124 | + // THIS notificationHandler class HAS NO BUSINESS BEING non-static |
| 125 | + // I ONLY did this cause i wanted access to the `_log` method from inside here |
| 126 | + // for the purpose of demonstration. In the real world, make it static and LET IT BE!! |
| 127 | + |
| 128 | + // It's 12am in the morning and i feel lazy dammit !!! |
| 129 | + |
| 130 | + //public static class RepeatWithDelay |
| 131 | + public class RepeatWithDelay |
| 132 | + implements Func1<Observable<? extends Void>, Observable<?>> { |
| 133 | + |
| 134 | + private final int _repeatLimit; |
| 135 | + private final int _pollingInterval; |
| 136 | + private int _repeatCount = 1; |
| 137 | + |
| 138 | + RepeatWithDelay(int repeatLimit, int pollingInterval) { |
| 139 | + _pollingInterval = pollingInterval; |
| 140 | + _repeatLimit = repeatLimit; |
| 141 | + } |
| 142 | + |
| 143 | + // this is a notificationhandler, all we care about is |
| 144 | + // the emission "type" not emission "content" |
| 145 | + // only onNext triggers a re-subscription |
| 146 | + |
| 147 | + @Override |
| 148 | + public Observable<?> call(Observable<? extends Void> inputObservable) { |
| 149 | + |
| 150 | + // it is critical to use inputObservable in the chain for the result |
| 151 | + // ignoring it and doing your own thing will break the sequence |
| 152 | + |
| 153 | + return inputObservable.flatMap(new Func1<Void, Observable<?>>() { |
| 154 | + @Override |
| 155 | + public Observable<?> call(Void blah) { |
| 156 | + |
| 157 | + |
| 158 | + if (_repeatCount >= _repeatLimit) { |
| 159 | + // terminate the sequence cause we reached the limit |
| 160 | + _log("Completing sequence"); |
| 161 | + return Observable.empty(); |
130 | 162 | } |
131 | | - }); |
| 163 | + |
| 164 | + // since we don't get an input |
| 165 | + // we store state in this handler to tell us the point of time we're firing |
| 166 | + _repeatCount++; |
| 167 | + |
| 168 | + return Observable.timer(_repeatCount * _pollingInterval, |
| 169 | + TimeUnit.MILLISECONDS); |
| 170 | + } |
| 171 | + }); |
| 172 | + } |
132 | 173 | } |
| 174 | + |
133 | 175 | // ----------------------------------------------------------------------------------- |
134 | 176 | // Method that help wiring up the example (irrelevant to RxJava) |
135 | 177 |
|
136 | | - private static CompositeSubscription _unsubscribeAndGetNewCompositeSub(CompositeSubscription subscription){ |
137 | | - RxUtils.unsubscribeIfNotNull(subscription); |
138 | | - return RxUtils.getNewCompositeSubIfUnsubscribed(subscription); |
139 | | - } |
140 | | - |
141 | 178 | private String _doNetworkCallAndGetStringResult() { |
142 | 179 |
|
143 | 180 | try { |
@@ -150,6 +187,12 @@ private String _doNetworkCallAndGetStringResult() { |
150 | 187 | return String.valueOf(_counter); |
151 | 188 | } |
152 | 189 |
|
| 190 | + private int _getSecondHand() { |
| 191 | + long millis = System.currentTimeMillis(); |
| 192 | + return (int) (TimeUnit.MILLISECONDS.toSeconds(millis) - |
| 193 | + TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); |
| 194 | + } |
| 195 | + |
153 | 196 | private void _log(String logMsg) { |
154 | 197 | if (_isCurrentlyOnMainThread()) { |
155 | 198 | _logs.add(0, logMsg + " (main thread) "); |
@@ -174,7 +217,6 @@ private void _setupLogger() { |
174 | 217 | _logs = new ArrayList<>(); |
175 | 218 | _adapter = new LogAdapter(getActivity(), new ArrayList<String>()); |
176 | 219 | _logsList.setAdapter(_adapter); |
177 | | - _subscriptions = _unsubscribeAndGetNewCompositeSub(_subscriptions); |
178 | 220 | _counter = 0; |
179 | 221 | } |
180 | 222 |
|
@@ -183,7 +225,7 @@ private boolean _isCurrentlyOnMainThread() { |
183 | 225 | } |
184 | 226 |
|
185 | 227 | private class LogAdapter |
186 | | - extends ArrayAdapter<String> { |
| 228 | + extends ArrayAdapter<String> { |
187 | 229 |
|
188 | 230 | public LogAdapter(Context context, List<String> logs) { |
189 | 231 | super(context, R.layout.item_log, R.id.item_log, logs); |
|