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

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
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

@ -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;
}

@ -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<Chapter> findChapters(String sql, String[] selectionArgs) {
ArrayList<Chapter> 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;
}
}

@ -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
}

@ -62,7 +62,7 @@ public class BookMarkAdapter extends ArrayAdapter<BookMark> {
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);

@ -223,7 +223,7 @@ public abstract class BookcaseAdapter extends RecyclerView.Adapter<BookcaseAdapt
return false;
}
for (Chapter chapter : chapters) {
if (ChapterService.isChapterCached(chapter.getBookId(), chapter.getTitle())) {
if (ChapterService.isChapterCached(chapter)) {
bw.write("\t" + chapter.getTitle());
bw.newLine();
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);
// 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);

@ -28,7 +28,7 @@ public class CatalogHolder extends ViewHolderImpl<Chapter> {
@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);

@ -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);
}
}

@ -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);
}
// 装载上一章节的内容

Loading…
Cancel
Save