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 27, 2024
1 parent d36795a commit 2df6e67
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
66 changes: 50 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,12 +42,14 @@ 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
import androidx.annotation.ColorInt
import androidx.annotation.MainThread
import androidx.annotation.VisibleForTesting
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.widget.SearchView
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
Expand Down Expand Up @@ -284,6 +286,31 @@ open class CardBrowser :
private var shouldRestoreScroll = false
private var postAutoScroll = false

private val drawerBackCallback =
object : OnBackPressedCallback(false) {
override fun handleOnBackPressed() {
closeDrawer()
}
}

private val multiSelectBackCallback =
object : OnBackPressedCallback(false) {
override fun handleOnBackPressed() {
viewModel.endMultiSelectMode()
}
}

private val closeCardBrowserBackCallback =
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
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 @@ -443,6 +470,27 @@ open class CardBrowser :

setupFlows()
registerOnForgetHandler { viewModel.queryAllSelectedCardIds() }
val drawerListener =
object : ActionBarDrawerToggle(
this,
drawerLayout,
R.string.drawer_open,
R.string.drawer_close,
) {
override fun onDrawerClosed(drawerView: View) {
super.onDrawerClosed(drawerView)
drawerBackCallback.isEnabled = false
}

override fun onDrawerOpened(drawerView: View) {
super.onDrawerOpened(drawerView)
drawerBackCallback.isEnabled = true
}
}
drawerLayout.addDrawerListener(drawerListener)
onBackPressedDispatcher.addCallback(this, closeCardBrowserBackCallback)
onBackPressedDispatcher.addCallback(this, multiSelectBackCallback)
onBackPressedDispatcher.addCallback(this, drawerBackCallback)
}

@Suppress("UNUSED_PARAMETER")
Expand Down Expand Up @@ -498,6 +546,7 @@ open class CardBrowser :
// show title and hide spinner
actionBarTitle.visibility = View.VISIBLE
deckSpinnerSelection.setSpinnerVisibility(View.GONE)
multiSelectBackCallback.isEnabled = true
} else {
Timber.d("end multiselect mode")
// If view which was originally selected when entering multi-select is visible then maintain its position
Expand All @@ -507,6 +556,7 @@ open class CardBrowser :
cardsAdapter.notifyDataSetChanged()
deckSpinnerSelection.setSpinnerVisibility(View.VISIBLE)
actionBarTitle.visibility = View.GONE
multiSelectBackCallback.isEnabled = false
}
// reload the actionbar using the multi-select mode actionbar
invalidateOptionsMenu()
Expand Down Expand Up @@ -926,22 +976,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
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ abstract class NavigationDrawerActivity :
private var navButtonGoesBack = false

// Navigation drawer list item entries
private lateinit var drawerLayout: DrawerLayout
lateinit var drawerLayout: DrawerLayout
private var navigationView: NavigationView? = null
lateinit var drawerToggle: ActionBarDrawerToggle
private set
Expand Down Expand Up @@ -413,7 +413,7 @@ abstract class NavigationDrawerActivity :
drawerLayout.openDrawer(GravityCompat.START, animationEnabled())
}

private fun closeDrawer() {
protected fun closeDrawer() {
drawerLayout.closeDrawer(GravityCompat.START, animationEnabled())
}

Expand Down

0 comments on commit 2df6e67

Please sign in to comment.