|
37 | 37 | public class RetrofitFragment |
38 | 38 | extends Fragment { |
39 | 39 |
|
40 | | - @Bind(R.id.demo_retrofit_contributors_username) EditText _username; |
41 | | - @Bind(R.id.demo_retrofit_contributors_repository) EditText _repo; |
| 40 | + @Bind(R.id.demo_retrofit_contributors_username) EditText _contributorsUsername; |
| 41 | + @Bind(R.id.demo_retrofit_contributors_repository) EditText _contributorsRepo; |
| 42 | + @Bind(R.id.demo_retrofit_contributors_with_user_info_username) EditText _userInfoUsername; |
| 43 | + @Bind(R.id.demo_retrofit_contributors_with_user_info_repository) EditText _userInfoRepo; |
42 | 44 | @Bind(R.id.log_list) ListView _resultList; |
43 | 45 |
|
44 | 46 | private GithubApi _api; |
@@ -87,106 +89,105 @@ public void onListContributorsClicked() { |
87 | 89 | _adapter.clear(); |
88 | 90 |
|
89 | 91 | _subscriptions.add(// |
90 | | - _api.contributors(_username.getText().toString(), _repo.getText().toString()) |
91 | | - .subscribeOn(Schedulers.io()) |
92 | | - .observeOn(AndroidSchedulers.mainThread()) |
93 | | - .subscribe(new Observer<List<Contributor>>() { |
94 | | - @Override |
95 | | - public void onCompleted() { |
96 | | - Timber.d("Retrofit call 1 completed"); |
97 | | - } |
98 | | - |
99 | | - @Override |
100 | | - public void onError(Throwable e) { |
101 | | - Timber.e(e, |
102 | | - "woops we got an error while getting the list of contributors"); |
103 | | - } |
104 | | - |
105 | | - @Override |
106 | | - public void onNext(List<Contributor> contributors) { |
107 | | - for (Contributor c : contributors) { |
108 | | - _adapter.add(format("%s has made %d contributions to %s", |
109 | | - c.login, |
110 | | - c.contributions, |
111 | | - _repo.getText().toString())); |
112 | | - |
113 | | - Timber.d("%s has made %d contributions to %s", |
114 | | - c.login, |
115 | | - c.contributions, |
116 | | - _repo.getText().toString()); |
| 92 | + _api.contributors(_contributorsUsername.getText().toString(), _contributorsRepo.getText().toString()) |
| 93 | + .subscribeOn(Schedulers.io()) |
| 94 | + .observeOn(AndroidSchedulers.mainThread()) |
| 95 | + .subscribe(new Observer<List<Contributor>>() { |
| 96 | + @Override |
| 97 | + public void onCompleted() { |
| 98 | + Timber.d("Retrofit call 1 completed"); |
117 | 99 | } |
118 | | - } |
119 | | - })); |
| 100 | + |
| 101 | + @Override |
| 102 | + public void onError(Throwable e) { |
| 103 | + Timber.e(e, |
| 104 | + "woops we got an error while getting the list of contributors"); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void onNext(List<Contributor> contributors) { |
| 109 | + for (Contributor c : contributors) { |
| 110 | + _adapter.add(format("%s has made %d contributions to %s", |
| 111 | + c.login, |
| 112 | + c.contributions, |
| 113 | + _contributorsRepo.getText().toString())); |
| 114 | + |
| 115 | + Timber.d("%s has made %d contributions to %s", |
| 116 | + c.login, |
| 117 | + c.contributions, |
| 118 | + _contributorsRepo.getText().toString()); |
| 119 | + } |
| 120 | + } |
| 121 | + })); |
120 | 122 | } |
121 | 123 |
|
122 | 124 | @OnClick(R.id.btn_demo_retrofit_contributors_with_user_info) |
123 | 125 | public void onListContributorsWithFullUserInfoClicked() { |
124 | 126 | _adapter.clear(); |
125 | 127 |
|
126 | | - _subscriptions.add(_api.contributors(_username.getText().toString(), |
127 | | - _repo.getText().toString()) |
128 | | - .flatMap(new Func1<List<Contributor>, Observable<Contributor>>() { |
129 | | - @Override |
130 | | - public Observable<Contributor> call(List<Contributor> contributors) { |
131 | | - return Observable.from(contributors); |
132 | | - } |
133 | | - }) |
134 | | - .flatMap(new Func1<Contributor, Observable<Pair<User, Contributor>>>() { |
135 | | - @Override |
136 | | - public Observable<Pair<User, Contributor>> call(Contributor contributor) { |
137 | | - Observable<User> _userObservable = _api.user(contributor.login) |
138 | | - .filter(new Func1<User, Boolean>() { |
139 | | - @Override |
140 | | - public Boolean call(User user) { |
141 | | - return !isEmpty(user.name) && !isEmpty(user.email); |
142 | | - } |
143 | | - }); |
144 | | - |
145 | | - return Observable.zip(_userObservable, |
146 | | - Observable.just(contributor), |
147 | | - new Func2<User, Contributor, Pair<User, Contributor>>() { |
148 | | - @Override |
149 | | - public Pair<User, Contributor> call(User user, |
150 | | - Contributor contributor) { |
151 | | - return new Pair<>(user, contributor); |
152 | | - } |
153 | | - }); |
154 | | - } |
155 | | - }) |
156 | | - .subscribeOn(Schedulers.newThread()) |
157 | | - .observeOn(AndroidSchedulers.mainThread()) |
158 | | - .subscribe(new Observer<Pair<User, Contributor>>() { |
159 | | - @Override |
160 | | - public void onCompleted() { |
161 | | - Timber.d("Retrofit call 2 completed "); |
162 | | - } |
163 | | - |
164 | | - @Override |
165 | | - public void onError(Throwable e) { |
166 | | - Timber.e(e, |
167 | | - "error while getting the list of contributors along with full names"); |
168 | | - } |
169 | | - |
170 | | - @Override |
171 | | - public void onNext(Pair<User, Contributor> pair) { |
172 | | - User user = pair.first; |
173 | | - Contributor contributor = pair.second; |
174 | | - |
175 | | - _adapter.add(format("%s(%s) has made %d contributions to %s", |
176 | | - user.name, |
177 | | - user.email, |
178 | | - contributor.contributions, |
179 | | - _repo.getText().toString())); |
180 | | - |
181 | | - _adapter.notifyDataSetChanged(); |
182 | | - |
183 | | - Timber.d("%s(%s) has made %d contributions to %s", |
184 | | - user.name, |
185 | | - user.email, |
186 | | - contributor.contributions, |
187 | | - _repo.getText().toString()); |
188 | | - } |
189 | | - })); |
| 128 | + _subscriptions.add(_api.contributors(_userInfoUsername.getText().toString(), _userInfoRepo.getText().toString()) |
| 129 | + .flatMap(new Func1<List<Contributor>, Observable<Contributor>>() { |
| 130 | + @Override |
| 131 | + public Observable<Contributor> call(List<Contributor> contributors) { |
| 132 | + return Observable.from(contributors); |
| 133 | + } |
| 134 | + }) |
| 135 | + .flatMap(new Func1<Contributor, Observable<Pair<User, Contributor>>>() { |
| 136 | + @Override |
| 137 | + public Observable<Pair<User, Contributor>> call(Contributor contributor) { |
| 138 | + Observable<User> _userObservable = _api.user(contributor.login) |
| 139 | + .filter(new Func1<User, Boolean>() { |
| 140 | + @Override |
| 141 | + public Boolean call(User user) { |
| 142 | + return !isEmpty(user.name) && !isEmpty(user.email); |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + return Observable.zip(_userObservable, |
| 147 | + Observable.just(contributor), |
| 148 | + new Func2<User, Contributor, Pair<User, Contributor>>() { |
| 149 | + @Override |
| 150 | + public Pair<User, Contributor> call(User user, |
| 151 | + Contributor contributor) { |
| 152 | + return new Pair<>(user, contributor); |
| 153 | + } |
| 154 | + }); |
| 155 | + } |
| 156 | + }) |
| 157 | + .subscribeOn(Schedulers.newThread()) |
| 158 | + .observeOn(AndroidSchedulers.mainThread()) |
| 159 | + .subscribe(new Observer<Pair<User, Contributor>>() { |
| 160 | + @Override |
| 161 | + public void onCompleted() { |
| 162 | + Timber.d("Retrofit call 2 completed "); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public void onError(Throwable e) { |
| 167 | + Timber.e(e, |
| 168 | + "error while getting the list of contributors along with full names"); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public void onNext(Pair<User, Contributor> pair) { |
| 173 | + User user = pair.first; |
| 174 | + Contributor contributor = pair.second; |
| 175 | + |
| 176 | + _adapter.add(format("%s(%s) has made %d contributions to %s", |
| 177 | + user.name, |
| 178 | + user.email, |
| 179 | + contributor.contributions, |
| 180 | + _contributorsRepo.getText().toString())); |
| 181 | + |
| 182 | + _adapter.notifyDataSetChanged(); |
| 183 | + |
| 184 | + Timber.d("%s(%s) has made %d contributions to %s", |
| 185 | + user.name, |
| 186 | + user.email, |
| 187 | + contributor.contributions, |
| 188 | + _contributorsRepo.getText().toString()); |
| 189 | + } |
| 190 | + })); |
190 | 191 | } |
191 | 192 |
|
192 | 193 | // ----------------------------------------------------------------------------------- |
|