-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample8Activity.java
207 lines (178 loc) · 8.81 KB
/
Example8Activity.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package iceteaviet.com.rxandroidex;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import iceteaviet.com.rxandroidex.adapter.SimpleStringAdapter;
import iceteaviet.com.rxandroidex.model.CombinedPojo;
import iceteaviet.com.rxandroidex.model.Post;
import iceteaviet.com.rxandroidex.model.User;
import iceteaviet.com.rxandroidex.rest.JsonPlaceholderService;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BiFunction;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class Example8Activity extends AppCompatActivity {
private static final String TAG = Example8Activity.class.getSimpleName();
@BindView(R.id.btn_load)
protected Button btnLoad;
@BindView(R.id.btn_load_observable)
protected Button btnObservableLoad;
@BindView(R.id.btn_zip)
protected Button btnZip;
@BindView(R.id.rv_example_list)
protected RecyclerView recyclerView;
private SimpleStringAdapter adapter;
private Call<List<Post>> call;
private Observable<List<Post>> postObservable;
private Observable<User> userObservable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example8);
ButterKnife.bind(this);
//Basic need for retrofit
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://jsonplaceholder.typicode.com/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) //Support RxJava for Retrofit2 (MUST HAVE)
.build();
//RxJava2CappAdapterFactory.create(): creates synchronous observables that by default do not operate on any scheduler
//RxJava2CappAdapterFactory.createAsync(): creates asynchronous observables that IGNORE all Observable#subscribeOn that set later
// -> If Apply subscribeOn(AndroidSchedulers.mainThread()) will take no effect,
//and the retrofit observable still continue executing WITHOUT NetworkOnMainThread exception!!!
final JsonPlaceholderService pojoCall = retrofit.create(JsonPlaceholderService.class);
//Normal callback
call = pojoCall.getListPost();
//Observable
postObservable = pojoCall.getListPostObservable();
userObservable = pojoCall.getUserObservable();
//Now, let's make things a bit more interesting.
// Let's say you not only want to retrieve the userPosts, but you are developing an Facebook-clone,
// and you want to retrieve 2 JSONs: getListPost() 2. getUser()
// You want to load these two JSONs in parallel, and when both are loaded, the page should be displayed
// The callback variant will become a bit more difficult:
// you have to create 2 callbacks, store the data in the activity, and if all the data is loaded, display the page
// There is 2 approach for this:
// - First: call getUser(), then when user loaded inside callback, call getListPost() -> Cause callback hell AND not optimal, not asynchronous
// - Second: call getUser() and getListPost() same time, inside callback of both of them
// check if the other data is finished loading -> process show data
// -> Cause boilerplate code that check finish loading and show data,
// -> hard to extend, needs to be adjusted in multiple places when we need more than 2 type of data in the future
// SO we use RxJava to make the code is more robust -> This is also the main reason to use RxJava over normal callback !!!
// Using zip() operator
final Observable<CombinedPojo> combinedPojoObservable = Observable.zip(postObservable, userObservable, new BiFunction<List<Post>, User, CombinedPojo>() {
@Override
public CombinedPojo apply(List<Post> posts, User users) throws Exception {
Log.d(TAG, "BiFunction apply()");
CombinedPojo res = new CombinedPojo();
res.setPosts(posts);
res.setUser(users);
return res;
}
});
adapter = new SimpleStringAdapter(this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//Normal callback
btnLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
adapter.clear();
call.enqueue(new Callback<List<Post>>() {
@Override
public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
List<Post> pojoList = response.body();
List<String> title = new ArrayList<>();
for (int i = 0; i < pojoList.size(); i++) {
title.add(pojoList.get(i).getTitle());
}
adapter.setStrings(title);
}
@Override
public void onFailure(Call<List<Post>> call, Throwable t) {
t.printStackTrace();
}
});
}
});
//Observable normal
btnObservableLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
adapter.clear();
postObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<Post>>() {
@Override
public void onSubscribe(Disposable d) {
Log.d(TAG, "onSubscribe");
}
@Override
public void onNext(List<Post> samplePojos) {
List<String> title = new ArrayList<>();
for (int i = 0; i < samplePojos.size(); i++) {
title.add(samplePojos.get(i).getTitle());
}
adapter.setStrings(title);
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete");
}
});
}
});
//Combine 2 observable using zip() operator
btnZip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
combinedPojoObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<CombinedPojo>() {
@Override
public void onSubscribe(Disposable d) {
Log.d(TAG, "onSubscribe");
}
@Override
public void onNext(CombinedPojo combinedPojo) {
Toast.makeText(Example8Activity.this,
"User: " + combinedPojo.getUser().getName() + ". Posts size: " + combinedPojo.getPosts().size(),
Toast.LENGTH_SHORT)
.show();
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete");
}
});
}
});
}
}