-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathExample5Activity.java
139 lines (114 loc) · 4.13 KB
/
Example5Activity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package info.androidhive.rxandroidexamples.basics;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import info.androidhive.rxandroidexamples.R;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Function;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
public class Example5Activity extends AppCompatActivity {
/**
* Basic Observable, Observer, Subscriber example
* Introduced CompositeDisposable and DisposableObserver
* The observable emits custom data type (Note) instead of primitive data types
* ----
* .map() operator is used to turn the note into all uppercase letters
* ----
* You can also notice we got rid of the below declarations
* Observable<Note> notesObservable = getNotesObservable();
* DisposableObserver<Note> notesObserver = getNotesObserver();
*/
private static final String TAG = Example5Activity.class.getSimpleName();
private CompositeDisposable disposable = new CompositeDisposable();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example5);
// add to Composite observable
// .map() operator is used to turn the note into all uppercase letters
disposable.add(getNotesObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map(new Function<Note, Note>() {
@Override
public Note apply(Note note) throws Exception {
// Making the note to all uppercase
note.setNote(note.getNote().toUpperCase());
return note;
}
})
.subscribeWith(getNotesObserver()));
}
private DisposableObserver<Note> getNotesObserver() {
return new DisposableObserver<Note>() {
@Override
public void onNext(Note note) {
Log.d(TAG, "Id: " + note.getId() + ", note: " + note.getNote());
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError: " + e.getMessage());
}
@Override
public void onComplete() {
Log.d(TAG, "All notes are emitted!");
}
};
}
private Observable<Note> getNotesObservable() {
final List<Note> notes = prepareNotes();
return Observable.create(new ObservableOnSubscribe<Note>() {
@Override
public void subscribe(ObservableEmitter<Note> emitter) throws Exception {
for (Note note : notes) {
if (!emitter.isDisposed()) {
emitter.onNext(note);
}
}
if (!emitter.isDisposed()) {
emitter.onComplete();
}
}
});
}
private List<Note> prepareNotes() {
List<Note> notes = new ArrayList<>();
notes.add(new Note(1, "buy tooth paste!"));
notes.add(new Note(2, "call brother!"));
notes.add(new Note(3, "watch narcos tonight!"));
notes.add(new Note(4, "pay power bill!"));
return notes;
}
class Note {
int id;
String note;
public Note(int id, String note) {
this.id = id;
this.note = note;
}
public int getId() {
return id;
}
public String getNote() {
return note;
}
public void setId(int id) {
this.id = id;
}
public void setNote(String note) {
this.note = note;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
disposable.clear();
}
}