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

Deck Options: add unconditional 'back' confirmation #15503

Merged
merged 2 commits into from
Feb 15, 2024
Merged
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
25 changes: 25 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 @@ -18,6 +18,7 @@ package com.ichi2.anki.pages
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.webkit.WebView
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.FragmentActivity
Expand All @@ -26,6 +27,7 @@ import com.ichi2.anki.CollectionManager
import com.ichi2.anki.OnPageFinishedCallback
import com.ichi2.anki.R
import com.ichi2.anki.SingleFragmentActivity
import com.ichi2.anki.dialogs.DiscardChangesDialog
import com.ichi2.annotations.NeedsTest
import com.ichi2.libanki.undoableOp
import com.ichi2.libanki.updateDeckConfigsRaw
Expand All @@ -35,6 +37,7 @@ 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")
@NeedsTest("saveAndExit closes screen")
class DeckOptions : PageFragment() {
override val title: String
get() = resources.getString(R.string.menu__deck_options)
Expand All @@ -48,10 +51,23 @@ class DeckOptions : PageFragment() {
}
}

// HACK: this is enabled unconditionally as we currently cannot get the 'changed' status
private val onBackSaveCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
Timber.v("DeckOptions: showing 'discard changes'")
DiscardChangesDialog.showDialog(requireContext()) {
Timber.i("OK button pressed to confirm discard changes")
this.isEnabled = false
requireActivity().onBackPressedDispatcher.onBackPressed()
}
}
}

override fun onCreateWebViewClient(savedInstanceState: Bundle?): PageWebViewClient {
val deckId = arguments?.getLong(ARG_DECK_ID)
?: throw Exception("missing deck ID")

requireActivity().onBackPressedDispatcher.addCallback(this, onBackSaveCallback)
david-allison marked this conversation as resolved.
Show resolved Hide resolved
requireActivity().onBackPressedDispatcher.addCallback(this, onBackCallback)
return DeckOptionsWebClient(deckId).apply {
onPageFinishedCallback = OnPageFinishedCallback { view ->
Expand All @@ -61,6 +77,15 @@ class DeckOptions : PageFragment() {
}
}

@Suppress("unused")
fun saveAndExit() {
// dispatch Ctrl+Enter
val downEvent = KeyEvent(0, 0, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER, 0, KeyEvent.META_CTRL_ON)
val upEvent = KeyEvent(0, 0, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER, 0, KeyEvent.META_CTRL_ON)
webView.dispatchKeyEvent(downEvent)
webView.dispatchKeyEvent(upEvent)
}

class DeckOptionsWebClient(val deckId: Long) : PageWebViewClient() {
override val promiseToWaitFor: String
get() = "\$deckOptions"
Expand Down
Loading