|
| 1 | +package com.rxjava2.android.samples.ui.operators; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.support.annotation.Nullable; |
| 5 | +import android.support.v7.app.AppCompatActivity; |
| 6 | +import android.util.Log; |
| 7 | +import android.view.View; |
| 8 | +import android.widget.Button; |
| 9 | +import android.widget.TextView; |
| 10 | + |
| 11 | +import com.rxjava2.android.samples.R; |
| 12 | +import com.rxjava2.android.samples.utils.AppConstant; |
| 13 | + |
| 14 | +import java.util.Random; |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | + |
| 17 | +import io.reactivex.Observable; |
| 18 | +import io.reactivex.ObservableSource; |
| 19 | +import io.reactivex.Observer; |
| 20 | +import io.reactivex.android.schedulers.AndroidSchedulers; |
| 21 | +import io.reactivex.disposables.Disposable; |
| 22 | +import io.reactivex.functions.Function; |
| 23 | +import io.reactivex.schedulers.Schedulers; |
| 24 | + |
| 25 | +/** |
| 26 | + * Created by thanhtuan on 26/04/18. |
| 27 | + */ |
| 28 | +public class SwitchMapExampleActivity extends AppCompatActivity { |
| 29 | + |
| 30 | + private static final String TAG = SwitchMapExampleActivity.class.getSimpleName(); |
| 31 | + Button btn; |
| 32 | + TextView textView; |
| 33 | + |
| 34 | + @Override |
| 35 | + protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 36 | + super.onCreate(savedInstanceState); |
| 37 | + setContentView(R.layout.activity_example); |
| 38 | + |
| 39 | + btn = (Button) findViewById(R.id.btn); |
| 40 | + textView = (TextView) findViewById(R.id.textView); |
| 41 | + |
| 42 | + btn.setOnClickListener(new View.OnClickListener() { |
| 43 | + @Override |
| 44 | + public void onClick(View view) { |
| 45 | + doSomeWork(); |
| 46 | + } |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + /* whenever a new item is emitted by the source Observable, it will unsubscribe to and stop |
| 51 | + * mirroring the Observable that was generated from the previously-emitted item, |
| 52 | + * and begin only mirroring the current one. |
| 53 | + * |
| 54 | + * Result: 5x |
| 55 | + */ |
| 56 | + private void doSomeWork() { |
| 57 | + getObservable() |
| 58 | + .switchMap(new Function<Integer, ObservableSource<String>>() { |
| 59 | + @Override |
| 60 | + public ObservableSource<String> apply(Integer integer) { |
| 61 | + int delay = new Random().nextInt(2); |
| 62 | + |
| 63 | + return Observable.just(integer.toString() + "x") |
| 64 | + .delay(delay, TimeUnit.SECONDS, Schedulers.io()); |
| 65 | + } |
| 66 | + }) |
| 67 | + .subscribeOn(Schedulers.io()) |
| 68 | + .observeOn(AndroidSchedulers.mainThread()) |
| 69 | + .subscribe(getObserver()); |
| 70 | + } |
| 71 | + |
| 72 | + private Observable<Integer> getObservable() { |
| 73 | + return Observable.just(1, 2, 3, 4, 5); |
| 74 | + } |
| 75 | + |
| 76 | + private Observer<String> getObserver() { |
| 77 | + return new Observer<String>() { |
| 78 | + @Override |
| 79 | + public void onSubscribe(Disposable d) { |
| 80 | + Log.d(TAG, " onSubscribe : " + d.isDisposed()); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void onNext(String value) { |
| 85 | + textView.append(" onNext : value : " + value); |
| 86 | + textView.append(AppConstant.LINE_SEPARATOR); |
| 87 | + Log.d(TAG, " onNext value : " + value); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void onError(Throwable e) { |
| 92 | + textView.append(" onError : " + e.getMessage()); |
| 93 | + textView.append(AppConstant.LINE_SEPARATOR); |
| 94 | + Log.d(TAG, " onError : " + e.getMessage()); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void onComplete() { |
| 99 | + textView.append(" onComplete"); |
| 100 | + textView.append(AppConstant.LINE_SEPARATOR); |
| 101 | + Log.d(TAG, " onComplete"); |
| 102 | + } |
| 103 | + }; |
| 104 | + } |
| 105 | +} |
0 commit comments