Skip to content

Commit 50cc3c3

Browse files
Code refactor for simplicity
1 parent e243132 commit 50cc3c3

31 files changed

+1969
-492
lines changed

app/src/main/java/com/rxjava2/android/samples/model/Car.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public void setBrand(String brand) {
1717
}
1818

1919
public Observable<String> brandDeferObservable() {
20-
// return Observable.just(brand);
2120
return Observable.defer(new Callable<ObservableSource<? extends String>>() {
2221
@Override
2322
public ObservableSource<? extends String> call() throws Exception {

app/src/main/java/com/rxjava2/android/samples/ui/ExampleBaseActivity.java

Lines changed: 0 additions & 226 deletions
This file was deleted.

app/src/main/java/com/rxjava2/android/samples/ui/operators/AsyncSubjectExampleActivity.java

Lines changed: 101 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,131 @@
11
package com.rxjava2.android.samples.ui.operators;
22

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;
49

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;
515
import io.reactivex.subjects.AsyncSubject;
616

717
/**
818
* Created by amitshekhar on 17/12/16.
919
*/
1020

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+
}
1241

1342
/* An AsyncSubject emits the last value (and only the last value) emitted by the source
1443
* Observable, and only after that source Observable completes. (If the source Observable
1544
* does not emit any values, the AsyncSubject also completes without emitting any values.)
1645
*/
17-
protected void doSomeWork() {
46+
private void doSomeWork() {
47+
1848
AsyncSubject<Integer> source = AsyncSubject.create();
1949

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
2251

2352
source.onNext(1);
2453
source.onNext(2);
2554
source.onNext(3);
2655

2756
/*
28-
* it will also emit 4 and onComplete for second observer also.
57+
* it will emit 4 and onComplete for second observer also.
2958
*/
30-
source.subscribe(getObserver("Second"));
59+
source.subscribe(getSecondObserver());
3160

3261
source.onNext(4);
3362
source.onComplete();
3463

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+
};
39129
}
40130

41131

0 commit comments

Comments
 (0)