GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: @mattlogan called me out for being lazy and not doing the right … · devqmr/RxJava-Android-Samples@481e175

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • app/src/main/java/com/morihacky/android/rxjava
    • RetrofitFragment.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: app/src/main/java/com/morihacky/android/rxjava/RetrofitFragment.java
+49-32Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import butterknife.ButterKnife;
1414
import butterknife.InjectView;
1515
import butterknife.OnClick;
16-
import com.morihacky.android.rxjava.R;
1716
import com.morihacky.android.rxjava.retrofit.Contributor;
1817
import com.morihacky.android.rxjava.retrofit.GithubApi;
1918
import com.morihacky.android.rxjava.retrofit.User;
@@ -27,6 +26,7 @@
2726
import rx.functions.Func1;
2827
import rx.functions.Func2;
2928
import rx.schedulers.Schedulers;
29+
import rx.subscriptions.CompositeSubscription;
3030
import timber.log.Timber;
3131

3232
import static com.google.common.base.Strings.isNullOrEmpty;
@@ -41,13 +41,27 @@ public class RetrofitFragment
4141

4242
private GithubApi _api;
4343
private ArrayAdapter<String> _adapter;
44+
private CompositeSubscription _subscriptions = new CompositeSubscription();
4445

4546
@Override
4647
public void onCreate(Bundle savedInstanceState) {
4748
super.onCreate(savedInstanceState);
4849
_api = _createGithubApi();
4950
}
5051

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+
5165
@Override
5266
public View onCreateView(LayoutInflater inflater,
5367
@Nullable ViewGroup container,
@@ -70,42 +84,45 @@ public View onCreateView(LayoutInflater inflater,
7084
public void onListContributorsClicked() {
7185
_adapter.clear();
7286

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+
}));
102118
}
103119

104120
@OnClick(R.id.btn_demo_retrofit_contributors_with_user_info)
105121
public void onListContributorsWithFullUserInfoClicked() {
106122
_adapter.clear();
107123

108-
_api.contributors(_username.getText().toString(), _repo.getText().toString())
124+
_subscriptions.add(_api.contributors(_username.getText().toString(),
125+
_repo.getText().toString())
109126
.flatMap(new Func1<List<Contributor>, Observable<Contributor>>() {
110127
@Override
111128
public Observable<Contributor> call(List<Contributor> contributors) {
@@ -167,7 +184,7 @@ public void onNext(Pair<User, Contributor> pair) {
167184
contributor.contributions,
168185
_repo.getText().toString());
169186
}
170-
});
187+
}));
171188
}
172189

173190
// -----------------------------------------------------------------------------------

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.