优化
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -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<String> {
|
||||
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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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 = "",//章节名称
|
||||
)
|
||||
Reference in New Issue
Block a user