|
13 | 13 | import butterknife.ButterKnife; |
14 | 14 | import butterknife.InjectView; |
15 | 15 | import butterknife.OnClick; |
16 | | -import com.morihacky.android.rxjava.R; |
17 | 16 | import com.morihacky.android.rxjava.retrofit.Contributor; |
18 | 17 | import com.morihacky.android.rxjava.retrofit.GithubApi; |
19 | 18 | import com.morihacky.android.rxjava.retrofit.User; |
|
27 | 26 | import rx.functions.Func1; |
28 | 27 | import rx.functions.Func2; |
29 | 28 | import rx.schedulers.Schedulers; |
| 29 | +import rx.subscriptions.CompositeSubscription; |
30 | 30 | import timber.log.Timber; |
31 | 31 |
|
32 | 32 | import static com.google.common.base.Strings.isNullOrEmpty; |
@@ -41,13 +41,27 @@ public class RetrofitFragment |
41 | 41 |
|
42 | 42 | private GithubApi _api; |
43 | 43 | private ArrayAdapter<String> _adapter; |
| 44 | + private CompositeSubscription _subscriptions = new CompositeSubscription(); |
44 | 45 |
|
45 | 46 | @Override |
46 | 47 | public void onCreate(Bundle savedInstanceState) { |
47 | 48 | super.onCreate(savedInstanceState); |
48 | 49 | _api = _createGithubApi(); |
49 | 50 | } |
50 | 51 |
|
| 52 | + @Override |
| 53 | + public void onResume() { |
| 54 | + super.onResume(); |
| 55 | + _subscriptions = RxUtils.getNewCompositeSubIfUnsubscribed(_subscriptions); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void onPause() { |
| 60 | + super.onPause(); |
| 61 | + |
| 62 | + RxUtils.unsubscribeIfNotNull(_subscriptions); |
| 63 | + } |
| 64 | + |
51 | 65 | @Override |
52 | 66 | public View onCreateView(LayoutInflater inflater, |
53 | 67 | @Nullable ViewGroup container, |
@@ -70,42 +84,45 @@ public View onCreateView(LayoutInflater inflater, |
70 | 84 | public void onListContributorsClicked() { |
71 | 85 | _adapter.clear(); |
72 | 86 |
|
73 | | - _api.contributors(_username.getText().toString(), _repo.getText().toString()) |
74 | | - .subscribeOn(Schedulers.io()) |
75 | | - .observeOn(AndroidSchedulers.mainThread()) |
76 | | - .subscribe(new Observer<List<Contributor>>() { |
77 | | - @Override |
78 | | - public void onCompleted() { |
79 | | - Timber.d("Retrofit call 1 completed"); |
80 | | - } |
81 | | - |
82 | | - @Override |
83 | | - public void onError(Throwable e) { |
84 | | - Timber.e(e, "woops we got an error while getting the list of contributors"); |
85 | | - } |
86 | | - |
87 | | - @Override |
88 | | - public void onNext(List<Contributor> contributors) { |
89 | | - 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 | | - |
95 | | - Timber.d("%s has made %d contributions to %s", |
96 | | - c.login, |
97 | | - c.contributions, |
98 | | - _repo.getText().toString()); |
99 | | - } |
100 | | - } |
101 | | - }); |
| 87 | + _subscriptions.add(// |
| 88 | + _api.contributors(_username.getText().toString(), _repo.getText().toString()) |
| 89 | + .subscribeOn(Schedulers.io()) |
| 90 | + .observeOn(AndroidSchedulers.mainThread()) |
| 91 | + .subscribe(new Observer<List<Contributor>>() { |
| 92 | + @Override |
| 93 | + public void onCompleted() { |
| 94 | + Timber.d("Retrofit call 1 completed"); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void onError(Throwable e) { |
| 99 | + Timber.e(e, |
| 100 | + "woops we got an error while getting the list of contributors"); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void onNext(List<Contributor> contributors) { |
| 105 | + for (Contributor c : contributors) { |
| 106 | + _adapter.add(format("%s has made %d contributions to %s", |
| 107 | + c.login, |
| 108 | + c.contributions, |
| 109 | + _repo.getText().toString())); |
| 110 | + |
| 111 | + Timber.d("%s has made %d contributions to %s", |
| 112 | + c.login, |
| 113 | + c.contributions, |
| 114 | + _repo.getText().toString()); |
| 115 | + } |
| 116 | + } |
| 117 | + })); |
102 | 118 | } |
103 | 119 |
|
104 | 120 | @OnClick(R.id.btn_demo_retrofit_contributors_with_user_info) |
105 | 121 | public void onListContributorsWithFullUserInfoClicked() { |
106 | 122 | _adapter.clear(); |
107 | 123 |
|
108 | | - _api.contributors(_username.getText().toString(), _repo.getText().toString()) |
| 124 | + _subscriptions.add(_api.contributors(_username.getText().toString(), |
| 125 | + _repo.getText().toString()) |
109 | 126 | .flatMap(new Func1<List<Contributor>, Observable<Contributor>>() { |
110 | 127 | @Override |
111 | 128 | public Observable<Contributor> call(List<Contributor> contributors) { |
@@ -167,7 +184,7 @@ public void onNext(Pair<User, Contributor> pair) { |
167 | 184 | contributor.contributions, |
168 | 185 | _repo.getText().toString()); |
169 | 186 | } |
170 | | - }); |
| 187 | + })); |
171 | 188 | } |
172 | 189 |
|
173 | 190 | // ----------------------------------------------------------------------------------- |
|