feat: 优化代码

pull/123/head
kunfei 5 years ago
parent 91cd8cd356
commit 6ce554173d
  1. 54
      app/src/main/java/io/legado/app/model/webBook/BookChapterList.kt
  2. 8
      app/src/main/java/io/legado/app/ui/filechooser/FilePicker.kt

@ -95,21 +95,57 @@ object BookChapterList {
} }
Debug.log(bookSource.bookSourceUrl, "◇目录总页数:${nextUrlList.size}") Debug.log(bookSource.bookSourceUrl, "◇目录总页数:${nextUrlList.size}")
for (item in chapterDataList) { for (item in chapterDataList) {
downloadToc(
coroutineScope,
item,
book,
bookSource,
analyzeRule,
tocRule,
listRule,
chapterList,
chapterDataList,
{
block.resume(finish(book, chapterList, reverse))
}, {
block.cancel(it)
})
}
}
}
} catch (e: Exception) {
block.resumeWithException(e)
}
}
private fun downloadToc(
coroutineScope: CoroutineScope,
chapterData: ChapterData<String>,
book: Book,
bookSource: BookSource,
analyzeRule: AnalyzeRule,
tocRule: TocRule,
listRule: String,
chapterList: ArrayList<BookChapter>,
chapterDataList: ArrayList<ChapterData<String>>,
onFinish: () -> Unit,
onError: (e: Throwable) -> Unit
) {
Coroutine.async(scope = coroutineScope) { Coroutine.async(scope = coroutineScope) {
val nextBody = AnalyzeUrl( val nextBody = AnalyzeUrl(
ruleUrl = item.nextUrl, ruleUrl = chapterData.nextUrl,
book = book, book = book,
headerMapF = bookSource.getHeaderMap() headerMapF = bookSource.getHeaderMap()
).getResponseAwait().body ).getResponseAwait().body
val nextChapterData = analyzeChapterList( val nextChapterData = analyzeChapterList(
analyzeRule.setContent(nextBody, item.nextUrl), analyzeRule.setContent(nextBody, chapterData.nextUrl),
book.bookUrl, item.nextUrl, tocRule, listRule, bookSource, book.bookUrl, chapterData.nextUrl, tocRule, listRule, bookSource,
false false
) )
synchronized(chapterDataList) { synchronized(chapterDataList) {
val isFinished = addChapterListIsFinish( val isFinished = addChapterListIsFinish(
chapterDataList, chapterDataList,
item, chapterData,
nextChapterData.chapterList nextChapterData.chapterList
) )
if (isFinished) { if (isFinished) {
@ -118,17 +154,11 @@ object BookChapterList {
chapterList.addAll(it) chapterList.addAll(it)
} }
} }
block.resume(finish(book, chapterList, reverse)) onFinish()
} }
} }
}.onError { }.onError {
block.resumeWithException(it) onError(it)
}
}
}
}
} catch (e: Exception) {
block.resumeWithException(e)
} }
} }

@ -21,7 +21,7 @@ object FilePicker {
selectList.removeAt(0) selectList.removeAt(0)
} }
items(selectList) { _, index -> items(selectList) { _, index ->
when (index) { when (if (default == null) index - 1 else index) {
0 -> default?.invoke() 0 -> default?.invoke()
1 -> { 1 -> {
try { try {
@ -60,7 +60,7 @@ object FilePicker {
selectList.removeAt(0) selectList.removeAt(0)
} }
items(selectList) { _, index -> items(selectList) { _, index ->
when (index) { when (if (default == null) index - 1 else index) {
0 -> default?.invoke() 0 -> default?.invoke()
1 -> { 1 -> {
try { try {
@ -104,7 +104,7 @@ object FilePicker {
selectList.removeAt(0) selectList.removeAt(0)
} }
items(selectList) { _, index -> items(selectList) { _, index ->
when (index) { when (if (default == null) index - 1 else index) {
0 -> default?.invoke() 0 -> default?.invoke()
1 -> { 1 -> {
try { try {
@ -152,7 +152,7 @@ object FilePicker {
selectList.removeAt(0) selectList.removeAt(0)
} }
items(selectList) { _, index -> items(selectList) { _, index ->
when (index) { when (if (default == null) index - 1 else index) {
0 -> default?.invoke() 0 -> default?.invoke()
1 -> { 1 -> {
try { try {

Loading…
Cancel
Save