|
1 | 1 | package com.rxjava2.android.samples.ui.operators; |
2 | 2 |
|
3 | | -import com.rxjava2.android.samples.ui.ExampleBaseActivity; |
| 3 | +import android.os.Bundle; |
| 4 | +import android.support.v7.app.AppCompatActivity; |
| 5 | +import android.util.Log; |
| 6 | +import android.view.View; |
| 7 | +import android.widget.Button; |
| 8 | +import android.widget.TextView; |
4 | 9 |
|
| 10 | +import com.rxjava2.android.samples.R; |
| 11 | +import com.rxjava2.android.samples.utils.AppConstant; |
| 12 | + |
| 13 | +import io.reactivex.Observer; |
| 14 | +import io.reactivex.disposables.Disposable; |
5 | 15 | import io.reactivex.subjects.AsyncSubject; |
6 | 16 |
|
7 | 17 | /** |
8 | 18 | * Created by amitshekhar on 17/12/16. |
9 | 19 | */ |
10 | 20 |
|
11 | | -public class AsyncSubjectExampleActivity extends ExampleBaseActivity { |
| 21 | +public class AsyncSubjectExampleActivity extends AppCompatActivity { |
| 22 | + |
| 23 | + private static final String TAG = AsyncSubjectExampleActivity.class.getSimpleName(); |
| 24 | + Button btn; |
| 25 | + TextView textView; |
| 26 | + |
| 27 | + @Override |
| 28 | + protected void onCreate(Bundle savedInstanceState) { |
| 29 | + super.onCreate(savedInstanceState); |
| 30 | + setContentView(R.layout.activity_example); |
| 31 | + btn = (Button) findViewById(R.id.btn); |
| 32 | + textView = (TextView) findViewById(R.id.textView); |
| 33 | + |
| 34 | + btn.setOnClickListener(new View.OnClickListener() { |
| 35 | + @Override |
| 36 | + public void onClick(View view) { |
| 37 | + doSomeWork(); |
| 38 | + } |
| 39 | + }); |
| 40 | + } |
12 | 41 |
|
13 | 42 | /* An AsyncSubject emits the last value (and only the last value) emitted by the source |
14 | 43 | * Observable, and only after that source Observable completes. (If the source Observable |
15 | 44 | * does not emit any values, the AsyncSubject also completes without emitting any values.) |
16 | 45 | */ |
17 | | - protected void doSomeWork() { |
| 46 | + private void doSomeWork() { |
| 47 | + |
18 | 48 | AsyncSubject<Integer> source = AsyncSubject.create(); |
19 | 49 |
|
20 | | - source.onNext(0); |
21 | | - source.subscribe(getObserver("First")); // it will emit only 4 and onComplete |
| 50 | + source.subscribe(getFirstObserver()); // it will emit only 4 and onComplete |
22 | 51 |
|
23 | 52 | source.onNext(1); |
24 | 53 | source.onNext(2); |
25 | 54 | source.onNext(3); |
26 | 55 |
|
27 | 56 | /* |
28 | | - * it will also emit 4 and onComplete for second observer also. |
| 57 | + * it will emit 4 and onComplete for second observer also. |
29 | 58 | */ |
30 | | - source.subscribe(getObserver("Second")); |
| 59 | + source.subscribe(getSecondObserver()); |
31 | 60 |
|
32 | 61 | source.onNext(4); |
33 | 62 | source.onComplete(); |
34 | 63 |
|
35 | | - /* |
36 | | - * it will also emit 4 and onComplete for second observer also. |
37 | | - */ |
38 | | - source.subscribe(getObserver("Third")); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + private Observer<Integer> getFirstObserver() { |
| 68 | + return new Observer<Integer>() { |
| 69 | + |
| 70 | + @Override |
| 71 | + public void onSubscribe(Disposable d) { |
| 72 | + Log.d(TAG, " First onSubscribe : " + d.isDisposed()); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void onNext(Integer value) { |
| 77 | + textView.append(" First onNext : value : " + value); |
| 78 | + textView.append(AppConstant.LINE_SEPARATOR); |
| 79 | + Log.d(TAG, " First onNext value : " + value); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void onError(Throwable e) { |
| 84 | + textView.append(" First onError : " + e.getMessage()); |
| 85 | + textView.append(AppConstant.LINE_SEPARATOR); |
| 86 | + Log.d(TAG, " First onError : " + e.getMessage()); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void onComplete() { |
| 91 | + textView.append(" First onComplete"); |
| 92 | + textView.append(AppConstant.LINE_SEPARATOR); |
| 93 | + Log.d(TAG, " First onComplete"); |
| 94 | + } |
| 95 | + }; |
| 96 | + } |
| 97 | + |
| 98 | + private Observer<Integer> getSecondObserver() { |
| 99 | + return new Observer<Integer>() { |
| 100 | + |
| 101 | + @Override |
| 102 | + public void onSubscribe(Disposable d) { |
| 103 | + textView.append(" Second onSubscribe : isDisposed :" + d.isDisposed()); |
| 104 | + Log.d(TAG, " Second onSubscribe : " + d.isDisposed()); |
| 105 | + textView.append(AppConstant.LINE_SEPARATOR); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void onNext(Integer value) { |
| 110 | + textView.append(" Second onNext : value : " + value); |
| 111 | + textView.append(AppConstant.LINE_SEPARATOR); |
| 112 | + Log.d(TAG, " Second onNext value : " + value); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void onError(Throwable e) { |
| 117 | + textView.append(" Second onError : " + e.getMessage()); |
| 118 | + textView.append(AppConstant.LINE_SEPARATOR); |
| 119 | + Log.d(TAG, " Second onError : " + e.getMessage()); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void onComplete() { |
| 124 | + textView.append(" Second onComplete"); |
| 125 | + textView.append(AppConstant.LINE_SEPARATOR); |
| 126 | + Log.d(TAG, " Second onComplete"); |
| 127 | + } |
| 128 | + }; |
39 | 129 | } |
40 | 130 |
|
41 | 131 |
|
|
0 commit comments