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 onBackPressed deprecation of CardTemplateEditor #17599

Merged
merged 1 commit into from
Dec 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
22 changes: 10 additions & 12 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.CheckResult
Expand Down Expand Up @@ -132,6 +133,12 @@ open class CardTemplateEditor :
* This occurs when the view is big enough.
*/
private var fragmented = false
val displayDiscardChangesCallback =
object : OnBackPressedCallback(false) {
override fun handleOnBackPressed() {
showDiscardChangesDialog()
}
}

// ----------------------------------------------------------------------------
// Listeners
Expand Down Expand Up @@ -187,6 +194,7 @@ open class CardTemplateEditor :

// Open TemplatePreviewerFragment if in fragmented mode
loadTemplatePreviewerFragmentIfFragmented()
onBackPressedDispatcher.addCallback(this, displayDiscardChangesCallback)
}

/**
Expand Down Expand Up @@ -230,20 +238,9 @@ open class CardTemplateEditor :
}
}

@Suppress("deprecation") // onBackPressed
@Deprecated("Deprecated in Java")
override fun onBackPressed() {
if (modelHasChanged()) {
showDiscardChangesDialog()
} else {
super.onBackPressed()
}
}

@Suppress("DEPRECATION")
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
onBackPressed()
onBackPressedDispatcher.onBackPressed()
return true
}
return super.onOptionsItemSelected(item)
Expand Down Expand Up @@ -547,6 +544,7 @@ open class CardTemplateEditor :
}
refreshFragmentRunnable = updateRunnable
refreshFragmentHandler.postDelayed(updateRunnable, REFRESH_PREVIEW_DELAY)
templateEditor.displayDiscardChangesCallback.isEnabled = modelHasChanged()
}

override fun beforeTextChanged(
Expand Down
Loading