feat: 优化代码

pull/123/head
kunfei 5 years ago
parent 91cd8cd356
commit 6ce554173d
  1. 88
      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,35 +95,21 @@ object BookChapterList {
} }
Debug.log(bookSource.bookSourceUrl, "◇目录总页数:${nextUrlList.size}") Debug.log(bookSource.bookSourceUrl, "◇目录总页数:${nextUrlList.size}")
for (item in chapterDataList) { for (item in chapterDataList) {
Coroutine.async(scope = coroutineScope) { downloadToc(
val nextBody = AnalyzeUrl( coroutineScope,
ruleUrl = item.nextUrl, item,
book = book, book,
headerMapF = bookSource.getHeaderMap() bookSource,
).getResponseAwait().body analyzeRule,
val nextChapterData = analyzeChapterList( tocRule,
analyzeRule.setContent(nextBody, item.nextUrl), listRule,
book.bookUrl, item.nextUrl, tocRule, listRule, bookSource, chapterList,
false chapterDataList,
) {
synchronized(chapterDataList) { block.resume(finish(book, chapterList, reverse))
val isFinished = addChapterListIsFinish( }, {
chapterDataList, block.cancel(it)
item, })
nextChapterData.chapterList
)
if (isFinished) {
chapterDataList.forEach { item ->
item.chapterList?.let {
chapterList.addAll(it)
}
}
block.resume(finish(book, chapterList, reverse))
}
}
}.onError {
block.resumeWithException(it)
}
} }
} }
} }
@ -132,6 +118,50 @@ object BookChapterList {
} }
} }
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) {
val nextBody = AnalyzeUrl(
ruleUrl = chapterData.nextUrl,
book = book,
headerMapF = bookSource.getHeaderMap()
).getResponseAwait().body
val nextChapterData = analyzeChapterList(
analyzeRule.setContent(nextBody, chapterData.nextUrl),
book.bookUrl, chapterData.nextUrl, tocRule, listRule, bookSource,
false
)
synchronized(chapterDataList) {
val isFinished = addChapterListIsFinish(
chapterDataList,
chapterData,
nextChapterData.chapterList
)
if (isFinished) {
chapterDataList.forEach { item ->
item.chapterList?.let {
chapterList.addAll(it)
}
}
onFinish()
}
}
}.onError {
onError(it)
}
}
private fun addChapterListIsFinish( private fun addChapterListIsFinish(
chapterDataList: ArrayList<ChapterData<String>>, chapterDataList: ArrayList<ChapterData<String>>,
chapterData: ChapterData<String>, chapterData: ChapterData<String>,

@ -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