diff --git a/app/src/main/java/io/legado/app/model/WebBook.kt b/app/src/main/java/io/legado/app/model/WebBook.kt index 5318a812d..ddbf15523 100644 --- a/app/src/main/java/io/legado/app/model/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/WebBook.kt @@ -43,11 +43,13 @@ class WebBook(private val bookSource: BookSource) { } } - fun getChapterList(book: Book, success: (List) -> Unit) { - Coroutine.async { + fun getChapterList(book: Book): Coroutine> { + return Coroutine.async { val analyzeUrl = AnalyzeUrl(book = book, ruleUrl = book.tocUrl) val response = analyzeUrl.getResponseAsync().await() - BookChapterList.analyzeChapterList(this, book, response, bookSource, analyzeUrl, success) + BookChapterList.analyzeChapterList(this, book, response, bookSource, analyzeUrl) } } + + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 020547d0a..8f0ddb328 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -89,8 +89,7 @@ class AnalyzeUrl( val matcher = pagePattern.matcher(ruleUrl) while (matcher.find()) { val pages = - matcher.group(1).split(",".toRegex()).dropLastWhile { it.isEmpty() } - .toTypedArray() + matcher.group(1).splitNotBlank(",") ruleUrl = if (page <= pages.size) { ruleUrl.replace(matcher.group(), pages[page - 1].trim { it <= ' ' }) } else { @@ -148,7 +147,7 @@ class AnalyzeUrl( } when (method) { Method.GET -> { - urlArray = url.split("\\?".toRegex()) + urlArray = url.split("?") url = urlArray[0] if (urlArray.size > 1) { analyzeFields(urlArray[1]) @@ -179,9 +178,9 @@ class AnalyzeUrl( @Throws(Exception::class) private fun analyzeFields(fieldsTxt: String) { queryStr = fieldsTxt - val queryS = fieldsTxt.split("&".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() + val queryS = fieldsTxt.splitNotBlank("&") for (query in queryS) { - val queryM = query.split("=".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() + val queryM = query.splitNotBlank("=") val value = if (queryM.size > 1) queryM[1] else "" if (TextUtils.isEmpty(charset)) { if (NetworkUtils.hasUrlEncoded(value)) { diff --git a/app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt b/app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt index ad2d8694c..61afa35c1 100644 --- a/app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt +++ b/app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt @@ -10,20 +10,18 @@ import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.utils.NetworkUtils import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import retrofit2.Response -import java.util.concurrent.atomic.AtomicInteger object BookChapterList { - fun analyzeChapterList( + suspend fun analyzeChapterList( coroutineScope: CoroutineScope, book: Book, response: Response, bookSource: BookSource, - analyzeUrl: AnalyzeUrl, - success: (List) -> Unit - ) { + analyzeUrl: AnalyzeUrl + ): List { val chapterList = arrayListOf() val baseUrl: String = NetworkUtils.getUrl(response) val body: String? = response.body() @@ -63,35 +61,27 @@ object BookChapterList { } } if (reverse) chapterList.reverse() - success(chapterList) } else if (chapterData.nextUrl.size > 1) { - val chapterDataList = arrayListOf>() for (item in chapterData.nextUrl) { if (!nextUrlList.contains(item)) { - val data = ChapterData(nextUrl = item) - chapterDataList.add(data) - } - } - val successCount = AtomicInteger(0) - for (item in chapterDataList) { - coroutineScope.launch { - val nextResponse = AnalyzeUrl(ruleUrl = item.nextUrl, book = book).getResponseAsync().await() - val nextChapterData = - analyzeChapterList(nextResponse.body() ?: "", item.nextUrl, tocRule, listRule, book) - item.chapterList = nextChapterData.chapterList - val nowCount = successCount.incrementAndGet() - if (nowCount == chapterDataList.size) { - for (newItem in chapterDataList) { - newItem.chapterList?.let { - chapterList.addAll(it) - } + withContext(coroutineScope.coroutineContext) { + val nextResponse = AnalyzeUrl(ruleUrl = item, book = book).getResponseAsync().await() + val nextChapterData = analyzeChapterList( + nextResponse.body() ?: "", + item, + tocRule, + listRule, + book + ) + nextChapterData.chapterList?.let { + chapterList.addAll(it) } - if (reverse) chapterList.reverse() - success(chapterList) } } } + if (reverse) chapterList.reverse() } + return chapterList } diff --git a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt index 491f9f7af..a849dd80b 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt @@ -8,9 +8,11 @@ import io.legado.app.base.BaseViewModel import io.legado.app.data.api.CommonHttpApi import io.legado.app.data.entities.SearchBook import io.legado.app.help.http.HttpHelper +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.delay import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext class SearchViewModel(application: Application) : BaseViewModel(application) { @@ -25,11 +27,17 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { } } + val c = execute { + + Log.e("TAG1", "start") + val response: String = HttpHelper.getApiService( "http://www.baidu.com" ).get("http://www.baidu.com").await() + Log.e("TAG1", "result: $response") + delay(2000L) response @@ -80,6 +88,27 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { // c.cancel() } + + + launch { + val list = test() + println("size: ${list.size} $list") + } + + + } + + suspend fun test(): MutableList { + val list = mutableListOf() + repeat(10) { + withContext(Dispatchers.IO) { + val response: String = HttpHelper.getApiService( + "http://www.baidu.com" + ).get("http://www.baidu.com").await() + list.add(response) + } + } + return list } }