Use BehaviorSubject instead of PublishSubject to observe page progress

This commit is contained in:
inorichi 2015-10-28 17:47:21 +01:00
parent e160f53bb2
commit cd456d2186
2 changed files with 11 additions and 14 deletions

View file

@ -1,7 +1,7 @@
package eu.kanade.mangafeed.data.models; package eu.kanade.mangafeed.data.models;
import eu.kanade.mangafeed.data.helpers.NetworkHelper; import eu.kanade.mangafeed.data.helpers.NetworkHelper;
import rx.subjects.PublishSubject; import rx.subjects.BehaviorSubject;
public class Page implements NetworkHelper.ProgressListener { public class Page implements NetworkHelper.ProgressListener {
@ -12,7 +12,7 @@ public class Page implements NetworkHelper.ProgressListener {
private transient int status; private transient int status;
private transient int progress; private transient int progress;
private transient PublishSubject<Integer> statusSubject; private transient BehaviorSubject<Integer> statusSubject;
public static final int QUEUE = 0; public static final int QUEUE = 0;
public static final int LOAD_PAGE = 1; public static final int LOAD_PAGE = 1;
@ -83,7 +83,7 @@ public class Page implements NetworkHelper.ProgressListener {
progress = (int) ((100 * bytesRead) / contentLength); progress = (int) ((100 * bytesRead) / contentLength);
} }
public void setStatusSubject(PublishSubject<Integer> subject) { public void setStatusSubject(BehaviorSubject<Integer> subject) {
this.statusSubject = subject; this.statusSubject = subject;
} }

View file

@ -24,7 +24,7 @@ import rx.Observable;
import rx.Subscription; import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers; import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers; import rx.schedulers.Schedulers;
import rx.subjects.PublishSubject; import rx.subjects.BehaviorSubject;
public class ReaderPageFragment extends Fragment { public class ReaderPageFragment extends Fragment {
@ -104,17 +104,18 @@ public class ReaderPageFragment extends Fragment {
private void processStatus(int status) { private void processStatus(int status) {
switch (status) { switch (status) {
case Page.READY:
showImage();
unsubscribeProgress();
unsubscribeStatus();
break;
case Page.LOAD_PAGE: case Page.LOAD_PAGE:
showLoading(); showLoading();
break; break;
case Page.DOWNLOAD_IMAGE: case Page.DOWNLOAD_IMAGE:
observeProgress();
showDownloading(); showDownloading();
break; break;
case Page.READY:
showImage();
unsubscribeProgress();
unsubscribeStatus();
break;
case Page.ERROR: case Page.ERROR:
showError(); showError();
unsubscribeProgress(); unsubscribeProgress();
@ -129,13 +130,9 @@ public class ReaderPageFragment extends Fragment {
if (page.getStatus() == Page.READY) { if (page.getStatus() == Page.READY) {
showImage(); showImage();
} else { } else {
processStatus(page.getStatus()); BehaviorSubject<Integer> statusSubject = BehaviorSubject.create();
PublishSubject<Integer> statusSubject = PublishSubject.create();
page.setStatusSubject(statusSubject); page.setStatusSubject(statusSubject);
observeProgress();
statusSubscription = statusSubject statusSubscription = statusSubject
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(this::processStatus); .subscribe(this::processStatus);