Skip to content

Commit

Permalink
fix onBackPressed() deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
Prince-kushwaha committed Dec 12, 2024
1 parent d36795a commit 7487ddf
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import android.widget.CheckBox
import android.widget.ListView
import android.widget.Spinner
import android.widget.TextView
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.annotation.CheckResult
Expand Down Expand Up @@ -283,6 +284,26 @@ open class CardBrowser :
private var oldCardTopOffset = 0
private var shouldRestoreScroll = false
private var postAutoScroll = false
private val onBackCallbackMethod =
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
when {
isDrawerOpen -> {
isEnabled = false
onBackPressedDispatcher.onBackPressed()
isEnabled = true
}
viewModel.isInMultiSelectMode -> viewModel.endMultiSelectMode()
else -> {
Timber.i("Back key pressed")
val data = Intent()
// Add reload flag to result intent so that schedule reset when returning to note editor
data.putExtra(NoteEditor.RELOAD_REQUIRED_EXTRA_KEY, reloadRequired)
closeCardBrowser(RESULT_OK, data)
}
}
}
}

init {
ChangeManager.subscribe(this)
Expand Down Expand Up @@ -442,6 +463,7 @@ open class CardBrowser :
}

setupFlows()
onBackPressedDispatcher.addCallback(onBackCallbackMethod)
registerOnForgetHandler { viewModel.queryAllSelectedCardIds() }
}

Expand Down Expand Up @@ -926,22 +948,6 @@ open class CardBrowser :
super.onDestroy()
}

@Deprecated("Deprecated in Java")
@Suppress("DEPRECATION")
override fun onBackPressed() {
when {
isDrawerOpen -> super.onBackPressed()
viewModel.isInMultiSelectMode -> viewModel.endMultiSelectMode()
else -> {
Timber.i("Back key pressed")
val data = Intent()
// Add reload flag to result intent so that schedule reset when returning to note editor
data.putExtra(NoteEditor.RELOAD_REQUIRED_EXTRA_KEY, reloadRequired)
closeCardBrowser(RESULT_OK, data)
}
}
}

override fun onPause() {
super.onPause()
// If the user entered something into the search, but didn't press "search", clear this.
Expand Down

0 comments on commit 7487ddf

Please sign in to comment.