Skip to content

Commit 9c4f75e

Browse files
author
Kaushik Gopal
committed
Merge branch 'alanwgeorge-master' (add github token support + nicer list population)
* alanwgeorge-master: fix: cleanup list populating code fix intents for better diff view added listview output to Retrofit example
2 parents 736b1ac + d5623ae commit 9c4f75e

File tree

4 files changed

+107
-43
lines changed

4 files changed

+107
-43
lines changed

app/src/main/java/com/morihacky/android/rxjava/MainActivity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ protected void onCreate(Bundle savedInstanceState) {
2020

2121
getSupportFragmentManager().beginTransaction()
2222
.addToBackStack(this.toString())
23-
//.replace(R.id.activity_main, new MainFragment(), this.toString())
24-
.replace(R.id.activity_main, new PseudoCacheConcatFragment(), this.toString())
23+
.replace(R.id.activity_main, new MainFragment(), this.toString())
2524
.commit();
2625
}
2726

app/src/main/java/com/morihacky/android/rxjava/RetrofitFragment.java

Lines changed: 99 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@
33
import android.os.Bundle;
44
import android.support.annotation.Nullable;
55
import android.support.v4.app.Fragment;
6+
import android.util.Pair;
67
import android.view.LayoutInflater;
78
import android.view.View;
89
import android.view.ViewGroup;
10+
import android.widget.ArrayAdapter;
911
import android.widget.EditText;
12+
import android.widget.ListView;
1013
import butterknife.ButterKnife;
1114
import butterknife.InjectView;
1215
import butterknife.OnClick;
13-
import com.google.common.base.Strings;
1416
import com.morihacky.android.rxjava.app.R;
1517
import com.morihacky.android.rxjava.retrofit.Contributor;
1618
import com.morihacky.android.rxjava.retrofit.GithubApi;
1719
import com.morihacky.android.rxjava.retrofit.User;
20+
import java.util.ArrayList;
1821
import java.util.List;
22+
import retrofit.RequestInterceptor;
1923
import retrofit.RestAdapter;
2024
import rx.Observable;
2125
import rx.Observer;
@@ -25,36 +29,50 @@
2529
import rx.schedulers.Schedulers;
2630
import timber.log.Timber;
2731

32+
import static com.google.common.base.Strings.isNullOrEmpty;
33+
import static java.lang.String.format;
34+
2835
public class RetrofitFragment
2936
extends Fragment {
3037

31-
GithubApi api;
32-
3338
@InjectView(R.id.demo_retrofit_contributors_username) EditText _username;
3439
@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;
3544

3645
@Override
3746
public void onCreate(Bundle savedInstanceState) {
3847
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();
4449
}
4550

4651
@Override
4752
public View onCreateView(LayoutInflater inflater,
4853
@Nullable ViewGroup container,
4954
@Nullable Bundle savedInstanceState) {
55+
5056
View layout = inflater.inflate(R.layout.fragment_retrofit, container, false);
5157
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+
5266
return layout;
5367
}
5468

5569
@OnClick(R.id.btn_demo_retrofit_contributors)
5670
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())
5876
.subscribe(new Observer<List<Contributor>>() {
5977
@Override
6078
public void onCompleted() {
@@ -69,67 +87,108 @@ public void onError(Throwable e) {
6987
@Override
7088
public void onNext(List<Contributor> contributors) {
7189
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+
7295
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());
7699
}
77100
}
78101
});
79102
}
80103

81104
@OnClick(R.id.btn_demo_retrofit_contributors_with_user_info)
82105
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())
84109
.flatMap(new Func1<List<Contributor>, Observable<Contributor>>() {
85110
@Override
86111
public Observable<Contributor> call(List<Contributor> contributors) {
87112
return Observable.from(contributors);
88113
}
89114
})
90-
.flatMap(new Func1<Contributor, Observable<?>>() {
115+
.flatMap(new Func1<Contributor, Observable<Pair<User, Contributor>>>() {
91116
@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+
});
113134
}
114135
})
115136
.subscribeOn(Schedulers.newThread())
116137
.observeOn(AndroidSchedulers.mainThread())
117-
.subscribe(new Observer<Object>() {
138+
.subscribe(new Observer<Pair<User, Contributor>>() {
118139
@Override
119140
public void onCompleted() {
120141
Timber.d("Retrofit call 2 completed ");
121142
}
122143

123144
@Override
124145
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");
127147
}
128148

129149
@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());
132171
}
133172
});
134173
}
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+
}
135194
}

app/src/main/res/layout/fragment_retrofit.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,10 @@
7474
android:hint="reponame"/>
7575
</LinearLayout>
7676

77+
<ListView
78+
android:id="@+id/log_list"
79+
android:layout_width="match_parent"
80+
android:layout_height="match_parent">
81+
</ListView>
82+
7783
</LinearLayout>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<string name="app_name">Android-RxJava</string>
55
<string name="hello_world">Hello world!</string>
66
<string name="action_settings">Settings</string>
7+
<string name="github_oauth_token"><!-- InsertYouroAuthTokenHere --></string>
78

89
<string name="btn_demo_schedulers">bg work (schedulers &amp; concurrency)</string>
910
<string name="btn_demo_buffer">accumulate calls (buffer)</string>
@@ -24,5 +25,4 @@
2425
<string name="msg_demo_polling">This is demo of polling or making a call repeatedly with RxJava. \n\nSimple polling: Notice in the logs how a network call (simulated) is repeatedly made in the background.</string>
2526
<string name="msg_demo_rxbus_1">Tap on the below button and RxBus will listen to the events</string>
2627
<string name="msg_demo_form_comb_latest">Monitor the state of multiple observables with the combineLatest operator. The submit button uses combineLatest to monitor validity of each of the 3 inputs. Only after the 3 inputs contain valid inputs will the submit button be enabled</string>
27-
2828
</resources>

0 commit comments

Comments
 (0)