mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -5,9 +5,13 @@ import android.text.method.LinkMovementMethod
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.viewModels
|
||||
import com.google.android.material.tabs.TabLayout
|
||||
import io.legado.app.R
|
||||
import io.legado.app.base.BaseDialogFragment
|
||||
import io.legado.app.data.entities.DictRule
|
||||
import io.legado.app.databinding.DialogDictBinding
|
||||
import io.legado.app.lib.theme.accentColor
|
||||
import io.legado.app.lib.theme.backgroundColor
|
||||
import io.legado.app.utils.invisible
|
||||
import io.legado.app.utils.setHtml
|
||||
import io.legado.app.utils.setLayout
|
||||
@@ -28,6 +32,8 @@ class DictDialog() : BaseDialogFragment(R.layout.dialog_dict) {
|
||||
private val viewModel by viewModels<DictViewModel>()
|
||||
private val binding by viewBinding(DialogDictBinding::bind)
|
||||
|
||||
private var word: String? = null
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
|
||||
@@ -35,19 +41,41 @@ class DictDialog() : BaseDialogFragment(R.layout.dialog_dict) {
|
||||
|
||||
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
|
||||
binding.tvDict.movementMethod = LinkMovementMethod()
|
||||
val word = arguments?.getString("word")
|
||||
word = arguments?.getString("word")
|
||||
if (word.isNullOrEmpty()) {
|
||||
toastOnUi(R.string.cannot_empty)
|
||||
dismiss()
|
||||
return
|
||||
}
|
||||
binding.tabLayout.setBackgroundColor(backgroundColor)
|
||||
binding.tabLayout.setSelectedTabIndicatorColor(accentColor)
|
||||
binding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
|
||||
override fun onTabReselected(tab: TabLayout.Tab) {
|
||||
|
||||
}
|
||||
|
||||
override fun onTabUnselected(tab: TabLayout.Tab) {
|
||||
|
||||
}
|
||||
|
||||
override fun onTabSelected(tab: TabLayout.Tab) {
|
||||
val dictRule = tab.tag as DictRule
|
||||
viewModel.dict(dictRule, word!!)
|
||||
}
|
||||
})
|
||||
viewModel.dictHtmlData.observe(viewLifecycleOwner) {
|
||||
binding.rotateLoading.invisible()
|
||||
binding.tvDict.setHtml(it)
|
||||
}
|
||||
viewModel.dict(word)
|
||||
viewModel.initData {
|
||||
viewModel.dictRules?.forEach {
|
||||
binding.tabLayout.addTab(binding.tabLayout.newTab().apply {
|
||||
text = it.name
|
||||
tag = it
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -3,46 +3,27 @@ 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 java.util.regex.Pattern
|
||||
|
||||
class DictViewModel(application: Application) : BaseViewModel(application) {
|
||||
|
||||
var dictRules: List<DictRule>? = null
|
||||
var dictHtmlData: MutableLiveData<String> = MutableLiveData()
|
||||
|
||||
fun dict(word: String) {
|
||||
if (isChinese(word)) {
|
||||
baiduDict(word)
|
||||
} else {
|
||||
haiciDict(word)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 海词英文词典
|
||||
*
|
||||
* @param word
|
||||
*/
|
||||
private fun haiciDict(word: String) {
|
||||
fun initData(onSuccess: () -> Unit) {
|
||||
execute {
|
||||
DefaultData.dictRules[1].search(word)
|
||||
DefaultData.dictRules
|
||||
}.onSuccess {
|
||||
dictHtmlData.postValue(it)
|
||||
}.onError {
|
||||
context.toastOnUi(it.localizedMessage)
|
||||
dictRules = it
|
||||
onSuccess.invoke()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 百度汉语词典
|
||||
*
|
||||
* @param word
|
||||
*/
|
||||
private fun baiduDict(word: String) {
|
||||
fun dict(dictRule: DictRule, word: String) {
|
||||
execute {
|
||||
DefaultData.dictRules[0].search(word)
|
||||
dictRule.search(word)
|
||||
}.onSuccess {
|
||||
dictHtmlData.postValue(it)
|
||||
}.onError {
|
||||
|
||||
@@ -69,23 +69,26 @@ class DictRuleActivity : VMBaseActivity<ActivityDictRuleBinding, DictRuleViewMod
|
||||
}
|
||||
|
||||
override fun selectAll(selectAll: Boolean) {
|
||||
TODO("Not yet implemented")
|
||||
adapter.selectAll()
|
||||
}
|
||||
|
||||
override fun revertSelection() {
|
||||
TODO("Not yet implemented")
|
||||
adapter.revertSelection()
|
||||
}
|
||||
|
||||
override fun onMenuItemClick(item: MenuItem?): Boolean {
|
||||
TODO("Not yet implemented")
|
||||
override fun onMenuItemClick(item: MenuItem): Boolean {
|
||||
when (item.itemId) {
|
||||
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
override fun update(vararg rule: DictRule) {
|
||||
TODO("Not yet implemented")
|
||||
viewModel.upsert(*rule)
|
||||
}
|
||||
|
||||
override fun delete(rule: DictRule) {
|
||||
TODO("Not yet implemented")
|
||||
viewModel.delete(rule)
|
||||
}
|
||||
|
||||
override fun edit(rule: DictRule) {
|
||||
@@ -105,7 +108,10 @@ class DictRuleActivity : VMBaseActivity<ActivityDictRuleBinding, DictRuleViewMod
|
||||
}
|
||||
|
||||
override fun upCountView() {
|
||||
TODO("Not yet implemented")
|
||||
binding.selectActionBar.upCountView(
|
||||
adapter.selection.size,
|
||||
adapter.itemCount
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,18 +1,31 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="16dp">
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<com.google.android.material.tabs.TabLayout
|
||||
android:id="@+id/tab_layout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
tools:ignore="SpeakableTextPresentCheck" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_dict"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="16dp"
|
||||
app:layout_constraintTop_toBottomOf="@id/tab_layout" />
|
||||
|
||||
<io.legado.app.ui.widget.anima.RotateLoading
|
||||
android:id="@+id/rotate_loading"
|
||||
android:layout_width="60dp"
|
||||
android:layout_height="60dp"
|
||||
android:layout_gravity="center" />
|
||||
android:layout_gravity="center"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/tab_layout" />
|
||||
|
||||
</FrameLayout>
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
Reference in New Issue
Block a user