Skip to content
This repository has been archived by the owner on Oct 17, 2023. It is now read-only.

Commit

Permalink
支持epub阅读(仅支持文字)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengyuecanzhu committed Jun 14, 2021
1 parent 891770a commit 72acb8e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
skipToChapterAndPage(chapterAndPage[0], chapterAndPage[1]);
} catch (Exception e) {
ToastUtils.showError("章节跳转失败,请截图联系作者。\n" +
ToastUtils.showError("章节跳转失败\n" +
e.getMessage());
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private void initView(int postion, final ViewHolder viewHolder) {
final Chapter chapter = getItem(postion);
// viewHolder.tvTitle.setText("【" + chapter.getTitle() + "】");
viewHolder.tvTitle.setText(chapter.getTitle());
if (ChapterService.isChapterCached(chapter.getBookId(), chapter.getTitle()) || chapter.getEnd() > 0 ) {
if (ChapterService.isChapterCached(chapter.getBookId(), chapter.getTitle()) || chapter.getEnd() > 0) {
viewHolder.tvTitle.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(getContext(),R.drawable.selector_category_load), null, null, null);
} else {
viewHolder.tvTitle.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(getContext(),R.drawable.selector_category_unload), null, null, null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package xyz.fycz.myreader.widget.page;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.text.TextUtils;
import android.util.Log;


import net.sf.jazzlib.ZipFile;

import org.jsoup.Jsoup;
Expand All @@ -18,6 +14,7 @@
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
Expand All @@ -27,7 +24,6 @@

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
Expand Down Expand Up @@ -65,6 +61,8 @@ public void refreshChapterList() {

Observable.create((ObservableOnSubscribe<xyz.fycz.myreader.greendao.entity.Book>) e -> {
File bookFile = new File(mCollBook.getChapterUrl());
if (!bookFile.exists())
e.onError(new FileNotFoundException("书籍源文件不存在\n" + mCollBook.getChapterUrl()));
epubBook = readBook(bookFile);

if (epubBook == null) {
Expand All @@ -76,7 +74,7 @@ public void refreshChapterList() {
}
mCharset = Charset.forName(mCollBook.getInfoUrl());

if (TextUtils.isEmpty(mCollBook.getImgUrl())){
if (TextUtils.isEmpty(mCollBook.getImgUrl())) {
saveCover();
}
e.onNext(mCollBook);
Expand All @@ -99,7 +97,7 @@ public void onNext(xyz.fycz.myreader.greendao.entity.Book bookShelfBean) {

@Override
public void onError(Throwable e) {
chapterError(e.getMessage());
error(STATUS_CATEGORY_ERROR, e.getMessage());
}
});
}
Expand Down Expand Up @@ -157,7 +155,9 @@ public void drawCover(Canvas canvas, float top) {

private List<Chapter> loadChapters() {
Metadata metadata = epubBook.getMetadata();
mCollBook.setName(metadata.getFirstTitle());
if (!TextUtils.isEmpty(metadata.getFirstTitle())) {
mCollBook.setName(metadata.getFirstTitle());
}
if (metadata.getAuthors().size() > 0) {
String author = metadata.getAuthors().get(0).toString().replaceAll("^, |, $", "");
mCollBook.setAuthor(author);
Expand Down Expand Up @@ -192,12 +192,14 @@ private List<Chapter> loadChapters() {
} else {
bean.setTitle(title);
}
bean.setEnd(1);
mChapterList.add(bean);
}
} else {
parseMenu(refs, 0);
for (int i = 0; i < mChapterList.size(); i++) {
mChapterList.get(i).setNumber(i);
mChapterList.get(i).setEnd(1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ public LocalPageLoader(PageView pageView, Book collBook, ChapterService mChapter
public void refreshChapterList() {
// 对于文件是否存在,或者为空的判断,不作处理。 ==> 在文件打开前处理过了。
mBookFile = new File(mCollBook.getChapterUrl());

if (!mBookFile.exists()) {
error(STATUS_CATEGORY_ERROR, "书籍源文件不存在\n" + mCollBook.getChapterUrl());
return;
}
mCharset = mCollBook.getInfoUrl();

// 判断文件是否已经加载过,并具有缓存
Expand Down Expand Up @@ -113,7 +116,7 @@ public void onSuccess(List<Chapter> chapters) {

@Override
public void onError(Throwable e) {
chapterError(e.getLocalizedMessage());
error(STATUS_CATEGORY_ERROR, e.getLocalizedMessage());
Log.d(TAG, "file load error:" + e.toString());
}
});
Expand Down Expand Up @@ -366,7 +369,6 @@ private boolean checkChapterType(RandomAccessFile bookStream) throws IOException
}



@Override
protected BufferedReader getChapterReader(Chapter chapter) throws Exception {
if (chapter.getEnd() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public void refreshChapterList() {
chapter.setId(StringHelper.getStringRandom(25));
chapter.setBookId(mCollBook.getId());
}
mChapterService.addChapters(newChapters);
emitter.onNext(newChapters);
}))
.compose(RxUtils::toSimpleSingle)
Expand All @@ -94,7 +95,7 @@ public void onNext(@NotNull List<Chapter> chapters) {

@Override
public void onError(Throwable e) {
chapterError(e.getLocalizedMessage());
error(STATUS_CATEGORY_ERROR, e.getLocalizedMessage());
Log.d(TAG, "file load error:" + e.toString());
}
});
Expand Down
13 changes: 11 additions & 2 deletions app/src/main/java/xyz/fycz/myreader/widget/page/PageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import xyz.fycz.myreader.util.IOUtils;
import xyz.fycz.myreader.util.ToastUtils;
import xyz.fycz.myreader.util.help.ChapterContentHelp;
import xyz.fycz.myreader.util.help.StringHelper;
import xyz.fycz.myreader.util.utils.BitmapUtil;
import xyz.fycz.myreader.util.utils.MeUtils;
import xyz.fycz.myreader.util.utils.RxUtils;
Expand Down Expand Up @@ -62,6 +63,7 @@ public abstract class PageLoader {
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 STATUS_CATEGORY_ERROR = 9; // 获取目录失败

private String errorMsg = "";
// 默认的显示参数配置
Expand Down Expand Up @@ -780,8 +782,12 @@ protected void openChapterInLastPage() {
}

public void chapterError(String msg) {
error(STATUS_ERROR, msg);
}

public void error(int status, String msg){
//加载错误
mStatus = STATUS_ERROR;
mStatus = status;
errorMsg = msg;
mPageView.drawCurPage(false);
}
Expand Down Expand Up @@ -1046,8 +1052,11 @@ private void drawContent(Bitmap bitmap) {
case STATUS_CATEGORY_EMPTY:
tip = "目录列表为空";
break;
case STATUS_CATEGORY_ERROR:
tip = "目录加载失败\n" + errorMsg;
break;
}
if (mStatus == STATUS_ERROR) {
if (mStatus == STATUS_ERROR || mStatus == STATUS_CATEGORY_ERROR) {
drawErrorMsg(canvas, tip, 0);
} else {
//将提示语句放到正中间
Expand Down

0 comments on commit 72acb8e

Please sign in to comment.