Skip to content

Commit

Permalink
Fix onBackPressed() deprecation for CardTemplateBrowserAppearanceEditor
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lukstbit committed Dec 9, 2024
1 parent 2c01ed0 commit 13d35ab
Showing 1 changed file with 29 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -73,34 +93,22 @@ 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 -> {}
}
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()
}
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 13d35ab

Please sign in to comment.