Skip to content

Commit

Permalink
DeckOption become closable if it's not loaded
Browse files Browse the repository at this point in the history
Currently, if you try to close the Deck Options before it's loaded,
the request `anki.deckOptionsPendingChanges()` can't be executed and
the deck options remain open.

With this change, the deck options is automatically closed. Indeed, if
the webview is not loaded, there was no change to save.

This is similar to Anki's implementation.

Co-authored-by: David Allison <[email protected]>
  • Loading branch information
Arthur-Milchior and david-allison committed Feb 4, 2025
1 parent 3f21e3c commit 36d7244
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions AnkiDroid/src/main/java/com/ichi2/anki/pages/DeckOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ import kotlinx.coroutines.withContext
import timber.log.Timber

@NeedsTest("15130: pressing back: icon + button should return to options if the manual is open")
@NeedsTest("17905: pressing back before the webpage is ready closes the screen")
class DeckOptions : PageFragment() {
private var webViewIsReady = false

/**
* Callback enabled when the manual is opened in the deck options.
* It requests the webview to go back to the Deck Options.
Expand All @@ -68,11 +71,16 @@ class DeckOptions : PageFragment() {
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
Timber.v("DeckOptions: requesting the webview to handle the user close request.")
webView.evaluateJavascript("anki.deckOptionsPendingChanges()") {
// Callback is handled in the WebView:
// * A 'discard changes' dialog may be shown, using confirm()
// * if no changes, or changes discarded, `deckOptionsRequireClose` is called
// which PostRequestHandler handles and calls on this fragment
if (webViewIsReady) {
webView.evaluateJavascript("anki.deckOptionsPendingChanges()") {
// Callback is handled in the WebView:
// * A 'discard changes' dialog may be shown, using confirm()
// * if no changes, or changes discarded, `deckOptionsRequireClose` is called
// which PostRequestHandler handles and calls on this fragment
}
} else {
// The webview is not yet loaded, no change could have occurred, we can safely close it.
actuallyClose()
}
}
}
Expand Down Expand Up @@ -221,6 +229,7 @@ class DeckOptions : PageFragment() {

fun onWebViewReady() {
Timber.d("WebView ready to receive input")
webViewIsReady = true
webView.isVisible = true
pageLoadingIndicator.isVisible = false
}
Expand Down

0 comments on commit 36d7244

Please sign in to comment.