mirror of
https://github.com/gedoor/legado.git
synced 2024-08-30 09:23:26 +08:00
优化
This commit is contained in:
parent
31dafd48d4
commit
1c589b2987
@ -24,11 +24,11 @@ data class DictRule(
|
||||
/**
|
||||
* 搜索字典
|
||||
*/
|
||||
suspend fun search(word: String): String? {
|
||||
suspend fun search(word: String): String {
|
||||
val analyzeUrl = AnalyzeUrl(urlRule, key = word)
|
||||
val body = analyzeUrl.getStrResponseAwait().body
|
||||
if (showRule.isBlank()) {
|
||||
return body
|
||||
return body!!
|
||||
}
|
||||
val analyzeRule = AnalyzeRule()
|
||||
return analyzeRule.getString(showRule, mContent = body)
|
||||
|
@ -60,15 +60,14 @@ class DictDialog() : BaseDialogFragment(R.layout.dialog_dict) {
|
||||
override fun onTabSelected(tab: TabLayout.Tab) {
|
||||
val dictRule = tab.tag as DictRule
|
||||
binding.rotateLoading.visible()
|
||||
viewModel.dict(dictRule, word!!)
|
||||
viewModel.dict(dictRule, word!!) {
|
||||
binding.rotateLoading.inVisible()
|
||||
binding.tvDict.setHtml(it)
|
||||
}
|
||||
}
|
||||
})
|
||||
viewModel.dictHtmlData.observe(viewLifecycleOwner) {
|
||||
binding.rotateLoading.inVisible()
|
||||
binding.tvDict.setHtml(it)
|
||||
}
|
||||
viewModel.initData {
|
||||
viewModel.dictRules?.forEach {
|
||||
it.forEach {
|
||||
binding.tabLayout.addTab(binding.tabLayout.newTab().apply {
|
||||
text = it.name
|
||||
tag = it
|
||||
|
@ -1,32 +1,35 @@
|
||||
package io.legado.app.ui.dict
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import io.legado.app.base.BaseViewModel
|
||||
import io.legado.app.data.entities.DictRule
|
||||
import io.legado.app.help.DefaultData
|
||||
import io.legado.app.utils.toastOnUi
|
||||
import io.legado.app.help.coroutine.Coroutine
|
||||
|
||||
class DictViewModel(application: Application) : BaseViewModel(application) {
|
||||
var dictRules: List<DictRule>? = null
|
||||
var dictHtmlData: MutableLiveData<String> = MutableLiveData()
|
||||
|
||||
fun initData(onSuccess: () -> Unit) {
|
||||
private var dictJob: Coroutine<String>? = null
|
||||
|
||||
fun initData(onSuccess: (List<DictRule>) -> Unit) {
|
||||
execute {
|
||||
DefaultData.dictRules
|
||||
}.onSuccess {
|
||||
dictRules = it
|
||||
onSuccess.invoke()
|
||||
onSuccess.invoke(it)
|
||||
}
|
||||
}
|
||||
|
||||
fun dict(dictRule: DictRule, word: String) {
|
||||
execute {
|
||||
fun dict(
|
||||
dictRule: DictRule,
|
||||
word: String,
|
||||
onFinally: (String) -> Unit
|
||||
) {
|
||||
dictJob?.cancel()
|
||||
dictJob = execute {
|
||||
dictRule.search(word)
|
||||
}.onSuccess {
|
||||
dictHtmlData.postValue(it)
|
||||
onFinally.invoke(it)
|
||||
}.onError {
|
||||
context.toastOnUi(it.localizedMessage)
|
||||
onFinally.invoke(it.localizedMessage ?: "ERROR")
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user