优化
This commit is contained in:
@@ -45,7 +45,7 @@ object Rss {
|
|||||||
)
|
)
|
||||||
val res = analyzeUrl.getStrResponseAwait()
|
val res = analyzeUrl.getStrResponseAwait()
|
||||||
checkRedirect(rssSource, res)
|
checkRedirect(rssSource, res)
|
||||||
return RssParserByRule.parseXML(sortName, sortUrl, res.body, rssSource, ruleData)
|
return RssParserByRule.parseXML(sortName, sortUrl, res.url, res.body, rssSource, ruleData)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getContent(
|
fun getContent(
|
||||||
@@ -79,6 +79,7 @@ object Rss {
|
|||||||
val analyzeRule = AnalyzeRule(rssArticle, rssSource)
|
val analyzeRule = AnalyzeRule(rssArticle, rssSource)
|
||||||
analyzeRule.setContent(res.body)
|
analyzeRule.setContent(res.body)
|
||||||
.setBaseUrl(NetworkUtils.getAbsoluteURL(rssArticle.origin, rssArticle.link))
|
.setBaseUrl(NetworkUtils.getAbsoluteURL(rssArticle.origin, rssArticle.link))
|
||||||
|
.setRedirectUrl(res.url)
|
||||||
return analyzeRule.getString(ruleContent)
|
return analyzeRule.getString(ruleContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import io.legado.app.model.analyzeRule.AnalyzeRule
|
|||||||
import io.legado.app.model.analyzeRule.RuleData
|
import io.legado.app.model.analyzeRule.RuleData
|
||||||
import io.legado.app.utils.NetworkUtils
|
import io.legado.app.utils.NetworkUtils
|
||||||
import splitties.init.appCtx
|
import splitties.init.appCtx
|
||||||
import java.util.*
|
import java.util.Locale
|
||||||
|
|
||||||
@Keep
|
@Keep
|
||||||
object RssParserByRule {
|
object RssParserByRule {
|
||||||
@@ -19,6 +19,7 @@ object RssParserByRule {
|
|||||||
fun parseXML(
|
fun parseXML(
|
||||||
sortName: String,
|
sortName: String,
|
||||||
sortUrl: String,
|
sortUrl: String,
|
||||||
|
redirectUrl: String,
|
||||||
body: String?,
|
body: String?,
|
||||||
rssSource: RssSource,
|
rssSource: RssSource,
|
||||||
ruleData: RuleData
|
ruleData: RuleData
|
||||||
@@ -40,7 +41,7 @@ object RssParserByRule {
|
|||||||
val articleList = mutableListOf<RssArticle>()
|
val articleList = mutableListOf<RssArticle>()
|
||||||
val analyzeRule = AnalyzeRule(ruleData, rssSource)
|
val analyzeRule = AnalyzeRule(ruleData, rssSource)
|
||||||
analyzeRule.setContent(body).setBaseUrl(sortUrl)
|
analyzeRule.setContent(body).setBaseUrl(sortUrl)
|
||||||
analyzeRule.setRedirectUrl(sortUrl)
|
analyzeRule.setRedirectUrl(redirectUrl)
|
||||||
var reverse = false
|
var reverse = false
|
||||||
if (ruleArticles.startsWith("-")) {
|
if (ruleArticles.startsWith("-")) {
|
||||||
reverse = true
|
reverse = true
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import io.legado.app.utils.FileDoc
|
|||||||
|
|
||||||
data class ImportBook(
|
data class ImportBook(
|
||||||
val file: FileDoc,
|
val file: FileDoc,
|
||||||
var isOnBookShelf: Boolean = LocalBook.isOnBookShelf(file.name)
|
var isOnBookShelf: Boolean = !file.isDir && LocalBook.isOnBookShelf(file.name)
|
||||||
) {
|
) {
|
||||||
val name get() = file.name
|
val name get() = file.name
|
||||||
val isDir get() = file.isDir
|
val isDir get() = file.isDir
|
||||||
|
|||||||
@@ -253,14 +253,13 @@ class ImportBookActivity : BaseImportBookActivity<ImportBookViewModel>(),
|
|||||||
binding.refreshProgressBar.isAutoLoading = true
|
binding.refreshProgressBar.isAutoLoading = true
|
||||||
scanDocJob?.cancel()
|
scanDocJob?.cancel()
|
||||||
scanDocJob = lifecycleScope.launch(IO) {
|
scanDocJob = lifecycleScope.launch(IO) {
|
||||||
viewModel.scanDoc(lastDoc, true) {
|
viewModel.scanDoc(lastDoc)
|
||||||
withContext(Main) {
|
withContext(Main) {
|
||||||
binding.refreshProgressBar.isAutoLoading = false
|
binding.refreshProgressBar.isAutoLoading = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private fun alertImportFileName() {
|
private fun alertImportFileName() {
|
||||||
alert(R.string.import_file_name) {
|
alert(R.string.import_file_name) {
|
||||||
|
|||||||
@@ -12,18 +12,23 @@ import io.legado.app.utils.FileDoc
|
|||||||
import io.legado.app.utils.delete
|
import io.legado.app.utils.delete
|
||||||
import io.legado.app.utils.getPrefInt
|
import io.legado.app.utils.getPrefInt
|
||||||
import io.legado.app.utils.list
|
import io.legado.app.utils.list
|
||||||
|
import io.legado.app.utils.mapParallel
|
||||||
import io.legado.app.utils.toastOnUi
|
import io.legado.app.utils.toastOnUi
|
||||||
import kotlinx.coroutines.Dispatchers.IO
|
import kotlinx.coroutines.Dispatchers.IO
|
||||||
import kotlinx.coroutines.Dispatchers.Main
|
import kotlinx.coroutines.Dispatchers.Main
|
||||||
import kotlinx.coroutines.FlowPreview
|
import kotlinx.coroutines.channels.Channel
|
||||||
|
import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED
|
||||||
import kotlinx.coroutines.channels.awaitClose
|
import kotlinx.coroutines.channels.awaitClose
|
||||||
import kotlinx.coroutines.flow.callbackFlow
|
import kotlinx.coroutines.flow.callbackFlow
|
||||||
|
import kotlinx.coroutines.flow.catch
|
||||||
|
import kotlinx.coroutines.flow.collect
|
||||||
|
import kotlinx.coroutines.flow.consumeAsFlow
|
||||||
import kotlinx.coroutines.flow.flowOn
|
import kotlinx.coroutines.flow.flowOn
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.isActive
|
import kotlinx.coroutines.flow.onEach
|
||||||
|
import kotlinx.coroutines.flow.takeWhile
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import java.util.Collections
|
import java.util.Collections
|
||||||
import kotlin.coroutines.coroutineContext
|
|
||||||
|
|
||||||
class ImportBookViewModel(application: Application) : BaseViewModel(application) {
|
class ImportBookViewModel(application: Application) : BaseViewModel(application) {
|
||||||
var rootDoc: FileDoc? = null
|
var rootDoc: FileDoc? = null
|
||||||
@@ -98,6 +103,8 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
|
|||||||
}.onError {
|
}.onError {
|
||||||
context.toastOnUi("添加书架失败,请尝试重新选择文件夹")
|
context.toastOnUi("添加书架失败,请尝试重新选择文件夹")
|
||||||
AppLog.put("添加书架失败\n${it.localizedMessage}", it)
|
AppLog.put("添加书架失败\n${it.localizedMessage}", it)
|
||||||
|
}.onSuccess {
|
||||||
|
context.toastOnUi("添加书架成功")
|
||||||
}.onFinally {
|
}.onFinally {
|
||||||
finally.invoke()
|
finally.invoke()
|
||||||
}
|
}
|
||||||
@@ -128,47 +135,32 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun scanDoc(
|
suspend fun scanDoc(fileDoc: FileDoc) {
|
||||||
fileDoc: FileDoc,
|
|
||||||
isRoot: Boolean,
|
|
||||||
finally: (suspend () -> Unit)? = null
|
|
||||||
) {
|
|
||||||
if (isRoot) {
|
|
||||||
dataCallback?.clear()
|
dataCallback?.clear()
|
||||||
}
|
val channel = Channel<FileDoc>(UNLIMITED)
|
||||||
if (!coroutineContext.isActive) {
|
var n = 1
|
||||||
finally?.invoke()
|
channel.trySend(fileDoc)
|
||||||
return
|
val list = arrayListOf<FileDoc>()
|
||||||
}
|
channel.consumeAsFlow()
|
||||||
kotlin.runCatching {
|
.mapParallel(64) { fileDoc ->
|
||||||
val list = ArrayList<FileDoc>()
|
fileDoc.list()!!
|
||||||
fileDoc.list()!!.forEach { docItem ->
|
}.onEach { fileDocs ->
|
||||||
if (!coroutineContext.isActive) {
|
n--
|
||||||
finally?.invoke()
|
list.clear()
|
||||||
return
|
fileDocs.forEach {
|
||||||
}
|
if (it.isDir) {
|
||||||
if (docItem.isDir) {
|
n++
|
||||||
scanDoc(docItem, false)
|
channel.trySend(it)
|
||||||
} else if (docItem.name.matches(bookFileRegex) || docItem.name.matches(
|
} else {
|
||||||
archiveFileRegex
|
list.add(it)
|
||||||
)
|
|
||||||
) {
|
|
||||||
list.add(docItem)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!coroutineContext.isActive) {
|
|
||||||
finally?.invoke()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (list.isNotEmpty()) {
|
|
||||||
dataCallback?.addItems(list)
|
dataCallback?.addItems(list)
|
||||||
}
|
}.takeWhile {
|
||||||
}.onFailure {
|
n > 0
|
||||||
|
}.catch {
|
||||||
context.toastOnUi("扫描文件夹出错\n${it.localizedMessage}")
|
context.toastOnUi("扫描文件夹出错\n${it.localizedMessage}")
|
||||||
}
|
}.collect()
|
||||||
if (isRoot) {
|
|
||||||
finally?.invoke()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updateCallBackFlow(filterKey: String?) {
|
fun updateCallBackFlow(filterKey: String?) {
|
||||||
|
|||||||
Reference in New Issue
Block a user