Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Deck Options: "Back" works incorrectly if the manual is open #15145

Merged
merged 3 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ abstract class AbstractFlashcardViewer :
}

/** Invoked by [CardViewerWebClient.onPageFinished] */
override fun onPageFinished() {
override fun onPageFinished(view: WebView) {
// intentionally blank
}

Expand Down Expand Up @@ -2451,7 +2451,7 @@ abstract class AbstractFlashcardViewer :
pageRenderStopwatch.logElapsed()
Timber.d("Java onPageFinished triggered: %s", url)
// onPageFinished will be called multiple times if the WebView redirects by setting window.location.href
onPageFinishedCallback?.onPageFinished()
onPageFinishedCallback?.onPageFinished(view)
view.loadUrl("javascript:onPageFinished();")
}

Expand Down
3 changes: 2 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/AnkiActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ open class AnkiActivity : AppCompatActivity, SimpleMessageDialogListener {
}

protected open fun onActionBarBackPressed(): Boolean {
finish()
Timber.v("onActionBarBackPressed")
onBackPressedDispatcher.onBackPressed()
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package com.ichi2.anki

import android.webkit.WebView

/**
* Intended to be used with [android.webkit.WebViewClient.onPageFinished]
*/
interface OnPageFinishedCallback {
fun onPageFinished()
fun interface OnPageFinishedCallback {
/** @see android.webkit.WebViewClient.onPageFinished */
fun onPageFinished(view: WebView)
}
5 changes: 3 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/Reviewer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import android.os.Parcelable
import android.text.SpannableString
import android.text.style.UnderlineSpan
import android.view.*
import android.webkit.WebView
import android.widget.*
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.*
Expand Down Expand Up @@ -1026,8 +1027,8 @@ open class Reviewer :
}
}

override fun onPageFinished() {
super.onPageFinished()
override fun onPageFinished(view: WebView) {
super.onPageFinished(view)
onFlagChanged()
onMarkChanged()
if (!displayAnswer) {
Expand Down
19 changes: 19 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/pages/DeckOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,47 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.webkit.WebView
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.FragmentActivity
import anki.collection.OpChanges
import com.ichi2.anki.CollectionManager
import com.ichi2.anki.OnPageFinishedCallback
import com.ichi2.anki.R
import com.ichi2.anki.SingleFragmentActivity
import com.ichi2.annotations.NeedsTest
import com.ichi2.libanki.undoableOp
import com.ichi2.libanki.updateDeckConfigsRaw
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber

@NeedsTest("pressing back: icon + button should go to the previous screen")
@NeedsTest("15130: pressing back: icon + button should return to options if the manual is open")
class DeckOptions : PageFragment() {
override val title: String
get() = resources.getString(R.string.menu__deck_options)
override val pageName = "deck-options"
override lateinit var webViewClient: PageWebViewClient
override var webChromeClient = PageChromeClient()

// handle going back from the manual
private val onBackCallback = object : OnBackPressedCallback(false) {
override fun handleOnBackPressed() {
Timber.v("webView: navigating back")
webView.goBack()
}
}

override fun onCreate(savedInstanceState: Bundle?) {
val deckId = arguments?.getLong(ARG_DECK_ID)
?: throw Exception("missing deck ID")
webViewClient = DeckOptionsWebClient(deckId)
super.onCreate(savedInstanceState)
requireActivity().onBackPressedDispatcher.addCallback(this, onBackCallback)
webViewClient.onPageFinishedCallback = OnPageFinishedCallback { view ->
Timber.v("canGoBack: %b", view.canGoBack())
onBackCallback.isEnabled = view.canGoBack()
}
}

class DeckOptionsWebClient(val deckId: Long) : PageWebViewClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.core.view.isVisible
import com.google.android.material.color.MaterialColors
import com.ichi2.anki.OnPageFinishedCallback
import com.ichi2.utils.toRGBHex
import timber.log.Timber

Expand All @@ -32,6 +33,8 @@ open class PageWebViewClient : WebViewClient() {
/** Wait for the provided promise to complete before showing the WebView */
open val promiseToWaitFor: String? = null

var onPageFinishedCallback: OnPageFinishedCallback? = null

override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
view?.let { webView ->
Expand All @@ -45,7 +48,7 @@ open class PageWebViewClient : WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
if (view == null) return

onPageFinishedCallback?.onPageFinished(view)
if (promiseToWaitFor == null) {
/** [PageFragment.webView] is invisible by default to avoid flashes while
* the page is loaded, and can be made visible again after it finishes loading */
Expand Down
Loading