Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
gedoor committed Oct 31, 2018
1 parent 0cf18f7 commit 20abbdf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ public class LocalPageLoader extends PageLoader {
//编码类型
private Charset mCharset;

private Disposable mChapterDisp = null;

LocalPageLoader(PageView pageView) {
super(pageView);
}
Expand Down Expand Up @@ -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<Boolean>) e -> {
Expand Down Expand Up @@ -353,16 +342,14 @@ private void loadChapterList() {
// 通过RxJava异步处理分章事件
Single.create((SingleOnSubscribe<List<ChapterListBean>>) e -> e.onSuccess(loadChapters()))
.compose(RxUtils::toSimpleSingle)
.compose(mPageView.getActivity().bindUntilEvent(ActivityEvent.DESTROY))
.subscribe(new SingleObserver<List<ChapterListBean>>() {
@Override
public void onSubscribe(Disposable d) {
mChapterDisp = d;
compositeDisposable.add(d);
}

@Override
public void onSuccess(List<ChapterListBean> value) {
mChapterDisp = null;
isChapterListPrepare = true;

// 存储章节到数据库
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<BookShelfBean>() {
.subscribe(new Observer<BookShelfBean>() {
@Override
public void onSubscribe(Disposable d) {
compositeDisposable.add(d);
}

@Override
public void onNext(BookShelfBean bookShelfBean) {
isChapterListPrepare = true;
Expand All @@ -95,6 +98,11 @@ public void onNext(BookShelfBean bookShelfBean) {
public void onError(Throwable e) {
chapterError(e.getMessage());
}

@Override
public void onComplete() {

}
});
}

Expand All @@ -116,6 +124,7 @@ chapterIndex, getBook().getChapterList(chapterIndex).getDurChapterName())
.subscribe(new Observer<BookContentBean>() {
@Override
public void onSubscribe(Disposable d) {
compositeDisposable.add(d);
handler.postDelayed(() -> {
DownloadingList(listHandle.REMOVE, getBook().getChapterList(chapterIndex).getDurChapterUrl());
d.dispose();
Expand Down
34 changes: 8 additions & 26 deletions app/src/main/java/com/monke/monkeybook/widget/page/PageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;

Expand All @@ -123,7 +122,7 @@ public abstract class PageLoader {
mContext = pageView.getContext();
mCurChapterPos = getBook().getDurChapter();
mCurPagePos = getBook().getDurChapterPage();

compositeDisposable = new CompositeDisposable();
// 初始化数据
initData();
// 初始化画笔
Expand Down Expand Up @@ -956,15 +955,12 @@ boolean hasNext(int pageOnCur) {
*/
void parseCurChapter() {
if (mCurChapter.getStatus() != Enum.PageStatus.FINISH) {
if (curDisposable != null) {
curDisposable.dispose();
}
Single.create((SingleOnSubscribe<TxtChapter>) e -> e.onSuccess(dealLoadPageList(mCurChapterPos)))
.compose(RxUtils::toSimpleSingle)
.subscribe(new SingleObserver<TxtChapter>() {
@Override
public void onSubscribe(Disposable d) {
curDisposable = d;
compositeDisposable.add(d);
}

@Override
Expand Down Expand Up @@ -995,15 +991,12 @@ void parsePrevChapter() {
if (mPreChapter.getStatus() == Enum.PageStatus.FINISH || prevChapterPos < 0) {
return;
}
if (prevDisposable != null) {
prevDisposable.dispose();
}
Single.create((SingleOnSubscribe<TxtChapter>) e -> e.onSuccess(dealLoadPageList(prevChapterPos)))
.compose(RxUtils::toSimpleSingle)
.subscribe(new SingleObserver<TxtChapter>() {
@Override
public void onSubscribe(Disposable d) {
prevDisposable = d;
compositeDisposable.add(d);
}

@Override
Expand Down Expand Up @@ -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<TxtChapter>) e -> e.onSuccess(dealLoadPageList(nextChapterPos)))
.compose(RxUtils::toSimpleSingle)
.subscribe(new SingleObserver<TxtChapter>() {
@Override
public void onSubscribe(Disposable d) {
nextDisposable = d;
compositeDisposable.add(d);
}

@Override
Expand Down Expand Up @@ -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;
Expand All @@ -1298,7 +1281,6 @@ public void closeBook() {
mCurChapter = null;
mNextChapter = null;
mPageView = null;

}

public boolean isClose() {
Expand Down

0 comments on commit 20abbdf

Please sign in to comment.