From 13d35ab257d67147b44429493491649b020a9bbe Mon Sep 17 00:00:00 2001 From: lukstbit <52494258+lukstbit@users.noreply.github.com> Date: Mon, 9 Dec 2024 16:45:20 +0200 Subject: [PATCH] Fix onBackPressed() deprecation for CardTemplateBrowserAppearanceEditor The previous code was showing a discard dialog if there were any changes, meaning that the user entered something different in the input boxed than what text was the screen initialized with. The fix introduces two listeners for the input boxes to use them as a dynamic listener for when there's change and implicitly could be BACK changes as well. The code still shows the discard changes dialog for the UP button in the toolbar like before. --- .../CardTemplateBrowserAppearanceEditor.kt | 54 ++++++++++--------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateBrowserAppearanceEditor.kt b/AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateBrowserAppearanceEditor.kt index d566d79d0e39..74c62650261d 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateBrowserAppearanceEditor.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateBrowserAppearanceEditor.kt @@ -21,8 +21,11 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.widget.EditText +import androidx.activity.OnBackPressedCallback +import androidx.activity.addCallback import androidx.annotation.CheckResult import androidx.appcompat.app.AlertDialog +import androidx.core.widget.doAfterTextChanged import com.ichi2.anki.dialogs.DiscardChangesDialog import com.ichi2.utils.message import com.ichi2.utils.negativeButton @@ -40,6 +43,14 @@ import timber.log.Timber class CardTemplateBrowserAppearanceEditor : AnkiActivity() { private lateinit var questionEditText: EditText private lateinit var answerEditText: EditText + + // start with the callback disabled as there aren't any changes yet + private val discardChangesCallback = object : OnBackPressedCallback(false) { + override fun handleOnBackPressed() { + showDiscardChangesDialog() + } + } + override fun onCreate(savedInstanceState: Bundle?) { if (showedActivityFailedScreen(savedInstanceState)) { return @@ -52,6 +63,15 @@ class CardTemplateBrowserAppearanceEditor : AnkiActivity() { return } initializeUiFromBundle(bundle) + // default result, only changed to RESULT_OK if actually saving changes + setResult(RESULT_CANCELED) + onBackPressedDispatcher.addCallback(discardChangesCallback) + questionEditText.doAfterTextChanged { _ -> + discardChangesCallback.isEnabled = hasChanges() + } + answerEditText.doAfterTextChanged { _ -> + discardChangesCallback.isEnabled = hasChanges() + } } override fun onCreateOptionsMenu(menu: Menu): Boolean { @@ -73,7 +93,11 @@ class CardTemplateBrowserAppearanceEditor : AnkiActivity() { } android.R.id.home -> { Timber.i("Back Pressed") - closeWithDiscardWarning() + if (hasChanges()) { + showDiscardChangesDialog() + } else { + finish() // the result was already set to RESULT_CANCELLED + } return true } else -> {} @@ -81,26 +105,10 @@ class CardTemplateBrowserAppearanceEditor : AnkiActivity() { return super.onOptionsItemSelected(item) } - @Suppress("DEPRECATION", "Deprecated in API34+dependencies for predictive back feature") - @Deprecated("Deprecated in Java") - override fun onBackPressed() { - Timber.i("Back Button Pressed") - super.onBackPressed() - closeWithDiscardWarning() - } - - private fun closeWithDiscardWarning() { - if (hasChanges()) { - Timber.i("Changes detected - displaying discard warning dialog") - showDiscardChangesDialog() - } else { - discardChangesAndClose() - } - } - private fun showDiscardChangesDialog() { DiscardChangesDialog.showDialog(this) { - discardChangesAndClose() + Timber.i("Changes discarded, finishing...") + finish() } } @@ -129,6 +137,8 @@ class CardTemplateBrowserAppearanceEditor : AnkiActivity() { answerEditText = findViewById(R.id.answer_format) answerEditText.setText(bundle.getString(INTENT_ANSWER_FORMAT)) + discardChangesCallback.isEnabled = hasChanges() + enableToolbar() setTitle(R.string.card_template_browser_appearance_title) } @@ -157,12 +167,6 @@ class CardTemplateBrowserAppearanceEditor : AnkiActivity() { saveAndExit() } - private fun discardChangesAndClose() { - Timber.i("Closing and discarding changes") - setResult(RESULT_CANCELED) - finish() - } - private fun saveAndExit() { Timber.i("Save and Exit") val data = Intent().apply {