diff --git a/app/src/main/java/com/monke/monkeybook/widget/page/LocalPageLoader.java b/app/src/main/java/com/monke/monkeybook/widget/page/LocalPageLoader.java index 7484e5a6d4..c68aafd96b 100644 --- a/app/src/main/java/com/monke/monkeybook/widget/page/LocalPageLoader.java +++ b/app/src/main/java/com/monke/monkeybook/widget/page/LocalPageLoader.java @@ -60,8 +60,6 @@ public class LocalPageLoader extends PageLoader { //编码类型 private Charset mCharset; - private Disposable mChapterDisp = null; - LocalPageLoader(PageView pageView) { super(pageView); } @@ -297,15 +295,6 @@ private boolean checkChapterType(RandomAccessFile bookStream) throws IOException return false; } - @Override - public void closeBook() { - super.closeBook(); - if (mChapterDisp != null) { - mChapterDisp.dispose(); - mChapterDisp = null; - } - } - @Override public void refreshChapterList() { Observable.create((ObservableOnSubscribe) e -> { @@ -353,16 +342,14 @@ private void loadChapterList() { // 通过RxJava异步处理分章事件 Single.create((SingleOnSubscribe>) e -> e.onSuccess(loadChapters())) .compose(RxUtils::toSimpleSingle) - .compose(mPageView.getActivity().bindUntilEvent(ActivityEvent.DESTROY)) .subscribe(new SingleObserver>() { @Override public void onSubscribe(Disposable d) { - mChapterDisp = d; + compositeDisposable.add(d); } @Override public void onSuccess(List value) { - mChapterDisp = null; isChapterListPrepare = true; // 存储章节到数据库 diff --git a/app/src/main/java/com/monke/monkeybook/widget/page/NetPageLoader.java b/app/src/main/java/com/monke/monkeybook/widget/page/NetPageLoader.java index 3765df569b..a3d78e2baa 100644 --- a/app/src/main/java/com/monke/monkeybook/widget/page/NetPageLoader.java +++ b/app/src/main/java/com/monke/monkeybook/widget/page/NetPageLoader.java @@ -4,7 +4,6 @@ import android.os.Handler; import com.monke.basemvplib.BaseActivity; -import com.monke.monkeybook.base.observer.SimpleObserver; import com.monke.monkeybook.bean.BookContentBean; import com.monke.monkeybook.bean.BookShelfBean; import com.monke.monkeybook.bean.ChapterListBean; @@ -70,8 +69,12 @@ private void loadChapterList() { WebBookModelImpl.getInstance().getChapterList(getBook()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) - .compose(mPageView.getActivity().bindUntilEvent(ActivityEvent.DESTROY)) - .subscribe(new SimpleObserver() { + .subscribe(new Observer() { + @Override + public void onSubscribe(Disposable d) { + compositeDisposable.add(d); + } + @Override public void onNext(BookShelfBean bookShelfBean) { isChapterListPrepare = true; @@ -95,6 +98,11 @@ public void onNext(BookShelfBean bookShelfBean) { public void onError(Throwable e) { chapterError(e.getMessage()); } + + @Override + public void onComplete() { + + } }); } @@ -116,6 +124,7 @@ chapterIndex, getBook().getChapterList(chapterIndex).getDurChapterName()) .subscribe(new Observer() { @Override public void onSubscribe(Disposable d) { + compositeDisposable.add(d); handler.postDelayed(() -> { DownloadingList(listHandle.REMOVE, getBook().getChapterList(chapterIndex).getDurChapterUrl()); d.dispose(); diff --git a/app/src/main/java/com/monke/monkeybook/widget/page/PageLoader.java b/app/src/main/java/com/monke/monkeybook/widget/page/PageLoader.java index 7cea8375ee..d13aa257cc 100644 --- a/app/src/main/java/com/monke/monkeybook/widget/page/PageLoader.java +++ b/app/src/main/java/com/monke/monkeybook/widget/page/PageLoader.java @@ -34,6 +34,7 @@ import io.reactivex.Single; import io.reactivex.SingleObserver; import io.reactivex.SingleOnSubscribe; +import io.reactivex.disposables.CompositeDisposable; import io.reactivex.disposables.Disposable; /** @@ -111,9 +112,7 @@ public abstract class PageLoader { int mCurChapterPos; int mCurPagePos; - private Disposable curDisposable; - private Disposable prevDisposable; - private Disposable nextDisposable; + CompositeDisposable compositeDisposable; //翻页时间 private long skipPageTime = 0; @@ -123,7 +122,7 @@ public abstract class PageLoader { mContext = pageView.getContext(); mCurChapterPos = getBook().getDurChapter(); mCurPagePos = getBook().getDurChapterPage(); - + compositeDisposable = new CompositeDisposable(); // 初始化数据 initData(); // 初始化画笔 @@ -956,15 +955,12 @@ boolean hasNext(int pageOnCur) { */ void parseCurChapter() { if (mCurChapter.getStatus() != Enum.PageStatus.FINISH) { - if (curDisposable != null) { - curDisposable.dispose(); - } Single.create((SingleOnSubscribe) e -> e.onSuccess(dealLoadPageList(mCurChapterPos))) .compose(RxUtils::toSimpleSingle) .subscribe(new SingleObserver() { @Override public void onSubscribe(Disposable d) { - curDisposable = d; + compositeDisposable.add(d); } @Override @@ -995,15 +991,12 @@ void parsePrevChapter() { if (mPreChapter.getStatus() == Enum.PageStatus.FINISH || prevChapterPos < 0) { return; } - if (prevDisposable != null) { - prevDisposable.dispose(); - } Single.create((SingleOnSubscribe) e -> e.onSuccess(dealLoadPageList(prevChapterPos))) .compose(RxUtils::toSimpleSingle) .subscribe(new SingleObserver() { @Override public void onSubscribe(Disposable d) { - prevDisposable = d; + compositeDisposable.add(d); } @Override @@ -1031,15 +1024,12 @@ void parseNextChapter() { if (mNextChapter.getStatus() == Enum.PageStatus.FINISH || nextChapterPos >= getBook().getChapterList().size()) { return; } - if (nextDisposable != null) { - nextDisposable.dispose(); - } Single.create((SingleOnSubscribe) e -> e.onSuccess(dealLoadPageList(nextChapterPos))) .compose(RxUtils::toSimpleSingle) .subscribe(new SingleObserver() { @Override public void onSubscribe(Disposable d) { - nextDisposable = d; + compositeDisposable.add(d); } @Override @@ -1281,15 +1271,8 @@ BookShelfBean getBook() { * 关闭书本 */ public void closeBook() { - if (curDisposable != null) { - curDisposable.dispose(); - } - if (prevDisposable != null) { - prevDisposable.dispose(); - } - if (nextDisposable != null) { - nextDisposable.dispose(); - } + compositeDisposable.dispose(); + compositeDisposable = null; isChapterListPrepare = false; isClose = true; @@ -1298,7 +1281,6 @@ public void closeBook() { mCurChapter = null; mNextChapter = null; mPageView = null; - } public boolean isClose() {