优化
This commit is contained in:
@@ -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 analyzeUrl = AnalyzeUrl(urlRule, key = word)
|
||||||
val body = analyzeUrl.getStrResponseAwait().body
|
val body = analyzeUrl.getStrResponseAwait().body
|
||||||
if (showRule.isBlank()) {
|
if (showRule.isBlank()) {
|
||||||
return body
|
return body!!
|
||||||
}
|
}
|
||||||
val analyzeRule = AnalyzeRule()
|
val analyzeRule = AnalyzeRule()
|
||||||
return analyzeRule.getString(showRule, mContent = body)
|
return analyzeRule.getString(showRule, mContent = body)
|
||||||
|
|||||||
@@ -60,15 +60,14 @@ class DictDialog() : BaseDialogFragment(R.layout.dialog_dict) {
|
|||||||
override fun onTabSelected(tab: TabLayout.Tab) {
|
override fun onTabSelected(tab: TabLayout.Tab) {
|
||||||
val dictRule = tab.tag as DictRule
|
val dictRule = tab.tag as DictRule
|
||||||
binding.rotateLoading.visible()
|
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.initData {
|
||||||
viewModel.dictRules?.forEach {
|
it.forEach {
|
||||||
binding.tabLayout.addTab(binding.tabLayout.newTab().apply {
|
binding.tabLayout.addTab(binding.tabLayout.newTab().apply {
|
||||||
text = it.name
|
text = it.name
|
||||||
tag = it
|
tag = it
|
||||||
|
|||||||
@@ -1,32 +1,35 @@
|
|||||||
package io.legado.app.ui.dict
|
package io.legado.app.ui.dict
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import androidx.lifecycle.MutableLiveData
|
|
||||||
import io.legado.app.base.BaseViewModel
|
import io.legado.app.base.BaseViewModel
|
||||||
import io.legado.app.data.entities.DictRule
|
import io.legado.app.data.entities.DictRule
|
||||||
import io.legado.app.help.DefaultData
|
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) {
|
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 {
|
execute {
|
||||||
DefaultData.dictRules
|
DefaultData.dictRules
|
||||||
}.onSuccess {
|
}.onSuccess {
|
||||||
dictRules = it
|
onSuccess.invoke(it)
|
||||||
onSuccess.invoke()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun dict(dictRule: DictRule, word: String) {
|
fun dict(
|
||||||
execute {
|
dictRule: DictRule,
|
||||||
|
word: String,
|
||||||
|
onFinally: (String) -> Unit
|
||||||
|
) {
|
||||||
|
dictJob?.cancel()
|
||||||
|
dictJob = execute {
|
||||||
dictRule.search(word)
|
dictRule.search(word)
|
||||||
}.onSuccess {
|
}.onSuccess {
|
||||||
dictHtmlData.postValue(it)
|
onFinally.invoke(it)
|
||||||
}.onError {
|
}.onError {
|
||||||
context.toastOnUi(it.localizedMessage)
|
onFinally.invoke(it.localizedMessage ?: "ERROR")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user