mirror of
https://github.com/gedoor/legado.git
synced 2024-08-30 09:23:26 +08:00
优化
This commit is contained in:
parent
fcc887cb25
commit
f84a75bab8
@ -83,7 +83,7 @@ object CacheBook {
|
|||||||
|
|
||||||
val downloadSummary: String
|
val downloadSummary: String
|
||||||
get() {
|
get() {
|
||||||
return "正在下载:${onDownloadCount}|等待中:${waitCount}|失败:${errorCount}|成功:${successCount}"
|
return "正在下载:${onDownloadCount}|等待中:${waitCount}|失败:${errorDownloadMap.count()}|成功:${successDownloadSet.size}"
|
||||||
}
|
}
|
||||||
|
|
||||||
val isRun: Boolean
|
val isRun: Boolean
|
||||||
@ -104,15 +104,6 @@ object CacheBook {
|
|||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
private val successCount: Int
|
|
||||||
get() {
|
|
||||||
var count = 0
|
|
||||||
cacheBookMap.forEach {
|
|
||||||
count += it.value.successCount
|
|
||||||
}
|
|
||||||
return count
|
|
||||||
}
|
|
||||||
|
|
||||||
val onDownloadCount: Int
|
val onDownloadCount: Int
|
||||||
get() {
|
get() {
|
||||||
var count = 0
|
var count = 0
|
||||||
@ -122,28 +113,18 @@ object CacheBook {
|
|||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
private val errorCount: Int
|
val successDownloadSet = linkedSetOf<String>()
|
||||||
get() {
|
val errorDownloadMap = hashMapOf<String, Int>()
|
||||||
var count = 0
|
|
||||||
cacheBookMap.forEach {
|
|
||||||
count += it.value.errorCount
|
|
||||||
}
|
|
||||||
return count
|
|
||||||
}
|
|
||||||
|
|
||||||
class CacheBookModel(var bookSource: BookSource, var book: Book) {
|
class CacheBookModel(var bookSource: BookSource, var book: Book) {
|
||||||
|
|
||||||
private val waitDownloadSet = linkedSetOf<Int>()
|
private val waitDownloadSet = linkedSetOf<Int>()
|
||||||
private val onDownloadSet = linkedSetOf<Int>()
|
private val onDownloadSet = linkedSetOf<Int>()
|
||||||
private val successDownloadSet = linkedSetOf<Int>()
|
|
||||||
private val errorDownloadMap = hashMapOf<Int, Int>()
|
|
||||||
private var isStopped = false
|
private var isStopped = false
|
||||||
private var waitingRetry = false
|
private var waitingRetry = false
|
||||||
|
|
||||||
val waitCount get() = waitDownloadSet.size
|
val waitCount get() = waitDownloadSet.size
|
||||||
val onDownloadCount get() = onDownloadSet.size
|
val onDownloadCount get() = onDownloadSet.size
|
||||||
val successCount get() = successDownloadSet.size
|
|
||||||
val errorCount get() = errorDownloadMap.size
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
|
postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
|
||||||
@ -178,29 +159,29 @@ object CacheBook {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
private fun onSuccess(index: Int) {
|
private fun onSuccess(chapter: BookChapter) {
|
||||||
onDownloadSet.remove(index)
|
onDownloadSet.remove(chapter.index)
|
||||||
successDownloadSet.add(index)
|
successDownloadSet.add(chapter.url)
|
||||||
errorDownloadMap.remove(index)
|
errorDownloadMap.remove(chapter.url)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
private fun onPreError(index: Int, error: Throwable) {
|
private fun onPreError(chapter: BookChapter, error: Throwable) {
|
||||||
waitingRetry = true
|
waitingRetry = true
|
||||||
if (error !is ConcurrentException) {
|
if (error !is ConcurrentException) {
|
||||||
errorDownloadMap[index] = (errorDownloadMap[index] ?: 0) + 1
|
errorDownloadMap[chapter.url] = (errorDownloadMap[chapter.url] ?: 0) + 1
|
||||||
}
|
}
|
||||||
onDownloadSet.remove(index)
|
onDownloadSet.remove(chapter.index)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
private fun onPostError(index: Int, error: Throwable, chapterTitle: String) {
|
private fun onPostError(chapter: BookChapter, error: Throwable) {
|
||||||
//重试3次
|
//重试3次
|
||||||
if ((errorDownloadMap[index] ?: 0) < 3 && !isStopped) {
|
if ((errorDownloadMap[chapter.url] ?: 0) < 3 && !isStopped) {
|
||||||
waitDownloadSet.add(index)
|
waitDownloadSet.add(chapter.index)
|
||||||
} else {
|
} else {
|
||||||
AppLog.put(
|
AppLog.put(
|
||||||
"下载${book.name}-${chapterTitle}失败\n${error.localizedMessage}",
|
"下载${book.name}-${chapter.title}失败\n${error.localizedMessage}",
|
||||||
error
|
error
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -208,9 +189,9 @@ object CacheBook {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
private fun onError(index: Int, error: Throwable, chapterTitle: String) {
|
private fun onError(chapter: BookChapter, error: Throwable) {
|
||||||
onPreError(index, error)
|
onPreError(chapter, error)
|
||||||
onPostError(index, error, chapterTitle)
|
onPostError(chapter, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
@ -266,12 +247,12 @@ object CacheBook {
|
|||||||
BookHelp.saveImages(bookSource, book, chapter, it)
|
BookHelp.saveImages(bookSource, book, chapter, it)
|
||||||
}
|
}
|
||||||
}.onSuccess {
|
}.onSuccess {
|
||||||
onSuccess(chapterIndex)
|
onSuccess(chapter)
|
||||||
}.onError {
|
}.onError {
|
||||||
onPreError(chapterIndex, it)
|
onPreError(chapter, it)
|
||||||
//出现错误等待一秒后重新加入待下载列表
|
//出现错误等待一秒后重新加入待下载列表
|
||||||
delay(1000)
|
delay(1000)
|
||||||
onPostError(chapterIndex, it, chapter.title)
|
onPostError(chapter, it)
|
||||||
}.onCancel {
|
}.onCancel {
|
||||||
onCancel(chapterIndex)
|
onCancel(chapterIndex)
|
||||||
}.onFinally {
|
}.onFinally {
|
||||||
@ -286,13 +267,13 @@ object CacheBook {
|
|||||||
chapter,
|
chapter,
|
||||||
context = context
|
context = context
|
||||||
).onSuccess { content ->
|
).onSuccess { content ->
|
||||||
onSuccess(chapterIndex)
|
onSuccess(chapter)
|
||||||
downloadFinish(chapter, content)
|
downloadFinish(chapter, content)
|
||||||
}.onError {
|
}.onError {
|
||||||
onPreError(chapterIndex, it)
|
onPreError(chapter, it)
|
||||||
//出现错误等待一秒后重新加入待下载列表
|
//出现错误等待一秒后重新加入待下载列表
|
||||||
delay(1000)
|
delay(1000)
|
||||||
onPostError(chapterIndex, it, chapter.title)
|
onPostError(chapter, it)
|
||||||
downloadFinish(chapter, "获取正文失败\n${it.localizedMessage}")
|
downloadFinish(chapter, "获取正文失败\n${it.localizedMessage}")
|
||||||
}.onCancel {
|
}.onCancel {
|
||||||
onCancel(chapterIndex)
|
onCancel(chapterIndex)
|
||||||
@ -315,10 +296,10 @@ object CacheBook {
|
|||||||
waitDownloadSet.remove(chapter.index)
|
waitDownloadSet.remove(chapter.index)
|
||||||
WebBook.getContent(scope, bookSource, book, chapter)
|
WebBook.getContent(scope, bookSource, book, chapter)
|
||||||
.onSuccess { content ->
|
.onSuccess { content ->
|
||||||
onSuccess(chapter.index)
|
onSuccess(chapter)
|
||||||
downloadFinish(chapter, content, resetPageOffset)
|
downloadFinish(chapter, content, resetPageOffset)
|
||||||
}.onError {
|
}.onError {
|
||||||
onError(chapter.index, it, chapter.title)
|
onError(chapter, it)
|
||||||
downloadFinish(chapter, "获取正文失败\n${it.localizedMessage}", resetPageOffset)
|
downloadFinish(chapter, "获取正文失败\n${it.localizedMessage}", resetPageOffset)
|
||||||
}.onCancel {
|
}.onCancel {
|
||||||
onCancel(chapter.index)
|
onCancel(chapter.index)
|
||||||
|
@ -17,7 +17,11 @@ import io.legado.app.utils.activityPendingIntent
|
|||||||
import io.legado.app.utils.postEvent
|
import io.legado.app.utils.postEvent
|
||||||
import io.legado.app.utils.servicePendingIntent
|
import io.legado.app.utils.servicePendingIntent
|
||||||
import io.legado.app.utils.toastOnUi
|
import io.legado.app.utils.toastOnUi
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.Job
|
||||||
|
import kotlinx.coroutines.asCoroutineDispatcher
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.isActive
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import splitties.init.appCtx
|
import splitties.init.appCtx
|
||||||
import java.util.concurrent.Executors
|
import java.util.concurrent.Executors
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
@ -51,6 +55,8 @@ class CacheBookService : BaseService() {
|
|||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
isRun = true
|
isRun = true
|
||||||
|
CacheBook.successDownloadSet.clear()
|
||||||
|
CacheBook.errorDownloadMap.clear()
|
||||||
launch {
|
launch {
|
||||||
while (isActive) {
|
while (isActive) {
|
||||||
delay(1000)
|
delay(1000)
|
||||||
@ -81,6 +87,8 @@ class CacheBookService : BaseService() {
|
|||||||
cachePool.close()
|
cachePool.close()
|
||||||
CacheBook.cacheBookMap.forEach { it.value.stop() }
|
CacheBook.cacheBookMap.forEach { it.value.stop() }
|
||||||
CacheBook.cacheBookMap.clear()
|
CacheBook.cacheBookMap.clear()
|
||||||
|
CacheBook.successDownloadSet.clear()
|
||||||
|
CacheBook.errorDownloadMap.clear()
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
postEvent(EventBus.UP_DOWNLOAD, "")
|
postEvent(EventBus.UP_DOWNLOAD, "")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user