Skip to content

Commit 7533fbc

Browse files
committed
Add second example for RxJava Backpressure.
1 parent b307371 commit 7533fbc

File tree

9 files changed

+308
-157
lines changed

9 files changed

+308
-157
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
android:label="@string/app_sample_observable_composition" />
2727

2828
<activity
29-
android:name=".activity.ActivityBackpressureSamples"
29+
android:name=".activity.backpressure.ActivityBackpressureSamples"
3030
android:label="@string/app_sample_backpressure" />
3131

3232
<activity

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

Lines changed: 0 additions & 150 deletions
This file was deleted.
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/**
2+
* Copyright (C) 2016 android10.org Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.fernandocejas.android10.rx.sample.activity.backpressure;
17+
18+
import android.content.Context;
19+
import android.content.Intent;
20+
import android.os.Bundle;
21+
import android.view.Window;
22+
import android.widget.TextView;
23+
import butterknife.Bind;
24+
import butterknife.ButterKnife;
25+
import butterknife.OnClick;
26+
import com.fernandocejas.android10.rx.sample.R;
27+
import com.fernandocejas.android10.rx.sample.activity.BaseActivity;
28+
import com.fernandocejas.android10.rx.sample.data.DataManager;
29+
import com.fernandocejas.android10.rx.sample.data.NumberGenerator;
30+
import com.fernandocejas.android10.rx.sample.data.StringGenerator;
31+
import com.fernandocejas.android10.rx.sample.executor.JobExecutor;
32+
import com.fernandocejas.arrow.strings.Strings;
33+
import rx.Subscription;
34+
import rx.android.schedulers.AndroidSchedulers;
35+
import rx.schedulers.Schedulers;
36+
import rx.subscriptions.Subscriptions;
37+
38+
public class ActivityBackpressureSamples extends BaseActivity implements
39+
BackpressureSubscriber.BackPressureResultListener {
40+
41+
@Bind(R.id.tv_sampleDescription) TextView tv_sampleDescription;
42+
@Bind(R.id.tv_itemsRequested) TextView tv_itemsRequested;
43+
@Bind(R.id.tv_itemsProcessed) TextView tv_itemsProcessed;
44+
@Bind(R.id.tv_itemsEmitted) TextView tv_itemsEmitted;
45+
@Bind(R.id.tv_result) TextView tv_result;
46+
47+
private Subscription subscription;
48+
private DataManager dataManager;
49+
50+
public static Intent getCallingIntent(Context context) {
51+
return new Intent(context, ActivityBackpressureSamples.class);
52+
}
53+
54+
@Override
55+
protected void onCreate(Bundle savedInstanceState) {
56+
super.onCreate(savedInstanceState);
57+
initialize();
58+
}
59+
60+
@Override
61+
protected void onDestroy() {
62+
subscription.unsubscribe();
63+
super.onDestroy();
64+
}
65+
66+
private void initialize() {
67+
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
68+
setContentView(R.layout.activity_samples_backpressure);
69+
70+
ButterKnife.bind(this);
71+
clearUpUI();
72+
73+
subscription = Subscriptions.unsubscribed();
74+
dataManager = new DataManager(new StringGenerator(),
75+
new NumberGenerator(), JobExecutor.getInstance());
76+
}
77+
78+
private void clearUpUI() {
79+
tv_sampleDescription.setText(Strings.EMPTY);
80+
tv_itemsRequested.setText(Strings.EMPTY);
81+
tv_itemsProcessed.setText(Strings.EMPTY);
82+
tv_itemsEmitted.setText(Strings.EMPTY);
83+
tv_result.setText(Strings.EMPTY);
84+
}
85+
86+
private void toggleProgressBar(boolean visible) {
87+
setProgressBarIndeterminateVisibility(visible);
88+
}
89+
90+
@OnClick(R.id.btn_backpressureException) void onBackpressureExceptionClick() {
91+
dataManager.milliseconds(1000)
92+
.observeOn(AndroidSchedulers.mainThread())
93+
.subscribeOn(Schedulers.computation())
94+
.subscribe(new BackpressureSubscriber(this, getString(R.string.btn_text_backpressure_exception)));
95+
}
96+
97+
@OnClick(R.id.btn_backpressureOperator) void onAnotherExampleClick() {
98+
dataManager.seconds(5)
99+
.observeOn(AndroidSchedulers.mainThread())
100+
.subscribeOn(Schedulers.computation())
101+
.subscribe(new BackpressureSubscriber(this, "Another sample", 10));
102+
}
103+
104+
@Override
105+
public void onOperationStart(String name, long itemsRequested) {
106+
clearUpUI();
107+
toggleProgressBar(true);
108+
tv_sampleDescription.setText(name);
109+
tv_itemsRequested.setText(String.valueOf(itemsRequested));
110+
}
111+
112+
@Override public void onOperationProgress(long itemsProcessedSoFar) {
113+
tv_itemsProcessed.setText(String.valueOf(itemsProcessedSoFar));
114+
}
115+
116+
@Override
117+
public void onOperationResult(long itemsEmitted, OperationResult operation) {
118+
tv_itemsEmitted.setText(String.valueOf(itemsEmitted));
119+
String resultMessage = Strings.EMPTY;
120+
switch (operation.result()) {
121+
case SUCCESS:
122+
resultMessage = getString(R.string.string_success);
123+
break;
124+
case FAILURE:
125+
if (operation.throwable().isPresent()) {
126+
resultMessage = String.format(getString(R.string.string_failure),
127+
operation.throwable().get().toString());
128+
} else {
129+
resultMessage = getString(R.string.string_failure);
130+
}
131+
break;
132+
}
133+
tv_result.setText(resultMessage);
134+
toggleProgressBar(false);
135+
}
136+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* Copyright (C) 2016 android10.org Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.fernandocejas.android10.rx.sample.activity.backpressure;
17+
18+
import com.fernandocejas.frodo.annotation.RxLogSubscriber;
19+
import java.lang.ref.WeakReference;
20+
import rx.Subscriber;
21+
import rx.exceptions.MissingBackpressureException;
22+
23+
@RxLogSubscriber
24+
class BackpressureSubscriber extends Subscriber<Long> {
25+
26+
interface BackPressureResultListener {
27+
void onOperationStart(String name, long itemsRequested);
28+
void onOperationProgress(long itemsProcessedSoFar);
29+
void onOperationResult(long itemsEmitted, OperationResult operation);
30+
}
31+
32+
private final WeakReference<BackPressureResultListener> backPressureListener;
33+
private final String name;
34+
private final long itemsRequested;
35+
36+
private long itemsEmitted = 0;
37+
38+
BackpressureSubscriber(BackPressureResultListener listener, String name) {
39+
this(listener, name, Long.MAX_VALUE);
40+
}
41+
42+
BackpressureSubscriber(BackPressureResultListener listener, String name, long requestedItems) {
43+
this.backPressureListener = new WeakReference<>(listener);
44+
this.name = name;
45+
this.itemsRequested = requestedItems;
46+
}
47+
48+
@Override public void onStart() {
49+
request(itemsRequested);
50+
sendStartData();
51+
}
52+
53+
@Override public void onNext(Long item) {
54+
itemsEmitted++;
55+
sendProgressData(itemsEmitted);
56+
request(itemsRequested);
57+
}
58+
59+
@Override public void onCompleted() {
60+
sendResultData(OperationResult.success());
61+
}
62+
63+
@Override public void onError(Throwable throwable) {
64+
if (throwable instanceof MissingBackpressureException) {
65+
sendResultData(OperationResult.failure(throwable));
66+
}
67+
}
68+
69+
private void sendStartData() {
70+
if (backPressureListener.get() != null) {
71+
backPressureListener.get().onOperationStart(name, itemsRequested);
72+
}
73+
}
74+
75+
private void sendProgressData(long itemsProcessedSoFar) {
76+
if (backPressureListener.get() != null) {
77+
backPressureListener.get().onOperationProgress(itemsProcessedSoFar);
78+
}
79+
}
80+
81+
private void sendResultData(OperationResult operation) {
82+
if (backPressureListener.get() != null) {
83+
backPressureListener.get().onOperationResult(itemsEmitted, operation);
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)