From 28f40e2a3b883954d0b17977fd71c2c8c119cfc6 Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Fri, 6 May 2022 06:44:03 +0800 Subject: [PATCH] =?UTF-8?q?refactor=EF=BC=9A=E6=9B=B4=E6=96=B0=E7=95=8C?= =?UTF-8?q?=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/association/VerificationCodeDialog.kt | 30 +++++++++++----- .../legado/app/ui/browser/WebViewActivity.kt | 8 ++--- .../io/legado/app/ui/browser/WebViewModel.kt | 14 +++++--- .../layout/dialog_verification_code_view.xml | 36 +++---------------- app/src/main/res/menu/verification_code.xml | 13 +++++++ 5 files changed, 52 insertions(+), 49 deletions(-) create mode 100644 app/src/main/res/menu/verification_code.xml diff --git a/app/src/main/java/io/legado/app/ui/association/VerificationCodeDialog.kt b/app/src/main/java/io/legado/app/ui/association/VerificationCodeDialog.kt index a38f17cfc..85a16fdfd 100644 --- a/app/src/main/java/io/legado/app/ui/association/VerificationCodeDialog.kt +++ b/app/src/main/java/io/legado/app/ui/association/VerificationCodeDialog.kt @@ -4,6 +4,8 @@ import android.annotation.SuppressLint import android.os.Bundle import android.view.View import android.view.ViewGroup +import android.view.MenuItem +import androidx.appcompat.widget.Toolbar import com.bumptech.glide.request.RequestOptions import io.legado.app.R import io.legado.app.base.BaseDialogFragment @@ -13,8 +15,7 @@ import io.legado.app.help.glide.ImageLoader import io.legado.app.help.glide.OkHttpModelLoader import io.legado.app.lib.theme.primaryColor import io.legado.app.ui.widget.dialog.PhotoDialog -import io.legado.app.utils.setLayout -import io.legado.app.utils.showDialogFragment +import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding /** @@ -23,7 +24,7 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding * val key = "${sourceOrigin ?: ""}_verificationResult" * CacheManager.get(key) */ -class VerificationCodeDialog() : BaseDialogFragment(R.layout.dialog_verification_code_view) { +class VerificationCodeDialog() : BaseDialogFragment(R.layout.dialog_verification_code_view), Toolbar.OnMenuItemClickListener { constructor(imageUrl: String, sourceOrigin: String? = null) : this() { arguments = Bundle().apply { @@ -41,6 +42,7 @@ class VerificationCodeDialog() : BaseDialogFragment(R.layout.dialog_verification @SuppressLint("CheckResult") override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { + initMenu() binding.run { toolBar.setBackgroundColor(primaryColor) val sourceOrigin = arguments?.getString("sourceOrigin") @@ -61,17 +63,29 @@ class VerificationCodeDialog() : BaseDialogFragment(R.layout.dialog_verification showDialogFragment(PhotoDialog(imageUrl, sourceOrigin)) } } - tvOk.setOnClickListener { + } + } + + private fun initMenu() { + binding.toolBar.setOnMenuItemClickListener(this) + binding.toolBar.inflateMenu(R.menu.verification_code) + binding.toolBar.menu.applyTint(requireContext()) + } + + @SuppressLint("InflateParams") + override fun onMenuItemClick(item: MenuItem): Boolean { + when (item.itemId) { + R.id.menu_ok -> { + val sourceOrigin = arguments?.getString("sourceOrigin") + val key = "${sourceOrigin}_verificationResult" val verificationCode = binding.verificationCode.text.toString() verificationCode.let { CacheManager.putMemory(key, it) dismiss() } - } - tvCancel.setOnClickListener { - dismiss() - } + } } + return false } override fun onDestroy() { diff --git a/app/src/main/java/io/legado/app/ui/browser/WebViewActivity.kt b/app/src/main/java/io/legado/app/ui/browser/WebViewActivity.kt index 3973b2023..055d3509c 100644 --- a/app/src/main/java/io/legado/app/ui/browser/WebViewActivity.kt +++ b/app/src/main/java/io/legado/app/ui/browser/WebViewActivity.kt @@ -68,8 +68,9 @@ class WebViewActivity : VMBaseActivity() { R.id.menu_copy_url -> sendToClip(viewModel.baseUrl) R.id.menu_ok -> { if (viewModel.sourceVerificationEnable) { - binding.titleBar.snackbar(R.string.ok) - finish() + viewModel.saveVerificationResult { + finish() + } } } } @@ -182,9 +183,6 @@ class WebViewActivity : VMBaseActivity() { override fun onDestroy() { super.onDestroy() - runBlocking { - viewModel.saveVerificationResult() - } binding.webView.destroy() } diff --git a/app/src/main/java/io/legado/app/ui/browser/WebViewModel.kt b/app/src/main/java/io/legado/app/ui/browser/WebViewModel.kt index f3fb99798..0ad527a09 100644 --- a/app/src/main/java/io/legado/app/ui/browser/WebViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/browser/WebViewModel.kt @@ -83,11 +83,15 @@ class WebViewModel(application: Application) : BaseViewModel(application) { } } - fun saveVerificationResult() { - if (sourceVerificationEnable) { - val key = "${sourceOrigin}_verificationResult" - html = AnalyzeUrl(baseUrl, headerMapF = headerMap).getStrResponse(useWebView = false).body - CacheManager.putMemory(key, html ?: "") + fun saveVerificationResult(success: () -> Unit) { + execute { + if (sourceVerificationEnable) { + val key = "${sourceOrigin}_verificationResult" + html = AnalyzeUrl(baseUrl, headerMapF = headerMap).getStrResponseAwait(useWebView = false).body + CacheManager.putMemory(key, html ?: "") + } + }.onSuccess { + success.invoke() } } diff --git a/app/src/main/res/layout/dialog_verification_code_view.xml b/app/src/main/res/layout/dialog_verification_code_view.xml index c218ba54d..707e28051 100644 --- a/app/src/main/res/layout/dialog_verification_code_view.xml +++ b/app/src/main/res/layout/dialog_verification_code_view.xml @@ -10,7 +10,9 @@ android:id="@+id/tool_bar" android:layout_width="match_parent" android:layout_height="wrap_content" + android:elevation="5dp" android:theme="?attr/actionBarStyle" + android:background="@color/background_menu" app:title="@string/input_verification_code" app:popupTheme="@style/AppTheme.PopupOverlay" app:titleTextAppearance="@style/ToolbarTitle" /> @@ -20,18 +22,15 @@ android:scaleType="fitXY" android:layout_width="match_parent" android:layout_height="50dp" - android:paddingLeft="12dp" - android:paddingRight="12dp" - android:paddingTop="3dp" + android:padding="3dp" android:background="?android:attr/selectableItemBackgroundBorderless" tools:ignore="UnusedAttribute" /> + android:padding="3dp" + android:layout_marginTop="8dp"> - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/menu/verification_code.xml b/app/src/main/res/menu/verification_code.xml new file mode 100644 index 000000000..5723c1b13 --- /dev/null +++ b/app/src/main/res/menu/verification_code.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file