diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 4726949cb..c5657da1e 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -46,6 +46,7 @@ import io.legado.app.ui.book.read.page.provider.TextPageFactory import io.legado.app.ui.book.searchContent.SearchContentActivity import io.legado.app.ui.book.source.edit.BookSourceEditActivity import io.legado.app.ui.book.toc.TocActivityResult +import io.legado.app.ui.dict.DictDialog import io.legado.app.ui.login.SourceLoginActivity import io.legado.app.ui.replace.ReplaceRuleActivity import io.legado.app.ui.replace.edit.ReplaceEditActivity @@ -531,6 +532,10 @@ class ReadBookActivity : ReadBookBaseActivity(), openSearchActivity(selectedText) return true } + R.id.menu_dict -> { + DictDialog.dict(supportFragmentManager, selectedText) + return true + } } return false } diff --git a/app/src/main/java/io/legado/app/ui/dict/DictDialog.kt b/app/src/main/java/io/legado/app/ui/dict/DictDialog.kt index d4f9fefab..573e708ae 100644 --- a/app/src/main/java/io/legado/app/ui/dict/DictDialog.kt +++ b/app/src/main/java/io/legado/app/ui/dict/DictDialog.kt @@ -1,11 +1,21 @@ package io.legado.app.ui.dict import android.os.Bundle +import android.text.Html +import android.view.LayoutInflater import android.view.View +import android.view.ViewGroup import androidx.fragment.app.FragmentManager import androidx.fragment.app.viewModels +import io.legado.app.R import io.legado.app.base.BaseDialogFragment +import io.legado.app.databinding.DialogDictBinding +import io.legado.app.utils.toastOnUi +import io.legado.app.utils.viewbindingdelegate.viewBinding +/** + * 词典 + */ class DictDialog : BaseDialogFragment() { companion object { @@ -21,10 +31,33 @@ class DictDialog : BaseDialogFragment() { } private val viewModel by viewModels() + private val binding by viewBinding(DialogDictBinding::bind) - override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { - viewModel.word = arguments?.getString("word") ?: "" + override fun onStart() { + super.onStart() + dialog?.window + ?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle?, + ): View? { + return inflater.inflate(R.layout.dialog_dict, container) + } + override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { + val word = arguments?.getString("word") + if (word.isNullOrEmpty()) { + toastOnUi(R.string.cannot_empty) + dismiss() + return + } + viewModel.dictHtmlData.observe(viewLifecycleOwner) { + binding.tvDict.text = Html.fromHtml(it) + } + viewModel.dict(word) } diff --git a/app/src/main/java/io/legado/app/ui/dict/DictViewModel.kt b/app/src/main/java/io/legado/app/ui/dict/DictViewModel.kt index 8b694776f..774876f76 100644 --- a/app/src/main/java/io/legado/app/ui/dict/DictViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/dict/DictViewModel.kt @@ -1,11 +1,30 @@ package io.legado.app.ui.dict import android.app.Application -import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.MutableLiveData +import io.legado.app.base.BaseViewModel +import io.legado.app.help.http.get +import io.legado.app.help.http.newCallStrResponse +import io.legado.app.help.http.okHttpClient +import org.jsoup.Jsoup -class DictViewModel(application: Application) : AndroidViewModel(application) { +class DictViewModel(application: Application) : BaseViewModel(application) { - var word: String? = null + var dictHtmlData: MutableLiveData = MutableLiveData() + fun dict(word: String) { + execute { + val body = okHttpClient.newCallStrResponse { + get("http://apii.dict.cn/mini.php", mapOf(Pair("q", word))) + }.body + val jsoup = Jsoup.parse(body) + jsoup.body() + }.onSuccess { + dictHtmlData.postValue(it.html()) + }.onError { + toastOnUi(it.localizedMessage) + } + + } } \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_dict.xml b/app/src/main/res/layout/dialog_dict.xml new file mode 100644 index 000000000..e04adae59 --- /dev/null +++ b/app/src/main/res/layout/dialog_dict.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/content_select_action.xml b/app/src/main/res/menu/content_select_action.xml index ec918aec4..63a4bb184 100644 --- a/app/src/main/res/menu/content_select_action.xml +++ b/app/src/main/res/menu/content_select_action.xml @@ -17,6 +17,10 @@ android:id="@+id/menu_aloud" android:title="@string/read_aloud" /> + + diff --git a/app/src/main/res/values-ja-rJP/strings.xml b/app/src/main/res/values-ja-rJP/strings.xml index e24a01296..961d104c5 100644 --- a/app/src/main/res/values-ja-rJP/strings.xml +++ b/app/src/main/res/values-ja-rJP/strings.xml @@ -846,4 +846,5 @@ 导出文件名 重置 url为空 + 字典 diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index 05869cbb9..efde516ba 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -846,4 +846,5 @@ Exportar o nome do arquivo 重置 url为空 + 字典 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index be7e0ac7b..05c592dc2 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -844,5 +844,6 @@ 导出文件名 重置 url为空 + 字典 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index dfce22319..b781f4ee1 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -845,5 +845,6 @@ 匯出檔案名 重設 url为空 + 字典 diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index ce903631d..509525cbc 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -845,5 +845,6 @@ 导出文件名 重置 url为空 + 字典 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 722607042..722ac6550 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -846,5 +846,6 @@ Export file name Reset Null url + 字典