Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Jan 22, 2025
1 parent 5caa3b8 commit 36672b1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 50 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/io/legado/app/model/rss/Rss.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object Rss {
)
val res = analyzeUrl.getStrResponseAwait()
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(
Expand Down Expand Up @@ -79,6 +79,7 @@ object Rss {
val analyzeRule = AnalyzeRule(rssArticle, rssSource)
analyzeRule.setContent(res.body)
.setBaseUrl(NetworkUtils.getAbsoluteURL(rssArticle.origin, rssArticle.link))
.setRedirectUrl(res.url)
return analyzeRule.getString(ruleContent)
}

Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.analyzeRule.RuleData
import io.legado.app.utils.NetworkUtils
import splitties.init.appCtx
import java.util.*
import java.util.Locale

@Keep
object RssParserByRule {
Expand All @@ -19,6 +19,7 @@ object RssParserByRule {
fun parseXML(
sortName: String,
sortUrl: String,
redirectUrl: String,
body: String?,
rssSource: RssSource,
ruleData: RuleData
Expand All @@ -40,7 +41,7 @@ object RssParserByRule {
val articleList = mutableListOf<RssArticle>()
val analyzeRule = AnalyzeRule(ruleData, rssSource)
analyzeRule.setContent(body).setBaseUrl(sortUrl)
analyzeRule.setRedirectUrl(sortUrl)
analyzeRule.setRedirectUrl(redirectUrl)
var reverse = false
if (ruleArticles.startsWith("-")) {
reverse = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.legado.app.utils.FileDoc

data class ImportBook(
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 isDir get() = file.isDir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,9 @@ class ImportBookActivity : BaseImportBookActivity<ImportBookViewModel>(),
binding.refreshProgressBar.isAutoLoading = true
scanDocJob?.cancel()
scanDocJob = lifecycleScope.launch(IO) {
viewModel.scanDoc(lastDoc, true) {
withContext(Main) {
binding.refreshProgressBar.isAutoLoading = false
}
viewModel.scanDoc(lastDoc)
withContext(Main) {
binding.refreshProgressBar.isAutoLoading = false
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@ import io.legado.app.utils.FileDoc
import io.legado.app.utils.delete
import io.legado.app.utils.getPrefInt
import io.legado.app.utils.list
import io.legado.app.utils.mapParallel
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.Dispatchers.IO
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.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.map
import kotlinx.coroutines.isActive
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.takeWhile
import kotlinx.coroutines.withContext
import java.util.Collections
import kotlin.coroutines.coroutineContext

class ImportBookViewModel(application: Application) : BaseViewModel(application) {
var rootDoc: FileDoc? = null
Expand Down Expand Up @@ -98,6 +103,8 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
}.onError {
context.toastOnUi("添加书架失败,请尝试重新选择文件夹")
AppLog.put("添加书架失败\n${it.localizedMessage}", it)
}.onSuccess {
context.toastOnUi("添加书架成功")
}.onFinally {
finally.invoke()
}
Expand Down Expand Up @@ -128,47 +135,32 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
}
}

suspend fun scanDoc(
fileDoc: FileDoc,
isRoot: Boolean,
finally: (suspend () -> Unit)? = null
) {
if (isRoot) {
dataCallback?.clear()
}
if (!coroutineContext.isActive) {
finally?.invoke()
return
}
kotlin.runCatching {
val list = ArrayList<FileDoc>()
fileDoc.list()!!.forEach { docItem ->
if (!coroutineContext.isActive) {
finally?.invoke()
return
}
if (docItem.isDir) {
scanDoc(docItem, false)
} else if (docItem.name.matches(bookFileRegex) || docItem.name.matches(
archiveFileRegex
)
) {
list.add(docItem)
suspend fun scanDoc(fileDoc: FileDoc) {
dataCallback?.clear()
val channel = Channel<FileDoc>(UNLIMITED)
var n = 1
channel.trySend(fileDoc)
val list = arrayListOf<FileDoc>()
channel.consumeAsFlow()
.mapParallel(64) { fileDoc ->
fileDoc.list()!!
}.onEach { fileDocs ->
n--
list.clear()
fileDocs.forEach {
if (it.isDir) {
n++
channel.trySend(it)
} else {
list.add(it)
}
}
}
if (!coroutineContext.isActive) {
finally?.invoke()
return
}
if (list.isNotEmpty()) {
dataCallback?.addItems(list)
}
}.onFailure {
context.toastOnUi("扫描文件夹出错\n${it.localizedMessage}")
}
if (isRoot) {
finally?.invoke()
}
}.takeWhile {
n > 0
}.catch {
context.toastOnUi("扫描文件夹出错\n${it.localizedMessage}")
}.collect()
}

fun updateCallBackFlow(filterKey: String?) {
Expand Down

0 comments on commit 36672b1

Please sign in to comment.