|
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 { |
30 | | - |
31 | | - GithubApi api; |
32 | | - |
33 | | - @InjectView(R.id.demo_retrofit_contributors_username) EditText _username; |
34 | | - @InjectView(R.id.demo_retrofit_contributors_repository) EditText _repo; |
35 | | - |
36 | | - @Override |
37 | | - public void onCreate(Bundle savedInstanceState) { |
38 | | - super.onCreate(savedInstanceState); |
39 | | - |
40 | | - RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint("https://api.github.com/") |
41 | | - .build(); |
42 | | - |
43 | | - api = restAdapter.create(GithubApi.class); |
44 | | - } |
45 | | - |
46 | | - @Override |
47 | | - public View onCreateView(LayoutInflater inflater, |
48 | | - @Nullable ViewGroup container, |
49 | | - @Nullable Bundle savedInstanceState) { |
50 | | - View layout = inflater.inflate(R.layout.fragment_retrofit, container, false); |
51 | | - ButterKnife.inject(this, layout); |
52 | | - return layout; |
53 | | - } |
54 | | - |
55 | | - @OnClick(R.id.btn_demo_retrofit_contributors) |
56 | | - public void onListContributorsClicked() { |
57 | | - api.contributors(_username.getText().toString(), _repo.getText().toString()) |
58 | | - .subscribe(new Observer<List<Contributor>>() { |
59 | | - @Override |
60 | | - public void onCompleted() { |
61 | | - Timber.d("Retrofit call 1 completed"); |
62 | | - } |
63 | | - |
64 | | - @Override |
65 | | - public void onError(Throwable e) { |
66 | | - Timber.e(e, "woops we got an error while getting the list of contributors"); |
67 | | - } |
68 | | - |
69 | | - @Override |
70 | | - public void onNext(List<Contributor> contributors) { |
71 | | - for (Contributor c : contributors) { |
72 | | - Timber.d("%s has made %d contributions to %s", |
73 | | - c.login, |
74 | | - c.contributions, |
75 | | - _repo.getText().toString()); |
76 | | - } |
77 | | - } |
78 | | - }); |
79 | | - } |
80 | | - |
81 | | - @OnClick(R.id.btn_demo_retrofit_contributors_with_user_info) |
82 | | - public void onListContributorsWithFullUserInfoClicked() { |
83 | | - api.contributors(_username.getText().toString(), _repo.getText().toString()) |
84 | | - .flatMap(new Func1<List<Contributor>, Observable<Contributor>>() { |
85 | | - @Override |
86 | | - public Observable<Contributor> call(List<Contributor> contributors) { |
87 | | - return Observable.from(contributors); |
88 | | - } |
89 | | - }) |
90 | | - .flatMap(new Func1<Contributor, Observable<?>>() { |
91 | | - @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(); |
113 | | - } |
114 | | - }) |
115 | | - .subscribeOn(Schedulers.newThread()) |
116 | | - .observeOn(AndroidSchedulers.mainThread()) |
117 | | - .subscribe(new Observer<Object>() { |
118 | | - @Override |
119 | | - public void onCompleted() { |
120 | | - Timber.d("Retrofit call 2 completed "); |
121 | | - } |
122 | | - |
123 | | - @Override |
124 | | - 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"); |
127 | | - } |
128 | | - |
129 | | - @Override |
130 | | - public void onNext(Object o) { |
131 | | - Timber.d("hi! onNext"); |
132 | | - } |
133 | | - }); |
134 | | - } |
| 37 | +public class RetrofitFragment extends Fragment { |
| 38 | + |
| 39 | + private ArrayAdapter<String> _adapter; |
| 40 | + |
| 41 | + GithubApi api; |
| 42 | + |
| 43 | + @InjectView(R.id.log_list) ListView _resultsListView; |
| 44 | + @InjectView(R.id.demo_retrofit_contributors_username) EditText _username; |
| 45 | + @InjectView(R.id.demo_retrofit_contributors_repository) EditText _repo; |
| 46 | + |
| 47 | + @Override |
| 48 | + public void onCreate(Bundle savedInstanceState) { |
| 49 | + super.onCreate(savedInstanceState); |
| 50 | + |
| 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(); |
| 69 | + |
| 70 | + api = restAdapter.create(GithubApi.class); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public View onCreateView(LayoutInflater inflater, |
| 75 | + @Nullable ViewGroup container, |
| 76 | + @Nullable Bundle savedInstanceState) { |
| 77 | + View layout = inflater.inflate(R.layout.fragment_retrofit, container, false); |
| 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 | + |
| 86 | + return layout; |
| 87 | + } |
| 88 | + |
| 89 | + @OnClick(R.id.btn_demo_retrofit_contributors) |
| 90 | + public void onListContributorsClicked() { |
| 91 | + _adapter.clear(); |
| 92 | + api.contributors(_username.getText().toString(), _repo.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"); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void onError(Throwable e) { |
| 103 | + Timber.e(e, "woops we got an error while getting the list of contributors"); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void onNext(List<Contributor> contributors) { |
| 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())); |
| 113 | + Timber.d("%s has made %d contributions to %s", |
| 114 | + c.login, |
| 115 | + c.contributions, |
| 116 | + _repo.getText().toString()); |
| 117 | + } |
| 118 | + } |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + @OnClick(R.id.btn_demo_retrofit_contributors_with_user_info) |
| 123 | + public void onListContributorsWithFullUserInfoClicked() { |
| 124 | + _adapter.clear(); |
| 125 | + api.contributors(_username.getText().toString(), _repo.getText().toString()) |
| 126 | + .flatMap(new Func1<List<Contributor>, Observable<Contributor>>() { |
| 127 | + @Override |
| 128 | + public Observable<Contributor> call(List<Contributor> contributors) { |
| 129 | + return Observable.from(contributors); |
| 130 | + } |
| 131 | + }) |
| 132 | + .flatMap(new Func1<Contributor, Observable<?>>() { |
| 133 | + @Override |
| 134 | + public Observable<?> call(Contributor contributor) { |
| 135 | + Observable.zip(Observable.just(contributor), |
| 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(); |
| 160 | + return Observable.empty(); |
| 161 | + } |
| 162 | + }) |
| 163 | + .subscribeOn(Schedulers.newThread()) |
| 164 | + .observeOn(AndroidSchedulers.mainThread()) |
| 165 | + .subscribe(new Observer<Object>() { |
| 166 | + @Override |
| 167 | + public void onCompleted() { |
| 168 | + Timber.d("Retrofit call 2 completed "); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public void onError(Throwable e) { |
| 173 | + Timber.e(e, |
| 174 | + "woops we got an error while getting the list of contributors along with full names"); |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public void onNext(Object o) { |
| 179 | + Timber.d("hi! onNext"); |
| 180 | + } |
| 181 | + }); |
| 182 | + } |
135 | 183 | } |
0 commit comments