This commit is contained in:
kunfei
2022-12-12 22:11:04 +08:00
parent 102dfdebe4
commit eb2cf16186
17 changed files with 99 additions and 24 deletions
@@ -144,7 +144,8 @@ data class BookChapter(
}
@Suppress("unused")
fun getFileName(): String = String.format("%05d-%s.nb", index, MD5Utils.md5Encode16(title))
fun getFileName(suffix: String = "nb"): String =
String.format("%05d-%s.%s", index, MD5Utils.md5Encode16(title), suffix)
@Suppress("unused")
fun getFontName(): String = String.format("%05d-%s.ttf", index, MD5Utils.md5Encode16(title))
@@ -282,6 +282,41 @@ object BookHelp {
).delete()
}
/**
* 设置是否禁用正文的去除重复标题,针对单个章节
*/
fun setRemoveSameTitle(book: Book, bookChapter: BookChapter, removeSameTitle: Boolean) {
if (removeSameTitle) {
val path = FileUtils.getPath(
downloadDir,
cacheFolderName,
book.getFolderName(),
bookChapter.getFileName(".nr")
)
File(path).delete()
} else {
FileUtils.createFileIfNotExist(
downloadDir,
cacheFolderName,
book.getFolderName(),
bookChapter.getFileName(".nr")
)
}
}
/**
* 获取是否去除重复标题
*/
fun removeSameTitle(book: Book, bookChapter: BookChapter): Boolean {
val path = FileUtils.getPath(
downloadDir,
cacheFolderName,
book.getFolderName(),
bookChapter.getFileName(".nr")
)
return !File(path).exists()
}
/**
* 格式化书名
*/
@@ -84,12 +84,16 @@ class ContentProcessor private constructor(
var sameTitleRemoved = false
if (content != "null") {
//去除重复标题
try {
if (BookHelp.removeSameTitle(book, chapter)) try {
val name = Pattern.quote(book.name)
val title = Pattern.quote(chapter.title)
val titleRegex = "^(\\s|\\p{P}|${name})*${title}(\\s)*".toRegex()
mContent = mContent.replace(titleRegex, "")
sameTitleRemoved = true
val titleRegex = "^(\\s|\\p{P}|${name})*${title}(\\s)*"
val matcher = Pattern.compile(titleRegex)
.matcher(mContent)
if (matcher.find()) {
mContent = mContent.substring(matcher.end())
sameTitleRemoved = true
}
} catch (e: Exception) {
AppLog.put("去除重复标题出错\n${e.localizedMessage}", e)
}
@@ -43,9 +43,6 @@ object ReadBook : CoroutineScope by MainScope() {
private val readRecord = ReadRecord()
var readStartTime: Long = System.currentTimeMillis()
/* 跳转历史记录 */
var bookProgressHistory: List<BookProgress>? = null
/* 跳转进度前进度记录 */
var lastBookPress: BookProgress? = null
@@ -274,7 +271,9 @@ object ReadBook : CoroutineScope by MainScope() {
}
/**
* 加载章内容
* 加载当前章节和前后一章内容
* @param resetPageOffset 滚动阅读是否重置滚动位置
* @param success 当前章节加载完成回调
*/
fun loadContent(resetPageOffset: Boolean, success: (() -> Unit)? = null) {
loadContent(durChapterIndex, resetPageOffset = resetPageOffset) {
@@ -284,6 +283,13 @@ object ReadBook : CoroutineScope by MainScope() {
loadContent(durChapterIndex - 1, resetPageOffset = resetPageOffset)
}
/**
* 加载章节内容
* @param index 章节序号
* @param upContent 是否更新视图
* @param resetPageOffset 滚动阅读是否重置滚动位置
* @param success 加载完成回调
*/
fun loadContent(
index: Int,
upContent: Boolean = true,
@@ -308,6 +314,9 @@ object ReadBook : CoroutineScope by MainScope() {
}
}
/**
* 下载正文
*/
private fun download(index: Int) {
if (index < 0) return
if (index > chapterSize - 1) {
@@ -332,6 +341,9 @@ object ReadBook : CoroutineScope by MainScope() {
}
}
/**
* 下载正文
*/
private fun download(
scope: CoroutineScope,
chapter: BookChapter,
@@ -404,10 +404,7 @@ class ReadBookActivity : BaseReadBookActivity(),
sureSyncProgress(progress)
}
}
R.id.menu_same_title_removed -> {
val chapterUrl = ReadBook.curTextChapter?.url
MD5Utils.md5Encode(chapterUrl)
}
R.id.menu_same_title_removed -> viewModel.reverseRemoveSameTitle()
R.id.menu_help -> showReadMenuHelp()
}
return super.onCompatOptionsItemSelected(item)
@@ -33,7 +33,6 @@ import io.legado.app.ui.book.read.page.provider.ImageProvider
import io.legado.app.ui.book.searchContent.SearchResult
import io.legado.app.utils.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
@@ -429,6 +428,25 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
return arrayOf(pageIndex, lineIndex, charIndex, addLine, charIndex2)
}
/**
* 翻转删除重复标题
*/
fun reverseRemoveSameTitle() {
execute {
val book = ReadBook.book
val textChapter = ReadBook.curTextChapter
if (book != null && textChapter != null) {
BookHelp.setRemoveSameTitle(
book, textChapter.chapter, !textChapter.sameTitleRemoved
)
ReadBook.loadContent(ReadBook.durChapterIndex)
}
}
}
/**
* 刷新图片
*/
fun refreshImage(src: String) {
execute {
ReadBook.book?.let { book ->
@@ -441,6 +459,9 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
}
}
/**
* 保存图片
*/
@Suppress("BlockingMethodInNonBlockingContext")
fun saveImage(src: String?, uri: Uri) {
src ?: return
@@ -469,7 +469,7 @@ class ReadMenu @JvmOverloads constructor(
binding.tvChapterName.text = it.title
binding.tvChapterName.visible()
if (!ReadBook.isLocalBook) {
binding.tvChapterUrl.text = it.url
binding.tvChapterUrl.text = it.chapter.getAbsoluteURL()
binding.tvChapterUrl.visible()
} else {
binding.tvChapterUrl.gone()
@@ -2,6 +2,7 @@ package io.legado.app.ui.book.read.page.entities
import androidx.annotation.Keep
import io.legado.app.data.entities.BookChapter
import kotlin.math.min
/**
@@ -10,9 +11,9 @@ import kotlin.math.min
@Keep
@Suppress("unused")
data class TextChapter(
val chapter: BookChapter,
val position: Int,
val title: String,
val url: String,
val pages: List<TextPage>,
val chaptersSize: Int,
val sameTitleRemoved: Boolean,
@@ -216,8 +216,8 @@ object ChapterProvider {
}
return TextChapter(
bookChapter,
bookChapter.index, displayTitle,
bookChapter.getAbsoluteURL(),
textPages, chapterSize,
bookContent.sameTitleRemoved,
bookChapter.isVip, bookChapter.isPay