From 9b819865287a134e08283e3a1d1fb466483611c1 Mon Sep 17 00:00:00 2001 From: Brayan Oliveira <69634269+brayandso@users.noreply.github.com> Date: Sat, 23 Mar 2024 18:59:55 -0300 Subject: [PATCH] =?UTF-8?q?s=C3=B3=20falta=20o=20hide=20ease?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/ichi2/anki/ReviewerTest.kt | 2 +- .../anki/previewer/CardViewerFragment.kt | 51 ++++++---- .../ui/windows/reviewer/ReviewerFragment.kt | 20 ++++ .../ui/windows/reviewer/ReviewerViewModel.kt | 94 ++++++++++++++++++- 4 files changed, 148 insertions(+), 19 deletions(-) diff --git a/AnkiDroid/src/androidTest/java/com/ichi2/anki/ReviewerTest.kt b/AnkiDroid/src/androidTest/java/com/ichi2/anki/ReviewerTest.kt index 0e90e8c8338f..1245949bf7ef 100755 --- a/AnkiDroid/src/androidTest/java/com/ichi2/anki/ReviewerTest.kt +++ b/AnkiDroid/src/androidTest/java/com/ichi2/anki/ReviewerTest.kt @@ -58,7 +58,7 @@ class ReviewerTest : InstrumentedTest() { @Test @Flaky(os = OS.ALL, "Fails on CI with timing issues frequently") - fun testCustomSchedulerWithCustomData() { + fun testCustomSchedulerWithCustomData() { // replicar col.cardStateCustomizer = """ states.good.normal.review.easeFactor = 3.0; diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/CardViewerFragment.kt b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/CardViewerFragment.kt index c021e5a91203..6b52e1f4a49d 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/CardViewerFragment.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/CardViewerFragment.kt @@ -20,6 +20,7 @@ import android.net.Uri import android.os.Bundle import android.view.View import android.webkit.CookieManager +import android.webkit.JsResult import android.webkit.WebChromeClient import android.webkit.WebResourceError import android.webkit.WebResourceRequest @@ -49,6 +50,7 @@ import timber.log.Timber abstract class CardViewerFragment(@LayoutRes layout: Int) : Fragment(layout) { protected abstract val viewModel: CardViewerViewModel protected abstract val webView: WebView + protected open val baseUrl = "http://${AnkiServer.LOCALHOST}/" @CallSuper override fun onViewCreated(view: View, savedInstanceState: Bundle?) { @@ -83,7 +85,7 @@ abstract class CardViewerFragment(@LayoutRes layout: Int) : Fragment(layout) { domStorageEnabled = true } loadDataWithBaseURL( - "http://${AnkiServer.LOCALHOST}/", + baseUrl, stdHtml(requireContext(), Themes.currentTheme.isNightMode), "text/html", null, @@ -133,22 +135,7 @@ abstract class CardViewerFragment(@LayoutRes layout: Int) : Fragment(layout) { } override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean { - val urlString = request.url.toString() - if (urlString.startsWith("playsound:")) { - viewModel.playSoundFromUrl(urlString) - return true - } - if (urlString.startsWith("tts-voices:")) { - TtsVoicesDialogFragment().show(childFragmentManager, null) - return true - } - try { - openUrl(request.url) - return true - } catch (_: Exception) { - Timber.w("Could not open url") - } - return false + return this@CardViewerFragment.shouldOverrideUrlLoading(request) } override fun onReceivedError( @@ -163,8 +150,38 @@ abstract class CardViewerFragment(@LayoutRes layout: Int) : Fragment(layout) { } } + protected open fun shouldOverrideUrlLoading(request: WebResourceRequest): Boolean { + val urlString = request.url.toString() + if (urlString.startsWith("playsound:")) { + viewModel.playSoundFromUrl(urlString) + return true + } + if (urlString.startsWith("tts-voices:")) { + TtsVoicesDialogFragment().show(childFragmentManager, null) + return true + } + try { + openUrl(request.url) + return true + } catch (_: Exception) { + Timber.w("Could not open url") + } + return false + } + private fun onCreateWebChromeClient(): WebChromeClient { return object : WebChromeClient() { + + override fun onJsAlert( + view: WebView?, + url: String?, + message: String?, + result: JsResult? + ): Boolean { + requireActivity().showSnackbar(message!!) + return super.onJsAlert(view, url, message, result) + } + private lateinit var customView: View // used for displaying `