修复存在同名章节时后面章节内容加载前面章节的问题

pull/21/head
fengyuecanzhu 3 years ago
parent e565da5a6f
commit b8f9e12b07
  1. 4
      app/src/main/java/xyz/fycz/myreader/experiment/BookWCEstimate.kt
  2. 7
      app/src/main/java/xyz/fycz/myreader/greendao/entity/Chapter.java
  3. 63
      app/src/main/java/xyz/fycz/myreader/greendao/service/ChapterService.java
  4. 2
      app/src/main/java/xyz/fycz/myreader/model/SearchWordEngine.kt
  5. 2
      app/src/main/java/xyz/fycz/myreader/ui/adapter/BookMarkAdapter.java
  6. 2
      app/src/main/java/xyz/fycz/myreader/ui/adapter/BookcaseAdapter.java
  7. 2
      app/src/main/java/xyz/fycz/myreader/ui/adapter/ChapterTitleAdapter.java
  8. 2
      app/src/main/java/xyz/fycz/myreader/ui/adapter/holder/CatalogHolder.java
  9. 2
      app/src/main/java/xyz/fycz/myreader/widget/page/LocalPageLoader.java
  10. 2
      app/src/main/java/xyz/fycz/myreader/widget/page/NetPageLoader.java

@ -49,13 +49,13 @@ class BookWCEstimate {
var cachedChapterSize = 0 var cachedChapterSize = 0
for (chapter in chapters) { for (chapter in chapters) {
if (cachedChapterSize >= 20) break if (cachedChapterSize >= 20) break
if (ChapterService.isChapterCached(chapter.bookId, chapter.title)) { if (ChapterService.isChapterCached(chapter)) {
cachedChapterSize++ cachedChapterSize++
} }
} }
if ((cachedChapterSize < 20 && chapters.size > 50) || cachedChapterSize == 0) return -2 if ((cachedChapterSize < 20 && chapters.size > 50) || cachedChapterSize == 0) return -2
chapters.forEach { chapter -> chapters.forEach { chapter ->
if (ChapterService.isChapterCached(chapter.bookId, chapter.title)) { if (ChapterService.isChapterCached(chapter)) {
sum += countChar( sum += countChar(
File( File(
APPCONST.BOOK_CACHE_PATH + chapter.bookId APPCONST.BOOK_CACHE_PATH + chapter.bookId

@ -17,6 +17,7 @@ import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Transient; import org.greenrobot.greendao.annotation.Transient;
import xyz.fycz.myreader.common.APPCONST; 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.model.third3.analyzeRule.RuleDataInterface;
import xyz.fycz.myreader.util.utils.FileUtils; import xyz.fycz.myreader.util.utils.FileUtils;
import xyz.fycz.myreader.util.utils.GsonExtensionsKt; import xyz.fycz.myreader.util.utils.GsonExtensionsKt;
@ -115,11 +116,9 @@ public class Chapter implements RuleDataInterface {
public String getContent() { public String getContent() {
if (end > 0) return end + ""; if (end > 0) return end + "";
String filePath = APPCONST.BOOK_CACHE_PATH + bookId File file = ChapterService.getChapterFile(this);
+ File.separator + title + FileUtils.SUFFIX_FY;
File file = new File(filePath);
if (file.exists() && file.length() > 0) { if (file.exists() && file.length() > 0) {
this.content = filePath; this.content = file.getAbsolutePath();
} else { } else {
this.content = null; this.content = null;
} }

@ -3,6 +3,7 @@ package xyz.fycz.myreader.greendao.service;
import android.database.Cursor; import android.database.Cursor;
import xyz.fycz.myreader.common.APPCONST; 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.entity.Chapter;
import xyz.fycz.myreader.greendao.gen.ChapterDao; import xyz.fycz.myreader.greendao.gen.ChapterDao;
import xyz.fycz.myreader.util.IOUtils; import xyz.fycz.myreader.util.IOUtils;
@ -19,15 +20,16 @@ public class ChapterService extends BaseService {
private static volatile ChapterService sInstance; private static volatile ChapterService sInstance;
public static ChapterService getInstance() { public static ChapterService getInstance() {
if (sInstance == null){ if (sInstance == null) {
synchronized (ChapterService.class){ synchronized (ChapterService.class) {
if (sInstance == null){ if (sInstance == null) {
sInstance = new ChapterService(); sInstance = new ChapterService();
} }
} }
} }
return sInstance; return sInstance;
} }
private List<Chapter> findChapters(String sql, String[] selectionArgs) { private List<Chapter> findChapters(String sql, String[] selectionArgs) {
ArrayList<Chapter> chapters = new ArrayList<>(); ArrayList<Chapter> chapters = new ArrayList<>();
try { try {
@ -188,11 +190,11 @@ public class ChapterService extends BaseService {
return; return;
} }
File file = getBookFile(chapter.getBookId(), chapter.getTitle()); File file = getChapterFileExisted(chapter);
BufferedWriter bw = null; BufferedWriter bw = null;
try { try {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))); bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
bw.write(content.replace(chapter.getTitle(), "")); bw.write(content);
bw.flush(); bw.flush();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -207,25 +209,25 @@ public class ChapterService extends BaseService {
* @param chapter * @param chapter
*/ */
public void deleteChapterCacheFile(Chapter chapter) { public void deleteChapterCacheFile(Chapter chapter) {
File file = getBookFile(chapter.getBookId(), chapter.getTitle()); File file = getChapterFile(chapter);
file.delete(); if (file.exists()) file.delete();
} }
/** /**
* 获取缓存章节内容 * 获取缓存章节内容
*
* @param chapter * @param chapter
* @return * @return
*/ */
public String getChapterCatheContent(Chapter chapter){ public String getChapterCatheContent(Chapter chapter) {
File file = new File(APPCONST.BOOK_CACHE_PATH + chapter.getBookId() File file = getChapterFile(chapter);
+ File.separator + chapter.getTitle() + FileUtils.SUFFIX_FY);
if (!file.exists()) return null; if (!file.exists()) return null;
BufferedReader br = null; BufferedReader br = null;
try { try {
br = new BufferedReader(new FileReader(file)); br = new BufferedReader(new FileReader(file));
StringBuilder s = new StringBuilder(); StringBuilder s = new StringBuilder();
String line = null; String line = null;
while ((line = br.readLine()) != null){ while ((line = br.readLine()) != null) {
s.append(line); s.append(line);
s.append("\n"); s.append("\n");
} }
@ -233,7 +235,7 @@ public class ChapterService extends BaseService {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
}finally { } finally {
IOUtils.close(br); IOUtils.close(br);
} }
} }
@ -278,13 +280,20 @@ public class ChapterService extends BaseService {
* 根据文件名判断是否被缓存过 (因为可能数据库显示被缓存过但是文件中却没有的情况所以需要根据文件判断是否被缓存 * 根据文件名判断是否被缓存过 (因为可能数据库显示被缓存过但是文件中却没有的情况所以需要根据文件判断是否被缓存
* ) * )
* *
* @param folderName : bookId * @param chapter : chapter
* @param fileName: chapterName
* @return * @return
*/ */
public static boolean isChapterCached(String folderName, String fileName) { public static boolean isChapterCached(Chapter chapter) {
File file = new File(APPCONST.BOOK_CACHE_PATH + folderName File file = getChapterFile(chapter);
+ File.separator + fileName + FileUtils.SUFFIX_FY); 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(); return file.exists();
} }
@ -326,4 +335,24 @@ public class ChapterService extends BaseService {
+ File.separator + fileName + FileUtils.SUFFIX_FY); + 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;
}
} }

@ -108,7 +108,7 @@ class SearchWordEngine(
chapterTitle = chapter.title, chapterTitle = chapter.title,
searchWord2List = mutableListOf() searchWord2List = mutableListOf()
) )
if (!isLocalBook && !ChapterService.isChapterCached(book.id, chapter.title)) { if (!isLocalBook && !ChapterService.isChapterCached(chapter)) {
emitter.onNext(searchWord1) emitter.onNext(searchWord1)
return@ObservableOnSubscribe return@ObservableOnSubscribe
} }

@ -62,7 +62,7 @@ public class BookMarkAdapter extends ArrayAdapter<BookMark> {
final BookMark bookMark = getItem(postion); final BookMark bookMark = getItem(postion);
assert bookMark != null; assert bookMark != null;
viewHolder.tvTitle.setText(String.format("%s[%s]", bookMark.getTitle(), bookMark.getBookMarkReadPosition() + 1)); 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); viewHolder.tvTitle.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(getContext(), R.drawable.selector_category_load),null,null,null);
} else { } else {
viewHolder.tvTitle.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(getContext(),R.drawable.selector_category_unload),null,null,null); viewHolder.tvTitle.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(getContext(),R.drawable.selector_category_unload),null,null,null);

@ -223,7 +223,7 @@ public abstract class BookcaseAdapter extends RecyclerView.Adapter<BookcaseAdapt
return false; return false;
} }
for (Chapter chapter : chapters) { for (Chapter chapter : chapters) {
if (ChapterService.isChapterCached(chapter.getBookId(), chapter.getTitle())) { if (ChapterService.isChapterCached(chapter)) {
bw.write("\t" + chapter.getTitle()); bw.write("\t" + chapter.getTitle());
bw.newLine(); bw.newLine();
br = new BufferedReader(new FileReader(APPCONST.BOOK_CACHE_PATH + book.getId() br = new BufferedReader(new FileReader(APPCONST.BOOK_CACHE_PATH + book.getId()

@ -68,7 +68,7 @@ public class ChapterTitleAdapter extends ArrayAdapter<Chapter> {
final Chapter chapter = getItem(postion); final Chapter chapter = getItem(postion);
// viewHolder.tvTitle.setText("【" + chapter.getTitle() + "】"); // viewHolder.tvTitle.setText("【" + chapter.getTitle() + "】");
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); viewHolder.tvTitle.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(getContext(),R.drawable.selector_category_load), null, null, null);
} else { } else {
viewHolder.tvTitle.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(getContext(),R.drawable.selector_category_unload), null, null, null); viewHolder.tvTitle.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(getContext(),R.drawable.selector_category_unload), null, null, null);

@ -28,7 +28,7 @@ public class CatalogHolder extends ViewHolderImpl<Chapter> {
@Override @Override
public void onBind(RecyclerView.ViewHolder holder, Chapter data, int pos) { 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); tvTitle.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(getContext(),R.drawable.selector_category_load), null, null, null);
} else { } else {
tvTitle.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(getContext(),R.drawable.selector_category_unload), null, null, null); tvTitle.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(getContext(),R.drawable.selector_category_unload), null, null, null);

@ -391,7 +391,7 @@ public class LocalPageLoader extends PageLoader {
@Override @Override
public boolean hasChapterData(Chapter chapter) { public boolean hasChapterData(Chapter chapter) {
return chapter.getEnd() > 0 || ChapterService.isChapterCached(mCollBook.getId(), chapter.getTitle()); return chapter.getEnd() > 0 || ChapterService.isChapterCached(chapter);
} }
} }

@ -113,7 +113,7 @@ public class NetPageLoader extends PageLoader {
@Override @Override
public boolean hasChapterData(Chapter chapter) { public boolean hasChapterData(Chapter chapter) {
return ChapterService.isChapterCached(mCollBook.getId(), chapter.getTitle()); return ChapterService.isChapterCached(chapter);
} }
// 装载上一章节的内容 // 装载上一章节的内容

Loading…
Cancel
Save