|
3 | 3 | import android.os.Bundle; |
4 | 4 | import android.support.annotation.Nullable; |
5 | 5 | import android.support.v4.app.Fragment; |
| 6 | +import android.util.Pair; |
6 | 7 | import android.view.LayoutInflater; |
7 | 8 | import android.view.View; |
8 | 9 | import android.view.ViewGroup; |
| 10 | +import android.widget.ArrayAdapter; |
9 | 11 | import android.widget.EditText; |
| 12 | +import android.widget.ListView; |
10 | 13 | import butterknife.ButterKnife; |
11 | 14 | import butterknife.InjectView; |
12 | 15 | import butterknife.OnClick; |
13 | | -import com.google.common.base.Strings; |
14 | 16 | import com.morihacky.android.rxjava.app.R; |
15 | 17 | import com.morihacky.android.rxjava.retrofit.Contributor; |
16 | 18 | import com.morihacky.android.rxjava.retrofit.GithubApi; |
17 | 19 | import com.morihacky.android.rxjava.retrofit.User; |
| 20 | +import java.util.ArrayList; |
18 | 21 | import java.util.List; |
| 22 | +import retrofit.RequestInterceptor; |
19 | 23 | import retrofit.RestAdapter; |
20 | 24 | import rx.Observable; |
21 | 25 | import rx.Observer; |
|
25 | 29 | import rx.schedulers.Schedulers; |
26 | 30 | import timber.log.Timber; |
27 | 31 |
|
| 32 | +import static com.google.common.base.Strings.isNullOrEmpty; |
| 33 | +import static java.lang.String.format; |
| 34 | + |
28 | 35 | public class RetrofitFragment |
29 | 36 | extends Fragment { |
30 | 37 |
|
31 | | - GithubApi api; |
32 | | - |
33 | 38 | @InjectView(R.id.demo_retrofit_contributors_username) EditText _username; |
34 | 39 | @InjectView(R.id.demo_retrofit_contributors_repository) EditText _repo; |
| 40 | + @InjectView(R.id.log_list) ListView _resultList; |
| 41 | + |
| 42 | + private GithubApi _api; |
| 43 | + private ArrayAdapter<String> _adapter; |
35 | 44 |
|
36 | 45 | @Override |
37 | 46 | public void onCreate(Bundle savedInstanceState) { |
38 | 47 | super.onCreate(savedInstanceState); |
39 | | - |
40 | | - RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint("https://api.github.com/") |
41 | | - .build(); |
42 | | - |
43 | | - api = restAdapter.create(GithubApi.class); |
| 48 | + _api = _createGithubApi(); |
44 | 49 | } |
45 | 50 |
|
46 | 51 | @Override |
47 | 52 | public View onCreateView(LayoutInflater inflater, |
48 | 53 | @Nullable ViewGroup container, |
49 | 54 | @Nullable Bundle savedInstanceState) { |
| 55 | + |
50 | 56 | View layout = inflater.inflate(R.layout.fragment_retrofit, container, false); |
51 | 57 | ButterKnife.inject(this, layout); |
| 58 | + |
| 59 | + _adapter = new ArrayAdapter<>(getActivity(), |
| 60 | + R.layout.item_log, |
| 61 | + R.id.item_log, |
| 62 | + new ArrayList<String>()); |
| 63 | + //_adapter.setNotifyOnChange(true); |
| 64 | + _resultList.setAdapter(_adapter); |
| 65 | + |
52 | 66 | return layout; |
53 | 67 | } |
54 | 68 |
|
55 | 69 | @OnClick(R.id.btn_demo_retrofit_contributors) |
56 | 70 | public void onListContributorsClicked() { |
57 | | - api.contributors(_username.getText().toString(), _repo.getText().toString()) |
| 71 | + _adapter.clear(); |
| 72 | + |
| 73 | + _api.contributors(_username.getText().toString(), _repo.getText().toString()) |
| 74 | + .subscribeOn(Schedulers.io()) |
| 75 | + .observeOn(AndroidSchedulers.mainThread()) |
58 | 76 | .subscribe(new Observer<List<Contributor>>() { |
59 | 77 | @Override |
60 | 78 | public void onCompleted() { |
@@ -69,67 +87,108 @@ public void onError(Throwable e) { |
69 | 87 | @Override |
70 | 88 | public void onNext(List<Contributor> contributors) { |
71 | 89 | for (Contributor c : contributors) { |
| 90 | + _adapter.add(format("%s has made %d contributions to %s", |
| 91 | + c.login, |
| 92 | + c.contributions, |
| 93 | + _repo.getText().toString())); |
| 94 | + |
72 | 95 | Timber.d("%s has made %d contributions to %s", |
73 | | - c.login, |
74 | | - c.contributions, |
75 | | - _repo.getText().toString()); |
| 96 | + c.login, |
| 97 | + c.contributions, |
| 98 | + _repo.getText().toString()); |
76 | 99 | } |
77 | 100 | } |
78 | 101 | }); |
79 | 102 | } |
80 | 103 |
|
81 | 104 | @OnClick(R.id.btn_demo_retrofit_contributors_with_user_info) |
82 | 105 | public void onListContributorsWithFullUserInfoClicked() { |
83 | | - api.contributors(_username.getText().toString(), _repo.getText().toString()) |
| 106 | + _adapter.clear(); |
| 107 | + |
| 108 | + _api.contributors(_username.getText().toString(), _repo.getText().toString()) |
84 | 109 | .flatMap(new Func1<List<Contributor>, Observable<Contributor>>() { |
85 | 110 | @Override |
86 | 111 | public Observable<Contributor> call(List<Contributor> contributors) { |
87 | 112 | return Observable.from(contributors); |
88 | 113 | } |
89 | 114 | }) |
90 | | - .flatMap(new Func1<Contributor, Observable<?>>() { |
| 115 | + .flatMap(new Func1<Contributor, Observable<Pair<User, Contributor>>>() { |
91 | 116 | @Override |
92 | | - public Observable<?> call(Contributor contributor) { |
93 | | - Observable.zip(Observable.just(contributor), |
94 | | - api.user(contributor.login).filter(new Func1<User, Boolean>() { |
95 | | - @Override |
96 | | - public Boolean call(User user) { |
97 | | - return !Strings.isNullOrEmpty(user.name) && !Strings.isNullOrEmpty(user.email); |
98 | | - } |
99 | | - }), |
100 | | - new Func2<Contributor, User, Object>() { |
101 | | - @Override |
102 | | - public Object call(Contributor contributor, User user) { |
103 | | - Timber.d("%s(%s) has made %d contributions to %s", |
104 | | - user.name, |
105 | | - user.email, |
106 | | - contributor.contributions, |
107 | | - _repo.getText().toString()); |
108 | | - |
109 | | - return Observable.empty(); |
110 | | - } |
111 | | - }).subscribe(); |
112 | | - return Observable.empty(); |
| 117 | + public Observable<Pair<User, Contributor>> call(Contributor contributor) { |
| 118 | + Observable<User> _userObservable = _api.user(contributor.login) |
| 119 | + .filter(new Func1<User, Boolean>() { |
| 120 | + @Override |
| 121 | + public Boolean call(User user) { |
| 122 | + return !isNullOrEmpty(user.name) && !isNullOrEmpty(user.email); |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + return Observable.zip(_userObservable, |
| 127 | + Observable.just(contributor), |
| 128 | + new Func2<User, Contributor, Pair<User, Contributor>>() { |
| 129 | + @Override |
| 130 | + public Pair<User, Contributor> call(User user, Contributor contributor) { |
| 131 | + return new Pair<>(user, contributor); |
| 132 | + } |
| 133 | + }); |
113 | 134 | } |
114 | 135 | }) |
115 | 136 | .subscribeOn(Schedulers.newThread()) |
116 | 137 | .observeOn(AndroidSchedulers.mainThread()) |
117 | | - .subscribe(new Observer<Object>() { |
| 138 | + .subscribe(new Observer<Pair<User, Contributor>>() { |
118 | 139 | @Override |
119 | 140 | public void onCompleted() { |
120 | 141 | Timber.d("Retrofit call 2 completed "); |
121 | 142 | } |
122 | 143 |
|
123 | 144 | @Override |
124 | 145 | public void onError(Throwable e) { |
125 | | - Timber.e(e, |
126 | | - "woops we got an error while getting the list of contributors along with full names"); |
| 146 | + Timber.e(e, "error while getting the list of contributors along with full names"); |
127 | 147 | } |
128 | 148 |
|
129 | 149 | @Override |
130 | | - public void onNext(Object o) { |
131 | | - Timber.d("hi! onNext"); |
| 150 | + public void onNext(Pair<User, Contributor> pair) { |
| 151 | + if (pair == null) { |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + User user = pair.first; |
| 156 | + Contributor contributor = pair.second; |
| 157 | + |
| 158 | + _adapter.add(format("%s(%s) has made %d contributions to %s", |
| 159 | + user.name, |
| 160 | + user.email, |
| 161 | + contributor.contributions, |
| 162 | + _repo.getText().toString())); |
| 163 | + |
| 164 | + _adapter.notifyDataSetChanged(); |
| 165 | + |
| 166 | + Timber.d("%s(%s) has made %d contributions to %s", |
| 167 | + user.name, |
| 168 | + user.email, |
| 169 | + contributor.contributions, |
| 170 | + _repo.getText().toString()); |
132 | 171 | } |
133 | 172 | }); |
134 | 173 | } |
| 174 | + |
| 175 | + // ----------------------------------------------------------------------------------- |
| 176 | + |
| 177 | + private GithubApi _createGithubApi() { |
| 178 | + |
| 179 | + RestAdapter.Builder builder = new RestAdapter.Builder().setEndpoint("https://api.github.com/"); |
| 180 | + //.setLogLevel(RestAdapter.LogLevel.FULL); |
| 181 | + |
| 182 | + final String githubToken = getResources().getString(R.string.github_oauth_token); |
| 183 | + if (!isNullOrEmpty(githubToken)) { |
| 184 | + builder.setRequestInterceptor(new RequestInterceptor() { |
| 185 | + @Override |
| 186 | + public void intercept(RequestFacade request) { |
| 187 | + request.addHeader("Authorization", format("token %s", githubToken)); |
| 188 | + } |
| 189 | + }); |
| 190 | + } |
| 191 | + |
| 192 | + return builder.build().create(GithubApi.class); |
| 193 | + } |
135 | 194 | } |