From b8f9e12b0723c80d3c0bfc56b0b7403369dc776c Mon Sep 17 00:00:00 2001 From: fengyuecanzhu <1021300691@qq.com> Date: Sun, 27 Mar 2022 18:19:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AD=98=E5=9C=A8=E5=90=8C?= =?UTF-8?q?=E5=90=8D=E7=AB=A0=E8=8A=82=E6=97=B6=E5=90=8E=E9=9D=A2=E7=AB=A0?= =?UTF-8?q?=E8=8A=82=E5=86=85=E5=AE=B9=E5=8A=A0=E8=BD=BD=E5=89=8D=E9=9D=A2?= =?UTF-8?q?=E7=AB=A0=E8=8A=82=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../myreader/experiment/BookWCEstimate.kt | 4 +- .../myreader/greendao/entity/Chapter.java | 7 +-- .../greendao/service/ChapterService.java | 63 ++++++++++++++----- .../fycz/myreader/model/SearchWordEngine.kt | 2 +- .../myreader/ui/adapter/BookMarkAdapter.java | 2 +- .../myreader/ui/adapter/BookcaseAdapter.java | 2 +- .../ui/adapter/ChapterTitleAdapter.java | 2 +- .../ui/adapter/holder/CatalogHolder.java | 2 +- .../myreader/widget/page/LocalPageLoader.java | 2 +- .../myreader/widget/page/NetPageLoader.java | 2 +- 10 files changed, 58 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/xyz/fycz/myreader/experiment/BookWCEstimate.kt b/app/src/main/java/xyz/fycz/myreader/experiment/BookWCEstimate.kt index 3c3f70a..148c33e 100644 --- a/app/src/main/java/xyz/fycz/myreader/experiment/BookWCEstimate.kt +++ b/app/src/main/java/xyz/fycz/myreader/experiment/BookWCEstimate.kt @@ -49,13 +49,13 @@ class BookWCEstimate { var cachedChapterSize = 0 for (chapter in chapters) { if (cachedChapterSize >= 20) break - if (ChapterService.isChapterCached(chapter.bookId, chapter.title)) { + if (ChapterService.isChapterCached(chapter)) { cachedChapterSize++ } } if ((cachedChapterSize < 20 && chapters.size > 50) || cachedChapterSize == 0) return -2 chapters.forEach { chapter -> - if (ChapterService.isChapterCached(chapter.bookId, chapter.title)) { + if (ChapterService.isChapterCached(chapter)) { sum += countChar( File( APPCONST.BOOK_CACHE_PATH + chapter.bookId diff --git a/app/src/main/java/xyz/fycz/myreader/greendao/entity/Chapter.java b/app/src/main/java/xyz/fycz/myreader/greendao/entity/Chapter.java index 912802f..a764c5d 100644 --- a/app/src/main/java/xyz/fycz/myreader/greendao/entity/Chapter.java +++ b/app/src/main/java/xyz/fycz/myreader/greendao/entity/Chapter.java @@ -17,6 +17,7 @@ import org.greenrobot.greendao.annotation.Id; import org.greenrobot.greendao.annotation.Transient; import xyz.fycz.myreader.common.APPCONST; +import xyz.fycz.myreader.greendao.service.ChapterService; import xyz.fycz.myreader.model.third3.analyzeRule.RuleDataInterface; import xyz.fycz.myreader.util.utils.FileUtils; import xyz.fycz.myreader.util.utils.GsonExtensionsKt; @@ -115,11 +116,9 @@ public class Chapter implements RuleDataInterface { public String getContent() { if (end > 0) return end + ""; - String filePath = APPCONST.BOOK_CACHE_PATH + bookId - + File.separator + title + FileUtils.SUFFIX_FY; - File file = new File(filePath); + File file = ChapterService.getChapterFile(this); if (file.exists() && file.length() > 0) { - this.content = filePath; + this.content = file.getAbsolutePath(); } else { this.content = null; } diff --git a/app/src/main/java/xyz/fycz/myreader/greendao/service/ChapterService.java b/app/src/main/java/xyz/fycz/myreader/greendao/service/ChapterService.java index a16bb44..5306265 100644 --- a/app/src/main/java/xyz/fycz/myreader/greendao/service/ChapterService.java +++ b/app/src/main/java/xyz/fycz/myreader/greendao/service/ChapterService.java @@ -3,6 +3,7 @@ package xyz.fycz.myreader.greendao.service; import android.database.Cursor; import xyz.fycz.myreader.common.APPCONST; +import xyz.fycz.myreader.greendao.entity.BookMark; import xyz.fycz.myreader.greendao.entity.Chapter; import xyz.fycz.myreader.greendao.gen.ChapterDao; import xyz.fycz.myreader.util.IOUtils; @@ -19,15 +20,16 @@ public class ChapterService extends BaseService { private static volatile ChapterService sInstance; public static ChapterService getInstance() { - if (sInstance == null){ - synchronized (ChapterService.class){ - if (sInstance == null){ + if (sInstance == null) { + synchronized (ChapterService.class) { + if (sInstance == null) { sInstance = new ChapterService(); } } } return sInstance; } + private List findChapters(String sql, String[] selectionArgs) { ArrayList chapters = new ArrayList<>(); try { @@ -188,11 +190,11 @@ public class ChapterService extends BaseService { return; } - File file = getBookFile(chapter.getBookId(), chapter.getTitle()); + File file = getChapterFileExisted(chapter); BufferedWriter bw = null; try { bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))); - bw.write(content.replace(chapter.getTitle(), "")); + bw.write(content); bw.flush(); } catch (IOException e) { e.printStackTrace(); @@ -207,25 +209,25 @@ public class ChapterService extends BaseService { * @param chapter */ public void deleteChapterCacheFile(Chapter chapter) { - File file = getBookFile(chapter.getBookId(), chapter.getTitle()); - file.delete(); + File file = getChapterFile(chapter); + if (file.exists()) file.delete(); } /** * 获取缓存章节内容 + * * @param chapter * @return */ - public String getChapterCatheContent(Chapter chapter){ - File file = new File(APPCONST.BOOK_CACHE_PATH + chapter.getBookId() - + File.separator + chapter.getTitle() + FileUtils.SUFFIX_FY); + public String getChapterCatheContent(Chapter chapter) { + File file = getChapterFile(chapter); if (!file.exists()) return null; BufferedReader br = null; try { br = new BufferedReader(new FileReader(file)); StringBuilder s = new StringBuilder(); String line = null; - while ((line = br.readLine()) != null){ + while ((line = br.readLine()) != null) { s.append(line); s.append("\n"); } @@ -233,7 +235,7 @@ public class ChapterService extends BaseService { } catch (IOException e) { e.printStackTrace(); return null; - }finally { + } finally { IOUtils.close(br); } } @@ -278,13 +280,20 @@ public class ChapterService extends BaseService { * 根据文件名判断是否被缓存过 (因为可能数据库显示被缓存过,但是文件中却没有的情况,所以需要根据文件判断是否被缓存 * 过) * - * @param folderName : bookId - * @param fileName: chapterName + * @param chapter : chapter * @return */ - public static boolean isChapterCached(String folderName, String fileName) { - File file = new File(APPCONST.BOOK_CACHE_PATH + folderName - + File.separator + fileName + FileUtils.SUFFIX_FY); + public static boolean isChapterCached(Chapter chapter) { + File file = getChapterFile(chapter); + return file.exists(); + } + + public static boolean isChapterCached(BookMark bookMark) { + Chapter chapter = new Chapter(); + chapter.setBookId(bookMark.getBookId()); + chapter.setNumber(bookMark.getBookMarkChapterNum()); + chapter.setTitle(bookMark.getTitle()); + File file = getChapterFile(chapter); return file.exists(); } @@ -326,4 +335,24 @@ public class ChapterService extends BaseService { + File.separator + fileName + FileUtils.SUFFIX_FY); } + public static File getChapterFile(Chapter chapter) { + File file = new File(APPCONST.BOOK_CACHE_PATH + chapter.getBookId() + + File.separator + chapter.getNumber() + "、" + chapter.getTitle() + FileUtils.SUFFIX_FY); + if (!file.exists()) { + file = new File(APPCONST.BOOK_CACHE_PATH + chapter.getBookId() + + File.separator + chapter.getTitle() + FileUtils.SUFFIX_FY); + } + return file; + } + + public static File getChapterFileExisted(Chapter chapter) { + File file = new File(APPCONST.BOOK_CACHE_PATH + chapter.getBookId() + + File.separator + chapter.getTitle() + FileUtils.SUFFIX_FY); + if (!file.exists()) { + file = FileUtils.getFile(APPCONST.BOOK_CACHE_PATH + chapter.getBookId() + + File.separator + chapter.getNumber() + "、" + chapter.getTitle() + FileUtils.SUFFIX_FY); + } + return file; + } + } diff --git a/app/src/main/java/xyz/fycz/myreader/model/SearchWordEngine.kt b/app/src/main/java/xyz/fycz/myreader/model/SearchWordEngine.kt index a3fd4ca..de557ef 100644 --- a/app/src/main/java/xyz/fycz/myreader/model/SearchWordEngine.kt +++ b/app/src/main/java/xyz/fycz/myreader/model/SearchWordEngine.kt @@ -108,7 +108,7 @@ class SearchWordEngine( chapterTitle = chapter.title, searchWord2List = mutableListOf() ) - if (!isLocalBook && !ChapterService.isChapterCached(book.id, chapter.title)) { + if (!isLocalBook && !ChapterService.isChapterCached(chapter)) { emitter.onNext(searchWord1) return@ObservableOnSubscribe } diff --git a/app/src/main/java/xyz/fycz/myreader/ui/adapter/BookMarkAdapter.java b/app/src/main/java/xyz/fycz/myreader/ui/adapter/BookMarkAdapter.java index 96c3418..03397ae 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/adapter/BookMarkAdapter.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/adapter/BookMarkAdapter.java @@ -62,7 +62,7 @@ public class BookMarkAdapter extends ArrayAdapter { final BookMark bookMark = getItem(postion); assert bookMark != null; viewHolder.tvTitle.setText(String.format("%s[%s]", bookMark.getTitle(), bookMark.getBookMarkReadPosition() + 1)); - if (ChapterService.isChapterCached(bookMark.getBookId(), bookMark.getTitle())){ + if (ChapterService.isChapterCached(bookMark)){ 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); diff --git a/app/src/main/java/xyz/fycz/myreader/ui/adapter/BookcaseAdapter.java b/app/src/main/java/xyz/fycz/myreader/ui/adapter/BookcaseAdapter.java index bad68e0..c0b1a6d 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/adapter/BookcaseAdapter.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/adapter/BookcaseAdapter.java @@ -223,7 +223,7 @@ public abstract class BookcaseAdapter extends RecyclerView.Adapter { 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) || 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); diff --git a/app/src/main/java/xyz/fycz/myreader/ui/adapter/holder/CatalogHolder.java b/app/src/main/java/xyz/fycz/myreader/ui/adapter/holder/CatalogHolder.java index 2ebf079..4982062 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/adapter/holder/CatalogHolder.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/adapter/holder/CatalogHolder.java @@ -28,7 +28,7 @@ public class CatalogHolder extends ViewHolderImpl { @Override public void onBind(RecyclerView.ViewHolder holder, Chapter data, int pos) { - if (ChapterService.isChapterCached(data.getBookId(), data.getTitle()) || data.getEnd() > 0) { + if (ChapterService.isChapterCached(data) || data.getEnd() > 0) { tvTitle.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(getContext(),R.drawable.selector_category_load), null, null, null); } else { tvTitle.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(getContext(),R.drawable.selector_category_unload), null, null, null); diff --git a/app/src/main/java/xyz/fycz/myreader/widget/page/LocalPageLoader.java b/app/src/main/java/xyz/fycz/myreader/widget/page/LocalPageLoader.java index 8a7b3a5..c662eb2 100644 --- a/app/src/main/java/xyz/fycz/myreader/widget/page/LocalPageLoader.java +++ b/app/src/main/java/xyz/fycz/myreader/widget/page/LocalPageLoader.java @@ -391,7 +391,7 @@ public class LocalPageLoader extends PageLoader { @Override public boolean hasChapterData(Chapter chapter) { - return chapter.getEnd() > 0 || ChapterService.isChapterCached(mCollBook.getId(), chapter.getTitle()); + return chapter.getEnd() > 0 || ChapterService.isChapterCached(chapter); } } 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 6974317..871b674 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 @@ -113,7 +113,7 @@ public class NetPageLoader extends PageLoader { @Override public boolean hasChapterData(Chapter chapter) { - return ChapterService.isChapterCached(mCollBook.getId(), chapter.getTitle()); + return ChapterService.isChapterCached(chapter); } // 装载上一章节的内容