33import android .os .Bundle ;
44import android .support .annotation .Nullable ;
55import android .support .v4 .app .Fragment ;
6+ import android .text .TextUtils ;
7+ import android .util .Base64 ;
68import android .view .LayoutInflater ;
79import android .view .View ;
810import android .view .ViewGroup ;
11+ import android .widget .ArrayAdapter ;
912import android .widget .EditText ;
10- import butterknife .ButterKnife ;
11- import butterknife .InjectView ;
12- import butterknife .OnClick ;
13+ import android .widget .ListView ;
14+
1315import 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+
21+ import java .util .ArrayList ;
1822import java .util .List ;
23+
24+ import butterknife .ButterKnife ;
25+ import butterknife .InjectView ;
26+ import butterknife .OnClick ;
27+ import retrofit .RequestInterceptor ;
1928import retrofit .RestAdapter ;
2029import rx .Observable ;
2130import rx .Observer ;
2534import rx .schedulers .Schedulers ;
2635import timber .log .Timber ;
2736
28- public class RetrofitFragment
29- extends Fragment {
37+ public class RetrofitFragment extends Fragment {
38+
39+ private ArrayAdapter <String > _adapter ;
3040
3141 GithubApi api ;
3242
43+ @ InjectView (R .id .log_list ) ListView _resultsListView ;
3344 @ InjectView (R .id .demo_retrofit_contributors_username ) EditText _username ;
3445 @ InjectView (R .id .demo_retrofit_contributors_repository ) EditText _repo ;
3546
3647 @ Override
3748 public void onCreate (Bundle savedInstanceState ) {
3849 super .onCreate (savedInstanceState );
3950
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 ();
4269
4370 api = restAdapter .create (GithubApi .class );
4471 }
@@ -49,12 +76,22 @@ public View onCreateView(LayoutInflater inflater,
4976 @ Nullable Bundle savedInstanceState ) {
5077 View layout = inflater .inflate (R .layout .fragment_retrofit , container , false );
5178 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+
5286 return layout ;
5387 }
5488
5589 @ OnClick (R .id .btn_demo_retrofit_contributors )
5690 public void onListContributorsClicked () {
91+ _adapter .clear ();
5792 api .contributors (_username .getText ().toString (), _repo .getText ().toString ())
93+ .subscribeOn (Schedulers .io ())
94+ .observeOn (AndroidSchedulers .mainThread ())
5895 .subscribe (new Observer <List <Contributor >>() {
5996 @ Override
6097 public void onCompleted () {
@@ -69,17 +106,22 @@ public void onError(Throwable e) {
69106 @ Override
70107 public void onNext (List <Contributor > contributors ) {
71108 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 ()));
72113 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 ());
76117 }
77118 }
78119 });
79120 }
80121
81122 @ OnClick (R .id .btn_demo_retrofit_contributors_with_user_info )
82123 public void onListContributorsWithFullUserInfoClicked () {
124+ _adapter .clear ();
83125 api .contributors (_username .getText ().toString (), _repo .getText ().toString ())
84126 .flatMap (new Func1 <List <Contributor >, Observable <Contributor >>() {
85127 @ Override
@@ -91,24 +133,30 @@ public Observable<Contributor> call(List<Contributor> contributors) {
91133 @ Override
92134 public Observable <?> call (Contributor contributor ) {
93135 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 ();
112160 return Observable .empty ();
113161 }
114162 })
@@ -123,7 +171,7 @@ public void onCompleted() {
123171 @ Override
124172 public void onError (Throwable e ) {
125173 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" );
127175 }
128176
129177 @ Override
0 commit comments