33import android .os .Bundle ;
44import android .support .annotation .Nullable ;
55import android .support .v4 .app .Fragment ;
6+ import android .util .Pair ;
67import android .view .LayoutInflater ;
78import android .view .View ;
89import android .view .ViewGroup ;
10+ import android .widget .ArrayAdapter ;
911import android .widget .EditText ;
12+ import android .widget .ListView ;
1013import butterknife .ButterKnife ;
1114import butterknife .InjectView ;
1215import butterknife .OnClick ;
13- import com .google .common .base .Strings ;
1416import com .morihacky .android .rxjava .app .R ;
1517import com .morihacky .android .rxjava .retrofit .Contributor ;
1618import com .morihacky .android .rxjava .retrofit .GithubApi ;
1719import com .morihacky .android .rxjava .retrofit .User ;
20+ import java .util .ArrayList ;
1821import java .util .List ;
22+ import retrofit .RequestInterceptor ;
1923import retrofit .RestAdapter ;
2024import rx .Observable ;
2125import rx .Observer ;
2529import rx .schedulers .Schedulers ;
2630import timber .log .Timber ;
2731
32+ import static com .google .common .base .Strings .isNullOrEmpty ;
33+ import static java .lang .String .format ;
34+
2835public 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}
0 commit comments