From 947a87bf257a17fb0c867b998eb62d3a5ad75d1c Mon Sep 17 00:00:00 2001 From: fengyuecanzhu <1021300691@qq.com> Date: Wed, 3 Feb 2021 22:00:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=98=85=E8=AF=BB=E4=BD=93?= =?UTF-8?q?=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../myreader/ui/activity/ReadActivity.java | 11 +--- .../myreader/widget/page/NetPageLoader.java | 58 +++++++------------ .../fycz/myreader/widget/page/PageLoader.java | 32 ++++++---- 3 files changed, 47 insertions(+), 54 deletions(-) diff --git a/app/src/main/java/xyz/fycz/myreader/ui/activity/ReadActivity.java b/app/src/main/java/xyz/fycz/myreader/ui/activity/ReadActivity.java index f7e1691..98942a9 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/activity/ReadActivity.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/activity/ReadActivity.java @@ -222,14 +222,7 @@ public class ReadActivity extends BaseActivity implements ColorPickerDialogListe } break; case 6: - mPageLoader.openChapter(); - if (mPageLoader.isPrev()) {//判断是否向前翻页打开章节,如果是则打开自己后跳转到最后一页,否则不跳转 - try {//概率性异常(空指针异常) - mPageLoader.skipToPage(mPageLoader.getAllPagePos() - 1); - } catch (Exception e) { - e.printStackTrace(); - } - } + mPageLoader.setmStatus(PageLoader.STATUS_LOADING); break; case 7: ToastUtils.showWarring("无网络连接!"); @@ -1000,9 +993,11 @@ public class ReadActivity extends BaseActivity implements ColorPickerDialogListe } }); } else { + mPageLoader.setmStatus(PageLoader.STATUS_LOADING_CHAPTER); CommonApi.getBookChapters(mBook.getChapterUrl(), mReadCrawler, false, new ResultCallback() { @Override public void onFinish(Object o, int code) { + mPageLoader.setmStatus(PageLoader.STATUS_LOADING); ArrayList chapters = (ArrayList) o; updateAllOldChapterData(chapters); initChapters(); diff --git a/app/src/main/java/xyz/fycz/myreader/widget/page/NetPageLoader.java b/app/src/main/java/xyz/fycz/myreader/widget/page/NetPageLoader.java index 720fe1d..b5a73b8 100644 --- a/app/src/main/java/xyz/fycz/myreader/widget/page/NetPageLoader.java +++ b/app/src/main/java/xyz/fycz/myreader/widget/page/NetPageLoader.java @@ -20,11 +20,6 @@ import java.io.*; import java.util.ArrayList; import java.util.List; -/** - * Created by newbiechen on 17-5-29. - * 网络页面加载器 - */ - public class NetPageLoader extends PageLoader { private static final String TAG = "PageFactory"; private ChapterService mChapterService; @@ -220,39 +215,30 @@ public class NetPageLoader extends PageLoader { * * @param chapter */ - public void getChapterContent(final Chapter chapter) { - Chapter cacheChapter = mChapterService.findChapterByBookIdAndTitle(chapter.getBookId(), chapter.getTitle()); - if (cacheChapter != null && hasChapterData(chapter)) { - chapter.setContent(APPCONST.BOOK_CACHE_PATH + chapter.getBookId() - + File.separator + chapter.getTitle() + FileUtils.SUFFIX_FY); - chapter.setId(cacheChapter.getId()); - mChapterService.saveOrUpdateChapter(chapter, null); - } else { - CommonApi.getChapterContent(chapter.getUrl(), mReadCrawler, new ResultCallback() { - @Override - public void onFinish(final Object o, int code) { -// chapter.setContent((String) o); - mChapterService.saveOrUpdateChapter(chapter, (String) o); - if (getPageStatus() == PageLoader.STATUS_LOADING) { - MyApplication.runOnUiThread(() -> { - if (isPrev) { - openChapterInLastPage(); - } else { - openChapter(); - } - }); - } - } - - @Override - public void onError(Exception e) { - ToastUtils.showError("章节加载失败\n" + e.getLocalizedMessage()); - e.printStackTrace(); + public void getChapterContent(Chapter chapter) { + CommonApi.getChapterContent(chapter.getUrl(), mReadCrawler, new ResultCallback() { + @Override + public void onFinish(final Object o, int code) { + mChapterService.saveOrUpdateChapter(chapter, (String) o); + if (getPageStatus() == PageLoader.STATUS_LOADING) { + MyApplication.runOnUiThread(() -> { + if (isPrev) { + openChapterInLastPage(); + } else { + openChapter(); + } + }); } + } - }); - } - + @Override + public void onError(Exception e) { + if (mCurChapterPos == chapter.getNumber()) + chapterError(); + e.printStackTrace(); + } + }); } + } diff --git a/app/src/main/java/xyz/fycz/myreader/widget/page/PageLoader.java b/app/src/main/java/xyz/fycz/myreader/widget/page/PageLoader.java index 643aa83..65a5827 100644 --- a/app/src/main/java/xyz/fycz/myreader/widget/page/PageLoader.java +++ b/app/src/main/java/xyz/fycz/myreader/widget/page/PageLoader.java @@ -55,12 +55,13 @@ public abstract class PageLoader { // 当前页面的状态 public static final int STATUS_LOADING = 1; // 正在加载 - public static final int STATUS_FINISH = 2; // 加载完成 - public static final int STATUS_ERROR = 3; // 加载错误 (一般是网络加载情况) - public static final int STATUS_EMPTY = 4; // 空数据 - public static final int STATUS_PARING = 5; // 正在解析 (装载本地数据) - public static final int STATUS_PARSE_ERROR = 6; // 本地文件解析错误(暂未被使用) - public static final int STATUS_CATEGORY_EMPTY = 7; // 获取到的目录为空 + public static final int STATUS_LOADING_CHAPTER = 2; // 正在加载目录 + public static final int STATUS_FINISH = 3; // 加载完成 + public static final int STATUS_ERROR = 4; // 加载错误 (一般是网络加载情况) + public static final int STATUS_EMPTY = 5; // 空数据 + public static final int STATUS_PARING = 6; // 正在解析 (装载本地数据) + public static final int STATUS_PARSE_ERROR = 7; // 本地文件解析错误(暂未被使用) + public static final int STATUS_CATEGORY_EMPTY = 8; // 获取到的目录为空 // 默认的显示参数配置 public static final int DEFAULT_MARGIN_HEIGHT = 28; public static final int DEFAULT_MARGIN_WIDTH = 15; @@ -721,7 +722,7 @@ public abstract class PageLoader { // 如果章节目录没有准备好 if (!isChapterListPrepare) { - mStatus = STATUS_LOADING; + mStatus = STATUS_LOADING_CHAPTER; mPageView.drawCurPage(false); return; } @@ -1007,13 +1008,16 @@ public abstract class PageLoader { String tip = ""; switch (mStatus) { case STATUS_LOADING: - tip = "正在拼命加载中..."; + tip = "正在加载章节内容..."; + break; + case STATUS_LOADING_CHAPTER: + tip = "正在加载目录列表..."; break; case STATUS_ERROR: - tip = "加载失败(点击边缘重试)"; + tip = "章节内容加载失败"; break; case STATUS_EMPTY: - tip = "文章内容为空"; + tip = "章节内容为空"; break; case STATUS_PARING: tip = "正在拆分章节请等待..."; @@ -1305,6 +1309,8 @@ public abstract class PageLoader { mCancelPage = mCurPage; if (parsePrevChapter()) { mCurPage = getPrevLastPage(); + if (mStatus == STATUS_LOADING) + mStatus = STATUS_FINISH; } else { mCurPage = new TxtPage(); } @@ -1378,6 +1384,8 @@ public abstract class PageLoader { // 解析下一章数据 if (parseNextChapter()) { mCurPage = mCurChapter.getPage(0); + if (mStatus == STATUS_LOADING) + mStatus = STATUS_FINISH; } else { mCurPage = new TxtPage(); } @@ -2000,6 +2008,10 @@ public abstract class PageLoader { isPrev = prev; } + public void setmStatus(int mStatus) { + this.mStatus = mStatus; + } + /*****************************************interface*****************************************/ public interface OnPageChangeListener {