|
| 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 | +} |
0 commit comments