Skip to content

Commit

Permalink
Fix onBackPressed() deprecation in SharedDecksActivity
Browse files Browse the repository at this point in the history
Moved the previous back handling code from the activity to the related
fragment. The predictive BACK navigation is done by changing the enabled
status of the back callback every time isDownloadInProgress is updated.

There was also a direct call to the activity onBackPressed() which was
replaced by the equivalent onBackPressedDispatcher.onBackPressed().
  • Loading branch information
lukstbit committed Dec 9, 2024
1 parent 908441e commit 2c01ed0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 41 deletions.
38 changes: 0 additions & 38 deletions AnkiDroid/src/main/java/com/ichi2/anki/SharedDecksActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -240,44 +240,6 @@ class SharedDecksActivity : AnkiActivity() {
onBackPressedDispatcher.addCallback(onBackPressedCallback)
}

/**
* If download screen is open:
* If download is in progress: Show download cancellation dialog
* If download is not in progress: Close the download screen
* Otherwise, close the WebView.
*/
@Deprecated("Deprecated in Java")
@Suppress("deprecation") // onBackPressed
override fun onBackPressed() {
when {
sharedDecksDownloadFragmentExists() -> {
supportFragmentManager.findFragmentByTag(SHARED_DECKS_DOWNLOAD_FRAGMENT)?.let {
if ((it as SharedDecksDownloadFragment).isDownloadInProgress) {
Timber.i("Back pressed when download is in progress, show cancellation confirmation dialog")
// Show cancel confirmation dialog if download is in progress
it.showCancelConfirmationDialog()
} else {
Timber.i("Back pressed when download is not in progress but download screen is open, close fragment")
// Remove fragment
supportFragmentManager.commit {
remove(it)
}
}
}
supportFragmentManager.popBackStackImmediate()
}
else -> {
Timber.i("Back pressed which would lead to closing of the WebView")
super.onBackPressed()
}
}
}

private fun sharedDecksDownloadFragmentExists(): Boolean {
val sharedDecksDownloadFragment = supportFragmentManager.findFragmentByTag(SHARED_DECKS_DOWNLOAD_FRAGMENT)
return sharedDecksDownloadFragment != null && sharedDecksDownloadFragment.isAdded
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.download_shared_decks_menu, menu)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import android.webkit.CookieManager
import android.widget.Button
import android.widget.ProgressBar
import android.widget.TextView
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider
Expand Down Expand Up @@ -83,6 +84,11 @@ class SharedDecksDownloadFragment : Fragment(R.layout.fragment_shared_decks_down
var isDownloadInProgress = false

private var downloadCancelConfirmationDialog: AlertDialog? = null
private val onBackPressedCallback = object : OnBackPressedCallback(isDownloadInProgress) {
override fun handleOnBackPressed() {
showCancelConfirmationDialog()
}
}

companion object {
const val DOWNLOAD_PROGRESS_CHECK_DELAY = 1000L
Expand Down Expand Up @@ -134,6 +140,7 @@ class SharedDecksDownloadFragment : Fragment(R.layout.fragment_shared_decks_down
cancelButton.visibility = View.VISIBLE
tryAgainButton.visibility = View.GONE
}
requireActivity().onBackPressedDispatcher.addCallback(onBackPressedCallback)
}

/**
Expand Down Expand Up @@ -170,6 +177,7 @@ class SharedDecksDownloadFragment : Fragment(R.layout.fragment_shared_decks_down
downloadId = downloadManager.enqueue(downloadRequest)
fileName = currentFileName
isDownloadInProgress = true
onBackPressedCallback.isEnabled = isDownloadInProgress
Timber.d("Download ID -> $downloadId")
Timber.d("File name -> $fileName")
view?.findViewById<TextView>(R.id.downloading_title)?.text = getString(R.string.downloading_file, fileName)
Expand Down Expand Up @@ -473,20 +481,21 @@ class SharedDecksDownloadFragment : Fragment(R.layout.fragment_shared_decks_down

unregisterReceiver()
isDownloadInProgress = false
onBackPressedCallback.isEnabled = isDownloadInProgress

// If the cancel confirmation dialog is being shown and the download is no longer in progress, then remove the dialog.
removeCancelConfirmationDialog()
}

@Suppress("deprecation") // onBackPressed
fun showCancelConfirmationDialog() {
private fun showCancelConfirmationDialog() {
downloadCancelConfirmationDialog = AlertDialog.Builder(requireContext()).create {
setTitle(R.string.cancel_download_question_title)
setPositiveButton(R.string.dialog_yes) { _, _ ->
downloadManager.remove(downloadId)
unregisterReceiver()
isDownloadInProgress = false
activity?.onBackPressed()
onBackPressedCallback.isEnabled = isDownloadInProgress
activity?.onBackPressedDispatcher?.onBackPressed()
}
setNegativeButton(R.string.dialog_no) { _, _ ->
downloadCancelConfirmationDialog?.dismiss()
Expand Down

0 comments on commit 2c01ed0

Please sign in to comment.