|
10 | 10 | import android.widget.ArrayAdapter; |
11 | 11 | import android.widget.EditText; |
12 | 12 | import android.widget.ListView; |
13 | | - |
| 13 | +import butterknife.Bind; |
| 14 | +import butterknife.ButterKnife; |
| 15 | +import butterknife.OnClick; |
14 | 16 | import com.morihacky.android.rxjava.R; |
15 | 17 | import com.morihacky.android.rxjava.RxUtils; |
16 | 18 | import com.morihacky.android.rxjava.retrofit.Contributor; |
17 | 19 | import com.morihacky.android.rxjava.retrofit.GithubApi; |
18 | 20 | import com.morihacky.android.rxjava.retrofit.GithubService; |
19 | 21 | import com.morihacky.android.rxjava.retrofit.User; |
20 | | - |
21 | 22 | import java.util.ArrayList; |
22 | 23 | import java.util.List; |
23 | | - |
24 | | -import butterknife.Bind; |
25 | | -import butterknife.ButterKnife; |
26 | | -import butterknife.OnClick; |
27 | 24 | import rx.Observable; |
28 | 25 | import rx.Observer; |
29 | 26 | import rx.android.schedulers.AndroidSchedulers; |
|
37 | 34 | import static java.lang.String.format; |
38 | 35 |
|
39 | 36 | public class RetrofitFragment |
40 | | - extends Fragment { |
| 37 | + extends Fragment { |
41 | 38 |
|
42 | 39 | @Bind(R.id.demo_retrofit_contributors_username) EditText _username; |
43 | 40 | @Bind(R.id.demo_retrofit_contributors_repository) EditText _repo; |
44 | 41 | @Bind(R.id.log_list) ListView _resultList; |
45 | 42 |
|
46 | | - private GithubApi _githubService; |
47 | 43 | private ArrayAdapter<String> _adapter; |
| 44 | + private GithubApi _githubService; |
48 | 45 | private CompositeSubscription _subscriptions = new CompositeSubscription(); |
49 | 46 |
|
50 | | - @Override public void onCreate(Bundle savedInstanceState) { |
| 47 | + @Override |
| 48 | + public void onCreate(Bundle savedInstanceState) { |
51 | 49 | super.onCreate(savedInstanceState); |
52 | 50 | String githubToken = getResources().getString(R.string.github_oauth_token); |
53 | 51 | _githubService = GithubService.createGithubService(githubToken); |
54 | 52 | } |
55 | 53 |
|
56 | | - @Override public void onResume() { |
57 | | - super.onResume(); |
58 | | - _subscriptions = RxUtils.getNewCompositeSubIfUnsubscribed(_subscriptions); |
59 | | - } |
60 | | - |
61 | | - @Override public void onPause() { |
62 | | - super.onPause(); |
63 | | - |
64 | | - RxUtils.unsubscribeIfNotNull(_subscriptions); |
65 | | - } |
66 | | - |
67 | 54 | @Override |
68 | 55 | public View onCreateView(LayoutInflater inflater, |
69 | | - @Nullable ViewGroup container, |
70 | | - @Nullable Bundle savedInstanceState) { |
| 56 | + @Nullable ViewGroup container, |
| 57 | + @Nullable Bundle savedInstanceState) { |
71 | 58 |
|
72 | 59 | View layout = inflater.inflate(R.layout.fragment_retrofit, container, false); |
73 | 60 | ButterKnife.bind(this, layout); |
@@ -79,105 +66,119 @@ public View onCreateView(LayoutInflater inflater, |
79 | 66 | return layout; |
80 | 67 | } |
81 | 68 |
|
82 | | - @OnClick(R.id.btn_demo_retrofit_contributors) public void onListContributorsClicked() { |
| 69 | + @Override |
| 70 | + public void onResume() { |
| 71 | + super.onResume(); |
| 72 | + _subscriptions = RxUtils.getNewCompositeSubIfUnsubscribed(_subscriptions); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void onPause() { |
| 77 | + super.onPause(); |
| 78 | + |
| 79 | + RxUtils.unsubscribeIfNotNull(_subscriptions); |
| 80 | + } |
| 81 | + |
| 82 | + @OnClick(R.id.btn_demo_retrofit_contributors) |
| 83 | + public void onListContributorsClicked() { |
83 | 84 | _adapter.clear(); |
84 | 85 |
|
85 | 86 | _subscriptions.add(// |
86 | | - _githubService.contributors(_username.getText().toString(), _repo.getText().toString()) |
87 | | - .subscribeOn(Schedulers.io()) |
88 | | - .observeOn(AndroidSchedulers.mainThread()) |
89 | | -// .unsubscribeOn(Schedulers.io()) |
90 | | - // TODO: remove when https://github.com/square/okhttp/issues/1592 is fixed |
91 | | - .subscribe(new Observer<List<Contributor>>() { |
92 | | - @Override public void onCompleted() { |
93 | | - Timber.d("Retrofit call 1 completed"); |
94 | | - } |
95 | | - |
96 | | - @Override public void onError(Throwable e) { |
97 | | - Timber.e(e, "woops we got an error while getting the list of contributors"); |
98 | | - } |
99 | | - |
100 | | - @Override public void onNext(List<Contributor> contributors) { |
101 | | - for (Contributor c : contributors) { |
102 | | - _adapter.add(format("%s has made %d contributions to %s", |
103 | | - c.login, |
104 | | - c.contributions, |
105 | | - _repo.getText().toString())); |
106 | | - |
107 | | - Timber.d("%s has made %d contributions to %s", |
108 | | - c.login, |
109 | | - c.contributions, |
110 | | - _repo.getText().toString()); |
111 | | - } |
112 | | - } |
113 | | - })); |
| 87 | + _githubService.contributors(_username.getText().toString(), _repo.getText().toString()) |
| 88 | + .subscribeOn(Schedulers.io()) |
| 89 | + .observeOn(AndroidSchedulers.mainThread()) |
| 90 | + .subscribe(new Observer<List<Contributor>>() { |
| 91 | + @Override |
| 92 | + public void onCompleted() { |
| 93 | + Timber.d("Retrofit call 1 completed"); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void onError(Throwable e) { |
| 98 | + Timber.e(e, "woops we got an error while getting the list of contributors"); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void onNext(List<Contributor> contributors) { |
| 103 | + for (Contributor c : contributors) { |
| 104 | + _adapter.add(format("%s has made %d contributions to %s", |
| 105 | + c.login, |
| 106 | + c.contributions, |
| 107 | + _repo.getText().toString())); |
| 108 | + |
| 109 | + Timber.d("%s has made %d contributions to %s", |
| 110 | + c.login, |
| 111 | + c.contributions, |
| 112 | + _repo.getText().toString()); |
| 113 | + } |
| 114 | + } |
| 115 | + })); |
114 | 116 | } |
115 | 117 |
|
116 | 118 | @OnClick(R.id.btn_demo_retrofit_contributors_with_user_info) |
117 | 119 | public void onListContributorsWithFullUserInfoClicked() { |
118 | 120 | _adapter.clear(); |
119 | 121 |
|
120 | 122 | _subscriptions.add(_githubService.contributors(_username.getText().toString(), _repo.getText().toString()) |
121 | | - .flatMap(new Func1<List<Contributor>, Observable<Contributor>>() { |
122 | | - @Override public Observable<Contributor> call(List<Contributor> contributors) { |
123 | | - return Observable.from(contributors); |
124 | | - } |
125 | | - }) |
126 | | - .flatMap(new Func1<Contributor, Observable<Pair<User, Contributor>>>() { |
127 | | - @Override |
128 | | - public Observable<Pair<User, Contributor>> call(Contributor contributor) { |
129 | | - Observable<User> _userObservable = _githubService.user(contributor.login) |
130 | | - .filter(new Func1<User, Boolean>() { |
131 | | - @Override public Boolean call(User user) { |
132 | | - return !isEmpty(user.name) && !isEmpty(user.email); |
133 | | - } |
134 | | - }); |
135 | | - |
136 | | - return Observable.zip(_userObservable, |
137 | | - Observable.just(contributor), |
138 | | - new Func2<User, Contributor, Pair<User, |
139 | | - Contributor>>() { |
140 | | - @Override |
141 | | - public Pair<User, Contributor> call(User user, |
142 | | - Contributor contributor) { |
143 | | - return new Pair<>(user, contributor); |
144 | | - } |
145 | | - }); |
146 | | - } |
147 | | - }) |
148 | | - .subscribeOn(Schedulers.newThread()) |
149 | | - .observeOn(AndroidSchedulers.mainThread()) |
150 | | -// .unsubscribeOn(Schedulers.newThread()) |
151 | | - // TODO: remove when https://github.com/square/okhttp/issues/1592 is fixed |
152 | | - .subscribe(new Observer<Pair<User, Contributor>>() { |
153 | | - @Override public void onCompleted() { |
154 | | - Timber.d("Retrofit call 2 completed "); |
155 | | - } |
156 | | - |
157 | | - @Override public void onError(Throwable e) { |
158 | | - Timber.e(e, |
159 | | - "error while getting the list of contributors along with full " |
160 | | - + "names"); |
161 | | - } |
162 | | - |
163 | | - @Override public void onNext(Pair<User, Contributor> pair) { |
164 | | - User user = pair.first; |
165 | | - Contributor contributor = pair.second; |
166 | | - |
167 | | - _adapter.add(format("%s(%s) has made %d contributions to %s", |
168 | | - user.name, |
169 | | - user.email, |
170 | | - contributor.contributions, |
171 | | - _repo.getText().toString())); |
172 | | - |
173 | | - _adapter.notifyDataSetChanged(); |
174 | | - |
175 | | - Timber.d("%s(%s) has made %d contributions to %s", |
176 | | - user.name, |
177 | | - user.email, |
178 | | - contributor.contributions, |
179 | | - _repo.getText().toString()); |
180 | | - } |
181 | | - })); |
| 123 | + .flatMap(new Func1<List<Contributor>, Observable<Contributor>>() { |
| 124 | + @Override |
| 125 | + public Observable<Contributor> call(List<Contributor> contributors) { |
| 126 | + return Observable.from(contributors); |
| 127 | + } |
| 128 | + }) |
| 129 | + .flatMap(new Func1<Contributor, Observable<Pair<User, Contributor>>>() { |
| 130 | + @Override |
| 131 | + public Observable<Pair<User, Contributor>> call(Contributor contributor) { |
| 132 | + Observable<User> _userObservable = _githubService.user(contributor.login) |
| 133 | + .filter(new Func1<User, Boolean>() { |
| 134 | + @Override |
| 135 | + public Boolean call(User user) { |
| 136 | + return !isEmpty(user.name) && !isEmpty(user.email); |
| 137 | + } |
| 138 | + }); |
| 139 | + |
| 140 | + return Observable.zip(_userObservable, |
| 141 | + Observable.just(contributor), |
| 142 | + new Func2<User, Contributor, Pair<User, Contributor>>() { |
| 143 | + @Override |
| 144 | + public Pair<User, Contributor> call(User user, Contributor contributor) { |
| 145 | + return new Pair<>(user, contributor); |
| 146 | + } |
| 147 | + }); |
| 148 | + } |
| 149 | + }) |
| 150 | + .subscribeOn(Schedulers.newThread()) |
| 151 | + .observeOn(AndroidSchedulers.mainThread()) |
| 152 | + .subscribe(new Observer<Pair<User, Contributor>>() { |
| 153 | + @Override |
| 154 | + public void onCompleted() { |
| 155 | + Timber.d("Retrofit call 2 completed "); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public void onError(Throwable e) { |
| 160 | + Timber.e(e, "error while getting the list of contributors along with full " + "names"); |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public void onNext(Pair<User, Contributor> pair) { |
| 165 | + User user = pair.first; |
| 166 | + Contributor contributor = pair.second; |
| 167 | + |
| 168 | + _adapter.add(format("%s(%s) has made %d contributions to %s", |
| 169 | + user.name, |
| 170 | + user.email, |
| 171 | + contributor.contributions, |
| 172 | + _repo.getText().toString())); |
| 173 | + |
| 174 | + _adapter.notifyDataSetChanged(); |
| 175 | + |
| 176 | + Timber.d("%s(%s) has made %d contributions to %s", |
| 177 | + user.name, |
| 178 | + user.email, |
| 179 | + contributor.contributions, |
| 180 | + _repo.getText().toString()); |
| 181 | + } |
| 182 | + })); |
182 | 183 | } |
183 | 184 | } |