Skip to content

Commit eb090e7

Browse files
committed
Back pressure example: ThrottleLast. Minor refactor.
1 parent bc077a2 commit eb090e7

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

app/src/main/java/com/fernandocejas/android10/rx/sample/activity/backpressure/ActivityBackpressureSamples.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import android.content.Context;
1919
import android.content.Intent;
20+
import android.content.res.ColorStateList;
21+
import android.graphics.Color;
2022
import android.os.Bundle;
2123
import android.view.Window;
2224
import android.widget.TextView;
@@ -30,6 +32,7 @@
3032
import com.fernandocejas.android10.rx.sample.data.StringGenerator;
3133
import com.fernandocejas.android10.rx.sample.executor.JobExecutor;
3234
import com.fernandocejas.arrow.strings.Strings;
35+
import java.util.concurrent.TimeUnit;
3336
import rx.Subscription;
3437
import rx.android.schedulers.AndroidSchedulers;
3538
import rx.schedulers.Schedulers;
@@ -115,7 +118,15 @@ private void toggleProgressBar(boolean visible) {
115118
.toList()
116119
.observeOn(AndroidSchedulers.mainThread())
117120
.subscribeOn(Schedulers.computation())
118-
.subscribe(new BackpressureSubscriber<>(this, getString(R.string.btn_text_backpressure_tolist), 1000));
121+
.subscribe(new BackpressureSubscriber<>(this, getString(R.string.btn_text_backpressure_toList), 1000));
122+
}
123+
124+
@OnClick(R.id.btn_backpressureThrottleLast) void onBackpressureThrottleLast() {
125+
dataManager.milliseconds(10000)
126+
.throttleLast(10, TimeUnit.MILLISECONDS)
127+
.observeOn(AndroidSchedulers.mainThread())
128+
.subscribeOn(Schedulers.computation())
129+
.subscribe(new BackpressureSubscriber<>(this, getString(R.string.btn_text_backpressure_throttleLast), 10000));
119130
}
120131

121132
@Override
@@ -133,21 +144,25 @@ public void onOperationStart(String name, long itemsRequested) {
133144
@Override
134145
public void onOperationResult(long itemsEmitted, OperationResult operation) {
135146
tv_itemsEmitted.setText(String.valueOf(itemsEmitted));
136-
String resultMessage = Strings.EMPTY;
137147
switch (operation.result()) {
138148
case SUCCESS:
139-
resultMessage = getString(R.string.string_success);
149+
tv_result.setText(getString(R.string.string_success));
150+
tv_result.setTextColor(ColorStateList.valueOf(Color.BLUE));
140151
break;
141152
case FAILURE:
153+
String failureMessage;
142154
if (operation.throwable().isPresent()) {
143-
resultMessage = String.format(getString(R.string.string_failure),
155+
failureMessage = String.format(getString(R.string.string_failure),
144156
operation.throwable().get().toString());
145157
} else {
146-
resultMessage = getString(R.string.string_failure);
158+
failureMessage = getString(R.string.string_failure);
147159
}
160+
tv_result.setText(failureMessage);
161+
tv_result.setTextColor(ColorStateList.valueOf(Color.RED));
148162
break;
163+
default:
164+
tv_result.setText(Strings.EMPTY);
149165
}
150-
tv_result.setText(resultMessage);
151166
toggleProgressBar(false);
152167
}
153168
}

app/src/main/res/layout/activity_samples_backpressure.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,14 @@
3939
android:id="@+id/btn_backpressureToList"
4040
android:layout_width="match_parent"
4141
android:layout_height="wrap_content"
42-
android:text="@string/btn_text_backpressure_tolist"
42+
android:text="@string/btn_text_backpressure_toList"
43+
/>
44+
45+
<Button
46+
android:id="@+id/btn_backpressureThrottleLast"
47+
android:layout_width="match_parent"
48+
android:layout_height="wrap_content"
49+
android:text="@string/btn_text_backpressure_throttleLast"
4350
/>
4451

4552
<LinearLayout

app/src/main/res/values/strings.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@
2626
<string name="tv_label_backpressure_items_processed">Items processed:</string>
2727
<string name="tv_label_backpressure_items_emitted">Items emitted:</string>
2828
<string name="tv_label_backpressure_result">Result:</string>
29-
<string name="btn_text_backpressure_exception">Backpressure exception</string>
30-
<string name="btn_text_backpressure_drop">Backpressure drop items</string>
31-
<string name="btn_text_backpressure_buffer">Backpressure buffer items</string>
32-
<string name="btn_text_backpressure_tolist">Backpressure toList</string>
29+
<string name="btn_text_backpressure_exception">Backpressure Exception</string>
30+
<string name="btn_text_backpressure_drop">Backpressure Drop Items</string>
31+
<string name="btn_text_backpressure_buffer">Backpressure Buffer Items</string>
32+
<string name="btn_text_backpressure_toList">Backpressure To List</string>
33+
<string name="btn_text_backpressure_throttleLast">Backpressure Throttle Last</string>
3334

3435
<string name="string_content_description">Non accessible view</string>
3536

0 commit comments

Comments
 (0)