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