From 9ccd60f06460300ee54a1ee1b0ca831f15b41a96 Mon Sep 17 00:00:00 2001 From: Horis <8674809+821938089@users.noreply.github.com> Date: Sat, 15 Mar 2025 12:39:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/book/BookExtensions.kt | 2 +- .../java/io/legado/app/help/book/BookHelp.kt | 27 +++++++++------- .../java/io/legado/app/model/ReadManga.kt | 31 +++++++------------ .../ui/book/manga/entities/MangaContent.kt | 8 ++--- 4 files changed, 32 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/book/BookExtensions.kt b/app/src/main/java/io/legado/app/help/book/BookExtensions.kt index 3d499c8f6..10cedb856 100644 --- a/app/src/main/java/io/legado/app/help/book/BookExtensions.kt +++ b/app/src/main/java/io/legado/app/help/book/BookExtensions.kt @@ -346,7 +346,7 @@ fun Book.getExportFileName( fun Book.simulatedTotalChapterNum(): Int { return if (readSimulating()) { val currentDate = LocalDate.now() - val daysPassed = between(this.config.startDate, currentDate).days + 1 + val daysPassed = between(config.startDate, currentDate).days + 1 // 计算当前应该解锁到哪一章 val chaptersToUnlock = max(0, (config.startChapter ?: 0) + (daysPassed * config.dailyChapters)) diff --git a/app/src/main/java/io/legado/app/help/book/BookHelp.kt b/app/src/main/java/io/legado/app/help/book/BookHelp.kt index 95b918319..7554d54c6 100644 --- a/app/src/main/java/io/legado/app/help/book/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/book/BookHelp.kt @@ -31,6 +31,7 @@ import io.legado.app.utils.postEvent import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.ensureActive +import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.flow import kotlinx.coroutines.sync.Mutex @@ -149,7 +150,7 @@ object BookHelp { book.getFolderName(), cacheImageFolderName ).listFiles()?.forEach { imgFile -> - if (!imgNames.contains(imgFile.name)){ + if (!imgNames.contains(imgFile.name)) { imgFile.delete() } } @@ -191,6 +192,17 @@ object BookHelp { } } + fun flowImages(bookChapter: BookChapter, content: String): Flow { + return flow { + val matcher = AppPattern.imgPattern.matcher(content) + while (matcher.find()) { + val src = matcher.group(1) ?: continue + val mSrc = NetworkUtils.getAbsoluteURL(bookChapter.url, src) + emit(mSrc) + } + } + } + suspend fun saveImages( bookSource: BookSource, book: Book, @@ -198,14 +210,7 @@ object BookHelp { content: String, concurrency: Int = AppConfig.threadCount ) = coroutineScope { - flow { - val matcher = AppPattern.imgPattern.matcher(content) - while (matcher.find()) { - val src = matcher.group(1) ?: continue - val mSrc = NetworkUtils.getAbsoluteURL(bookChapter.url, src) - emit(mSrc) - } - }.onEachParallel(concurrency) { mSrc -> + flowImages(bookChapter, content).onEachParallel(concurrency) { mSrc -> saveImage(bookSource, book, mSrc, bookChapter) }.collect() } @@ -329,8 +334,8 @@ object BookHelp { * 检测该章节是否下载 */ fun hasContent(book: Book, bookChapter: BookChapter): Boolean { - return if (book.isLocalTxt - || (bookChapter.isVolume && bookChapter.url.startsWith(bookChapter.title)) + return if (book.isLocalTxt || + (bookChapter.isVolume && bookChapter.url.startsWith(bookChapter.title)) ) { true } else { diff --git a/app/src/main/java/io/legado/app/model/ReadManga.kt b/app/src/main/java/io/legado/app/model/ReadManga.kt index 50b3c5486..8631c5e16 100644 --- a/app/src/main/java/io/legado/app/model/ReadManga.kt +++ b/app/src/main/java/io/legado/app/model/ReadManga.kt @@ -1,7 +1,6 @@ package io.legado.app.model import io.legado.app.constant.AppLog -import io.legado.app.constant.AppPattern import io.legado.app.data.appDb import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter @@ -26,7 +25,6 @@ import io.legado.app.ui.book.manga.entities.MangaChapter import io.legado.app.ui.book.manga.entities.MangaContent import io.legado.app.ui.book.manga.entities.MangaContentData import io.legado.app.ui.book.manga.entities.ReaderLoading -import io.legado.app.utils.NetworkUtils import io.legado.app.utils.mapIndexed import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineStart @@ -38,7 +36,6 @@ import kotlinx.coroutines.cancelChildren import kotlinx.coroutines.delay import kotlinx.coroutines.ensureActive import kotlinx.coroutines.flow.distinctUntilChanged -import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.toList import kotlinx.coroutines.launch import kotlinx.coroutines.sync.Semaphore @@ -431,7 +428,7 @@ object ReadManga : CoroutineScope by MainScope() { (downloadFailChapters[chapter.index] ?: 0) + 1 contentLoadFinish(chapter, null) }, cancel = { - contentLoadFinish(chapter, null, canceled = true) + contentLoadFinish(chapter, null, canceled = true) }) } else { contentLoadFinish(chapter, null, "加载内容失败 没有书源") @@ -562,22 +559,16 @@ object ReadManga : CoroutineScope by MainScope() { } private suspend fun getManageChapter(chapter: BookChapter, content: String): MangaChapter { - val list = flow { - val matcher = AppPattern.imgPattern.matcher(content) - while (matcher.find()) { - val src = matcher.group(1) ?: continue - val mSrc = NetworkUtils.getAbsoluteURL(chapter.url, src) - emit(mSrc) - } - }.distinctUntilChanged().mapIndexed { index, src -> - MangaContent( - mChapterIndex = chapter.index, - chapterSize = chapterSize, - mImageUrl = src, - index = index, - mChapterName = chapter.title - ) - }.toList() + val list = BookHelp.flowImages(chapter, content) + .distinctUntilChanged().mapIndexed { index, src -> + MangaContent( + mChapterIndex = chapter.index, + chapterSize = chapterSize, + mImageUrl = src, + index = index, + mChapterName = chapter.title + ) + }.toList() val imageCount = list.size diff --git a/app/src/main/java/io/legado/app/ui/book/manga/entities/MangaContent.kt b/app/src/main/java/io/legado/app/ui/book/manga/entities/MangaContent.kt index 2ab6d240a..9d842e672 100644 --- a/app/src/main/java/io/legado/app/ui/book/manga/entities/MangaContent.kt +++ b/app/src/main/java/io/legado/app/ui/book/manga/entities/MangaContent.kt @@ -1,10 +1,10 @@ package io.legado.app.ui.book.manga.entities data class MangaContent( - var mChapterIndex: Int = 0,//总章节位置 - var chapterSize: Int,//总章节数量 + val mChapterIndex: Int = 0,//总章节位置 + val chapterSize: Int,//总章节数量 val mImageUrl: String = "",//当前URL - var index: Int = 0,//当前章节位置 + val index: Int = 0,//当前章节位置 var imageCount: Int = 0,//当前章节内容总数 - var mChapterName: String = "",//章节名称 + val mChapterName: String = "",//章节名称 ) \ No newline at end of file