From 4c5d030d61cecf9399c9dfac479a3649f65f5978 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 12 Nov 2020 21:01:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AD=A3=E6=96=87=E8=A7=84=E5=88=99=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=AD=97=E4=BD=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/data/entities/BookChapter.kt | 5 + .../main/java/io/legado/app/help/BookHelp.kt | 40 +++++--- .../legado/app/model/webBook/BookContent.kt | 42 ++++----- .../io/legado/app/service/AudioPlayService.kt | 13 +-- .../io/legado/app/service/CacheBookService.kt | 6 +- .../io/legado/app/service/help/CacheBook.kt | 7 +- .../legado/app/ui/book/cache/CacheActivity.kt | 2 +- .../app/ui/book/read/page/ContentTextView.kt | 26 +++--- .../app/ui/book/read/page/ContentView.kt | 10 +- .../app/ui/book/read/page/PageFactory.kt | 8 +- .../legado/app/ui/book/read/page/PageView.kt | 12 +-- .../app/ui/book/read/page/TextPageFactory.kt | 52 ++++++----- .../ui/book/read/page/entities/PageData.kt | 6 ++ .../ui/book/read/page/entities/TextChapter.kt | 5 +- .../read/page/provider/ChapterProvider.kt | 91 ++++--------------- .../searchContent/SearchContentActivity.kt | 4 +- .../app/ui/book/toc/ChapterListAdapter.kt | 4 +- .../app/ui/book/toc/ChapterListFragment.kt | 2 +- 18 files changed, 154 insertions(+), 181 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/book/read/page/entities/PageData.kt diff --git a/app/src/main/java/io/legado/app/data/entities/BookChapter.kt b/app/src/main/java/io/legado/app/data/entities/BookChapter.kt index ecc00633f..3441a4ed4 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookChapter.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookChapter.kt @@ -6,6 +6,7 @@ import androidx.room.ForeignKey import androidx.room.Ignore import androidx.room.Index import io.legado.app.utils.GSON +import io.legado.app.utils.MD5Utils import io.legado.app.utils.NetworkUtils import io.legado.app.utils.fromJsonObject import kotlinx.android.parcel.IgnoredOnParcel @@ -62,5 +63,9 @@ data class BookChapter( fun getAbsoluteURL(): String { return NetworkUtils.getAbsoluteURL(baseUrl, url)!! } + + fun getFileName(): String = String.format("%05d-%s.nb", index, MD5Utils.md5Encode16(title)) + + fun getFontName(): String = String.format("%05d-%s.ttf", index, MD5Utils.md5Encode16(title)) } diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index 8b594b11f..fb58c2e80 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -31,14 +31,6 @@ object BookHelp { private val downloadDir: File = App.INSTANCE.externalFilesDir private val downloadImages = CopyOnWriteArraySet() - fun formatChapterName(bookChapter: BookChapter): String { - return String.format( - "%05d-%s.nb", - bookChapter.index, - MD5Utils.md5Encode16(bookChapter.title) - ) - } - fun clearCache() { FileUtils.deleteFile( FileUtils.getPath(downloadDir, cacheFolderName) @@ -75,7 +67,7 @@ object BookHelp { downloadDir, cacheFolderName, book.getFolderName(), - formatChapterName(bookChapter), + bookChapter.getFileName(), ).writeText(content) //保存图片 content.split("\n").forEach { @@ -91,6 +83,30 @@ object BookHelp { postEvent(EventBus.SAVE_CONTENT, bookChapter) } + suspend fun saveFont(book: Book, bookChapter: BookChapter, font: ByteArray) { + FileUtils.createFileIfNotExist( + downloadDir, + cacheFolderName, + book.getFolderName(), + "font", + bookChapter.getFontName() + ).writeBytes(font) + } + + suspend fun getFont(book: Book, bookChapter: BookChapter): File? { + val fontFile = FileUtils.getFile( + downloadDir, + cacheFolderName, + book.getFolderName(), + "font", + bookChapter.getFontName() + ) + if (fontFile.exists()) { + return fontFile + } + return null + } + suspend fun saveImage(book: Book, src: String) { while (downloadImages.contains(src)) { delay(100) @@ -158,7 +174,7 @@ object BookHelp { downloadDir, cacheFolderName, book.getFolderName(), - formatChapterName(bookChapter) + bookChapter.getFileName() ) } } @@ -171,7 +187,7 @@ object BookHelp { downloadDir, cacheFolderName, book.getFolderName(), - formatChapterName(bookChapter) + bookChapter.getFileName() ) if (file.exists()) { return file.readText() @@ -188,7 +204,7 @@ object BookHelp { downloadDir, cacheFolderName, book.getFolderName(), - formatChapterName(bookChapter) + bookChapter.getFileName() ).delete() } } diff --git a/app/src/main/java/io/legado/app/model/webBook/BookContent.kt b/app/src/main/java/io/legado/app/model/webBook/BookContent.kt index bc9ecab94..855f2ec0f 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookContent.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookContent.kt @@ -6,6 +6,7 @@ import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.rule.ContentRule +import io.legado.app.help.BookHelp import io.legado.app.model.Debug import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeUrl @@ -38,7 +39,7 @@ object BookContent { val analyzeRule = AnalyzeRule(book) analyzeRule.setContent(body).setBaseUrl(baseUrl) analyzeRule.getByteArray(it)?.let { font -> - + BookHelp.saveFont(book, bookChapter, font) } } var contentData = analyzeContent( @@ -62,16 +63,14 @@ object BookContent { ruleUrl = nextUrl, book = book, headerMapF = bookSource.getHeaderMap() - ).getResponseAwait(bookSource.bookSourceUrl) - .body?.let { nextBody -> - contentData = - analyzeContent( - book, nextUrl, nextBody, contentRule, bookChapter, bookSource, false - ) - nextUrl = - if (contentData.nextUrl.isNotEmpty()) contentData.nextUrl[0] else "" - content.append(contentData.content).append("\n") - } + ).getResponseAwait(bookSource.bookSourceUrl).body?.let { nextBody -> + contentData = analyzeContent( + book, nextUrl, nextBody, contentRule, bookChapter, bookSource, false + ) + nextUrl = + if (contentData.nextUrl.isNotEmpty()) contentData.nextUrl[0] else "" + content.append(contentData.content).append("\n") + } } Debug.log(bookSource.bookSourceUrl, "◇本章总页数:${nextUrlList.size}") } else if (contentData.nextUrl.size > 1) { @@ -86,20 +85,12 @@ object BookContent { ruleUrl = item.nextUrl, book = book, headerMapF = bookSource.getHeaderMap() - ).getResponseAwait(bookSource.bookSourceUrl) - .body?.let { - contentData = - analyzeContent( - book, - item.nextUrl, - it, - contentRule, - bookChapter, - bookSource, - false - ) - item.content = contentData.content - } + ).getResponseAwait(bookSource.bookSourceUrl).body?.let { + contentData = analyzeContent( + book, item.nextUrl, it, contentRule, bookChapter, bookSource, false + ) + item.content = contentData.content + } } } for (item in contentDataList) { @@ -119,6 +110,7 @@ object BookContent { Debug.log(bookSource.bookSourceUrl, "└${bookChapter.title}") Debug.log(bookSource.bookSourceUrl, "┌获取正文内容") Debug.log(bookSource.bookSourceUrl, "└\n$contentStr") + BookHelp.saveContent(book, bookChapter, contentStr) return contentStr } diff --git a/app/src/main/java/io/legado/app/service/AudioPlayService.kt b/app/src/main/java/io/legado/app/service/AudioPlayService.kt index d629f2ba8..9fb514430 100644 --- a/app/src/main/java/io/legado/app/service/AudioPlayService.kt +++ b/app/src/main/java/io/legado/app/service/AudioPlayService.kt @@ -24,7 +24,6 @@ import io.legado.app.constant.EventBus import io.legado.app.constant.IntentAction import io.legado.app.constant.Status import io.legado.app.data.entities.BookChapter -import io.legado.app.help.BookHelp import io.legado.app.help.IntentHelp import io.legado.app.help.MediaHelp import io.legado.app.model.analyzeRule.AnalyzeUrl @@ -283,19 +282,17 @@ class AudioPlayService : BaseService(), } } - private fun loadContent(chapter: BookChapter) { - AudioPlay.book?.let { book -> - AudioPlay.webBook?.getContent(book, chapter, scope = this) - ?.onSuccess(IO) { content -> + private fun loadContent(chapter: BookChapter) = AudioPlay.apply { + book?.let { book -> + webBook?.getContent(book, chapter, scope = this@AudioPlayService) + ?.onSuccess { content -> + removeLoading(chapter.index) if (content.isEmpty()) { withContext(Main) { toast("未获取到资源链接") } - removeLoading(chapter.index) } else { - BookHelp.saveContent(book, chapter, content) contentLoadFinish(chapter, content) - removeLoading(chapter.index) } }?.onError { contentLoadFinish(chapter, it.localizedMessage ?: toString()) diff --git a/app/src/main/java/io/legado/app/service/CacheBookService.kt b/app/src/main/java/io/legado/app/service/CacheBookService.kt index 0ef8a2ea2..c0d7d0c98 100644 --- a/app/src/main/java/io/legado/app/service/CacheBookService.kt +++ b/app/src/main/java/io/legado/app/service/CacheBookService.kt @@ -20,7 +20,6 @@ import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.webBook.WebBook import io.legado.app.service.help.CacheBook import io.legado.app.utils.postEvent -import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.isActive import org.jetbrains.anko.toast @@ -197,8 +196,7 @@ class CacheBookService : BaseService() { CacheBook.addLog("getContentError${it.localizedMessage}") updateNotification("getContentError${it.localizedMessage}") } - .onSuccess(IO) { content -> - BookHelp.saveContent(book, bookChapter, content) + .onSuccess { synchronized(this@CacheBookService) { downloadCount[book.bookUrl]?.increaseSuccess() downloadCount[book.bookUrl]?.increaseFinished() @@ -221,7 +219,7 @@ class CacheBookService : BaseService() { downloadCount.remove(book.bookUrl) } } - }.onFinally(IO) { + }.onFinally { postDownloading(true) } } else { diff --git a/app/src/main/java/io/legado/app/service/help/CacheBook.kt b/app/src/main/java/io/legado/app/service/help/CacheBook.kt index 3e96bd616..1c69ac09c 100644 --- a/app/src/main/java/io/legado/app/service/help/CacheBook.kt +++ b/app/src/main/java/io/legado/app/service/help/CacheBook.kt @@ -7,11 +7,9 @@ import io.legado.app.R import io.legado.app.constant.IntentAction import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter -import io.legado.app.help.BookHelp import io.legado.app.model.webBook.WebBook import io.legado.app.service.CacheBookService import io.legado.app.utils.msg -import kotlinx.coroutines.Dispatchers.IO import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.CopyOnWriteArraySet @@ -76,10 +74,7 @@ object CacheBook { } downloadMap[book.bookUrl]?.add(chapter.index) webBook.getContent(book, chapter) - .onSuccess(IO) { content -> - if (content.isNotBlank()) { - BookHelp.saveContent(book, chapter, content) - } + .onSuccess { content -> if (ReadBook.book?.bookUrl == book.bookUrl) { ReadBook.contentLoadFinish( book, diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt index d9cf476c1..c52aee6ba 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt @@ -151,7 +151,7 @@ class CacheActivity : VMBaseActivity(R.layout.activity_download) val chapterCaches = hashSetOf() val cacheNames = BookHelp.getChapterFiles(book) App.db.bookChapterDao().getChapterList(book.bookUrl).forEach { chapter -> - if (cacheNames.contains(BookHelp.formatChapterName(chapter))) { + if (cacheNames.contains(chapter.getFileName())) { chapterCaches.add(chapter.url) } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt index be7d61348..86dc57d52 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt @@ -12,9 +12,7 @@ import io.legado.app.constant.PreferKey import io.legado.app.help.ReadBookConfig import io.legado.app.lib.theme.accentColor import io.legado.app.service.help.ReadBook -import io.legado.app.ui.book.read.page.entities.TextChar -import io.legado.app.ui.book.read.page.entities.TextLine -import io.legado.app.ui.book.read.page.entities.TextPage +import io.legado.app.ui.book.read.page.entities.* import io.legado.app.ui.book.read.page.provider.ChapterProvider import io.legado.app.ui.book.read.page.provider.ImageProvider import io.legado.app.ui.widget.dialog.PhotoDialog @@ -40,6 +38,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at private val visibleRect = RectF() private val selectStart = arrayOf(0, 0, 0) private val selectEnd = arrayOf(0, 0, 0) + private var textChapter: TextChapter? = null private var textPage: TextPage = TextPage() //滚动参数 @@ -51,8 +50,9 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at contentDescription = textPage.text } - fun setContent(textPage: TextPage) { - this.textPage = textPage + fun setContent(pageData: PageData) { + this.textChapter = pageData.textChapter + this.textPage = pageData.textPage contentDescription = textPage.text invalidate() } @@ -139,7 +139,11 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at isTitle: Boolean, isReadAloud: Boolean, ) { - val textPaint = if (isTitle) ChapterProvider.titlePaint else ChapterProvider.contentPaint + val textPaint = if (isTitle) { + textChapter?.titlePaint ?: ChapterProvider.titlePaint + } else { + textChapter?.contentPaint ?: ChapterProvider.contentPaint + } textPaint.color = if (isReadAloud) context.accentColor else ReadBookConfig.textColor textChars.forEach { @@ -186,13 +190,13 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at pageOffset = min(0, offset) } else if (pageOffset > 0) { pageFactory.moveToPrev(false) - textPage = pageFactory.currentPage + textPage = pageFactory.curData.textPage pageOffset -= textPage.height.toInt() upView?.invoke(textPage) } else if (pageOffset < -textPage.height) { pageOffset += textPage.height.toInt() pageFactory.moveToNext(false) - textPage = pageFactory.currentPage + textPage = pageFactory.curData.textPage upView?.invoke(textPage) } invalidate() @@ -482,15 +486,15 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at return when (relativePos) { 0 -> pageOffset.toFloat() 1 -> pageOffset + textPage.height - else -> pageOffset + textPage.height + pageFactory.nextPage.height + else -> pageOffset + textPage.height + pageFactory.nextData.textPage.height } } private fun relativePage(relativePos: Int): TextPage { return when (relativePos) { 0 -> textPage - 1 -> pageFactory.nextPage - else -> pageFactory.nextPagePlus + 1 -> pageFactory.nextData.textPage + else -> pageFactory.nextPlusData.textPage } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt index 00e392e18..d94c30086 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt @@ -12,6 +12,7 @@ import io.legado.app.constant.AppConst.timeFormat import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadTipConfig import io.legado.app.service.help.ReadBook +import io.legado.app.ui.book.read.page.entities.PageData import io.legado.app.ui.book.read.page.entities.TextPage import io.legado.app.ui.book.read.page.provider.ChapterProvider import io.legado.app.ui.widget.BatteryView @@ -217,11 +218,12 @@ class ContentView(context: Context) : FrameLayout(context) { tvBattery?.setBattery(battery) } - fun setContent(textPage: TextPage, resetPageOffset: Boolean = true) { - setProgress(textPage) - if (resetPageOffset) + fun setContent(pageData: PageData, resetPageOffset: Boolean = true) { + setProgress(pageData.textPage) + if (resetPageOffset) { resetPageOffset() - content_text_view.setContent(textPage) + } + content_text_view.setContent(pageData) } fun resetPageOffset() { diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/PageFactory.kt b/app/src/main/java/io/legado/app/ui/book/read/page/PageFactory.kt index 75fd79094..949ec848f 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/PageFactory.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/PageFactory.kt @@ -10,13 +10,13 @@ abstract class PageFactory(protected val dataSource: DataSource) { abstract fun moveToPrev(upContent: Boolean): Boolean - abstract val nextPage: DATA + abstract val nextData: DATA - abstract val prevPage: DATA + abstract val prevData: DATA - abstract val currentPage: DATA + abstract val curData: DATA - abstract val nextPagePlus: DATA + abstract val nextPlusData: DATA abstract fun hasNext(): Boolean diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt index e1fce6de9..9846bee77 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt @@ -376,16 +376,16 @@ class PageView(context: Context, attrs: AttributeSet) : override fun upContent(relativePosition: Int, resetPageOffset: Boolean) { if (isScroll && !callBack.isAutoPage) { - curPage.setContent(pageFactory.currentPage, resetPageOffset) + curPage.setContent(pageFactory.curData, resetPageOffset) } else { curPage.resetPageOffset() when (relativePosition) { - -1 -> prevPage.setContent(pageFactory.prevPage) - 1 -> nextPage.setContent(pageFactory.nextPage) + -1 -> prevPage.setContent(pageFactory.prevData) + 1 -> nextPage.setContent(pageFactory.nextData) else -> { - curPage.setContent(pageFactory.currentPage) - nextPage.setContent(pageFactory.nextPage) - prevPage.setContent(pageFactory.prevPage) + curPage.setContent(pageFactory.curData) + nextPage.setContent(pageFactory.nextData) + prevPage.setContent(pageFactory.prevData) } } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt b/app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt index 95917ae11..613f18707 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt @@ -1,9 +1,10 @@ package io.legado.app.ui.book.read.page import io.legado.app.service.help.ReadBook +import io.legado.app.ui.book.read.page.entities.PageData import io.legado.app.ui.book.read.page.entities.TextPage -class TextPageFactory(dataSource: DataSource) : PageFactory(dataSource) { +class TextPageFactory(dataSource: DataSource) : PageFactory(dataSource) { override fun hasPrev(): Boolean = with(dataSource) { return hasPrevChapter() || pageIndex > 0 @@ -57,74 +58,81 @@ class TextPageFactory(dataSource: DataSource) : PageFactory(dataSource false } - override val currentPage: TextPage + override val curData: PageData get() = with(dataSource) { ReadBook.msg?.let { - return@with TextPage(text = it).format() + return@with PageData(TextPage(text = it).format()) } currentChapter?.let { - return@with it.page(pageIndex) - ?: TextPage(title = it.title).format() + val page = it.page(pageIndex) ?: TextPage(title = it.title).format() + return@with PageData(page, it) } - return TextPage().format() + return PageData(TextPage().format()) } - override val nextPage: TextPage + override val nextData: PageData get() = with(dataSource) { ReadBook.msg?.let { - return@with TextPage(text = it).format() + return@with PageData(TextPage(text = it).format()) } currentChapter?.let { if (pageIndex < it.pageSize - 1) { - return@with it.page(pageIndex + 1)?.removePageAloudSpan() + val page = it.page(pageIndex + 1)?.removePageAloudSpan() ?: TextPage(title = it.title).format() + return@with PageData(page, it) } } if (!hasNextChapter()) { - return@with TextPage(text = "") + return@with PageData(TextPage(text = "")) } nextChapter?.let { - return@with it.page(0)?.removePageAloudSpan() + val page = it.page(0)?.removePageAloudSpan() ?: TextPage(title = it.title).format() + return@with PageData(page, it) } - return TextPage().format() + return PageData(TextPage().format()) } - override val prevPage: TextPage + override val prevData: PageData get() = with(dataSource) { ReadBook.msg?.let { - return@with TextPage(text = it).format() + return@with PageData(TextPage(text = it).format()) } if (pageIndex > 0) { currentChapter?.let { - return@with it.page(pageIndex - 1)?.removePageAloudSpan() + val page = it.page(pageIndex - 1)?.removePageAloudSpan() ?: TextPage(title = it.title).format() + return@with PageData(page, it) } } prevChapter?.let { - return@with it.lastPage?.removePageAloudSpan() + val page = it.lastPage?.removePageAloudSpan() ?: TextPage(title = it.title).format() + return@with PageData(page, it) } - return TextPage().format() + return PageData(TextPage().format()) } - override val nextPagePlus: TextPage + override val nextPlusData: PageData get() = with(dataSource) { currentChapter?.let { if (pageIndex < it.pageSize - 2) { - return@with it.page(pageIndex + 2)?.removePageAloudSpan() + val page = it.page(pageIndex + 2)?.removePageAloudSpan() ?: TextPage(title = it.title).format() + return@with PageData(page, it) } nextChapter?.let { nc -> if (pageIndex < it.pageSize - 1) { - return@with nc.page(0)?.removePageAloudSpan() + val page = nc.page(0)?.removePageAloudSpan() ?: TextPage(title = nc.title).format() + return@with PageData(page, nc) } - return@with nc.page(1)?.removePageAloudSpan() + val page = nc.page(1)?.removePageAloudSpan() ?: TextPage(title = nc.title).format() + return@with PageData(page, nc) } } - return TextPage().format() + return PageData(TextPage().format()) } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/PageData.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/PageData.kt new file mode 100644 index 000000000..3d9bec50c --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/PageData.kt @@ -0,0 +1,6 @@ +package io.legado.app.ui.book.read.page.entities + +data class PageData( + val textPage: TextPage, + val textChapter: TextChapter? = null +) \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt index 3f183830d..1cd9177c9 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt @@ -1,5 +1,6 @@ package io.legado.app.ui.book.read.page.entities +import android.text.TextPaint import kotlin.math.min data class TextChapter( @@ -9,7 +10,9 @@ data class TextChapter( val pages: List, val pageLines: List, val pageLengths: List, - val chaptersSize: Int + val chaptersSize: Int, + var titlePaint: TextPaint? = null, + var contentPaint: TextPaint? = null ) { fun page(index: Int): TextPage? { return pages.getOrNull(index) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt index 7ea1e428a..879eb1e9b 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt @@ -66,19 +66,18 @@ object ChapterProvider { src = NetworkUtils.getAbsoluteURL(bookChapter.url, src) } src?.let { - durY = - setTypeImage( - book, bookChapter, src, durY, textPages, imageStyle - ) + durY = setTypeImage( + book, bookChapter, src, durY, textPages, imageStyle + ) } } else { val isTitle = index == 0 + val textPaint = if (isTitle) titlePaint else contentPaint if (!(isTitle && ReadBookConfig.titleMode == 2)) { - durY = - setTypeText( - text, durY, textPages, pageLines, - pageLengths, stringBuilder, isTitle - ) + durY = setTypeText( + text, durY, textPages, pageLines, + pageLengths, stringBuilder, isTitle, textPaint + ) } } } @@ -185,9 +184,9 @@ object ChapterProvider { pageLengths: ArrayList, stringBuilder: StringBuilder, isTitle: Boolean, + textPaint: TextPaint ): Float { var durY = if (isTitle) y + titleTopSpacing else y - val textPaint = if (isTitle) titlePaint else contentPaint val layout = StaticLayout( text, textPaint, visibleWidth, Layout.Alignment.ALIGN_NORMAL, 0f, 0f, true ) @@ -200,12 +199,7 @@ object ChapterProvider { if (lineIndex == 0 && layout.lineCount > 1 && !isTitle) { //第一行 textLine.text = words - addCharsToLineFirst( - textLine, - words.toStringArray(), - textPaint, - desiredWidth - ) + addCharsToLineFirst(textLine, words.toStringArray(), textPaint, desiredWidth) } else if (lineIndex == layout.lineCount - 1) { //最后一行 textLine.text = "$words\n" @@ -213,22 +207,11 @@ object ChapterProvider { val x = if (isTitle && ReadBookConfig.titleMode == 1) (visibleWidth - layout.getLineWidth(lineIndex)) / 2 else 0f - addCharsToLineLast( - textLine, - words.toStringArray(), - textPaint, - x - ) + addCharsToLineLast(textLine, words.toStringArray(), textPaint, x) } else { //中间行 textLine.text = words - addCharsToLineMiddle( - textLine, - words.toStringArray(), - textPaint, - desiredWidth, - 0f - ) + addCharsToLineMiddle(textLine, words.toStringArray(), textPaint, desiredWidth, 0f) } if (durY + textPaint.textHeight > visibleHeight) { //当前页面结束,设置各种值 @@ -264,33 +247,18 @@ object ChapterProvider { ) { var x = 0f if (!ReadBookConfig.textFullJustify) { - addCharsToLineLast( - textLine, - words, - textPaint, - x - ) + addCharsToLineLast(textLine, words, textPaint, x) return } val bodyIndent = ReadBookConfig.paragraphIndent val icw = StaticLayout.getDesiredWidth(bodyIndent, textPaint) / bodyIndent.length bodyIndent.toStringArray().forEach { val x1 = x + icw - textLine.addTextChar( - charData = it, - start = paddingLeft + x, - end = paddingLeft + x1 - ) + textLine.addTextChar(charData = it, start = paddingLeft + x, end = paddingLeft + x1) x = x1 } val words1 = words.copyOfRange(bodyIndent.length, words.size) - addCharsToLineMiddle( - textLine, - words1, - textPaint, - desiredWidth, - x - ) + addCharsToLineMiddle(textLine, words1, textPaint, desiredWidth, x) } /** @@ -304,12 +272,7 @@ object ChapterProvider { startX: Float, ) { if (!ReadBookConfig.textFullJustify) { - addCharsToLineLast( - textLine, - words, - textPaint, - startX - ) + addCharsToLineLast(textLine, words, textPaint, startX) return } val gapCount: Int = words.lastIndex @@ -318,17 +281,10 @@ object ChapterProvider { words.forEachIndexed { index, s -> val cw = StaticLayout.getDesiredWidth(s, textPaint) val x1 = if (index != words.lastIndex) (x + cw + d) else (x + cw) - textLine.addTextChar( - charData = s, - start = paddingLeft + x, - end = paddingLeft + x1 - ) + textLine.addTextChar(charData = s, start = paddingLeft + x, end = paddingLeft + x1) x = x1 } - exceed( - textLine, - words - ) + exceed(textLine, words) } /** @@ -344,17 +300,10 @@ object ChapterProvider { words.forEach { val cw = StaticLayout.getDesiredWidth(it, textPaint) val x1 = x + cw - textLine.addTextChar( - charData = it, - start = paddingLeft + x, - end = paddingLeft + x1 - ) + textLine.addTextChar(charData = it, start = paddingLeft + x, end = paddingLeft + x1) x = x1 } - exceed( - textLine, - words - ) + exceed(textLine, words) } /** diff --git a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt b/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt index fe28e2373..6218c900f 100644 --- a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt @@ -122,7 +122,7 @@ class SearchContentActivity : observeEvent(EventBus.SAVE_CONTENT) { chapter -> viewModel.book?.bookUrl?.let { bookUrl -> if (chapter.bookUrl == bookUrl) { - adapter.cacheFileNames.add(BookHelp.formatChapterName(chapter)) + adapter.cacheFileNames.add(chapter.getFileName()) adapter.notifyItemChanged(chapter.index, true) } } @@ -143,7 +143,7 @@ class SearchContentActivity : App.db.bookChapterDao().getChapterList(viewModel.bookUrl).map { chapter -> val job = async(Dispatchers.IO) { if (isLocalBook - || adapter.cacheFileNames.contains(BookHelp.formatChapterName(chapter)) + || adapter.cacheFileNames.contains(chapter.getFileName()) ) { searchResults = searchChapter(newText, chapter) } diff --git a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt index 9a994f7ca..a92d78f9d 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt @@ -6,7 +6,6 @@ import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.data.entities.BookChapter -import io.legado.app.help.BookHelp import io.legado.app.lib.theme.accentColor import io.legado.app.utils.getCompatColor import io.legado.app.utils.visible @@ -22,8 +21,7 @@ class ChapterListAdapter(context: Context, val callback: Callback) : override fun convert(holder: ItemViewHolder, item: BookChapter, payloads: MutableList) { with(holder.itemView) { val isDur = callback.durChapterIndex() == item.index - val cached = callback.isLocalBook - || cacheFileNames.contains(BookHelp.formatChapterName(item)) + val cached = callback.isLocalBook || cacheFileNames.contains(item.getFileName()) if (payloads.isEmpty()) { if (isDur) { tv_chapter_name.setTextColor(context.accentColor) diff --git a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt index 679b53238..ba20d22aa 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt @@ -110,7 +110,7 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme observeEvent(EventBus.SAVE_CONTENT) { chapter -> viewModel.book?.bookUrl?.let { bookUrl -> if (chapter.bookUrl == bookUrl) { - adapter.cacheFileNames.add(BookHelp.formatChapterName(chapter)) + adapter.cacheFileNames.add(chapter.getFileName()) adapter.notifyItemChanged(chapter.index, true) } }