Skip to content
Open
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
70 changes: 35 additions & 35 deletions AnkiDroid/src/main/java/com/ichi2/anki/StudyOptionsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package com.ichi2.anki
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import androidx.core.view.MenuItemCompat
import androidx.core.view.MenuProvider
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import androidx.lifecycle.lifecycleScope
Expand Down Expand Up @@ -62,6 +64,7 @@ class StudyOptionsActivity :
loadStudyOptionsFragment()
}
setResult(RESULT_OK)
addMenuProvider(menuProvider)

setFragmentResultListener(REQUEST_KEY) { _, bundle ->
when (CustomStudyAction.fromBundle(bundle)) {
Expand Down Expand Up @@ -91,10 +94,40 @@ class StudyOptionsActivity :
)
addToBackStack(null)
}
invalidateOptionsMenu()
invalidateMenu()
}
}

private val menuProvider: MenuProvider =
object : MenuProvider {
override fun onCreateMenu(
menu: Menu,
menuInflater: MenuInflater,
) {
menuInflater.inflate(R.menu.activity_study_options, menu)
val undoMenuItem = menu.findItem(R.id.action_undo)
val undoActionProvider = MenuItemCompat.getActionProvider(undoMenuItem) as? RtlCompliantActionProvider
undoActionProvider?.clickHandler = { _, menuItem -> onMenuItemSelected(menuItem) }
}

override fun onPrepareMenu(menu: Menu) {
val undoMenuItem = menu.findItem(R.id.action_undo)
undoMenuItem.isVisible = undoState.hasAction && (currentFragment is StudyOptionsFragment)
undoMenuItem.title = undoState.label
}

override fun onMenuItemSelected(item: MenuItem): Boolean =
when (item.itemId) {
R.id.action_undo -> {
launchCatchingTask {
undoAndShowSnackbar()
}
true
}
else -> false
}
}

private fun loadStudyOptionsFragment() {
val currentFragment = StudyOptionsFragment()
supportFragmentManager.commit {
Expand All @@ -105,39 +138,6 @@ class StudyOptionsActivity :
private val currentFragment: Fragment?
get() = supportFragmentManager.findFragmentById(R.id.studyoptions_frame)

override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.activity_study_options, menu)
val undoMenuItem = menu.findItem(R.id.action_undo)
val undoActionProvider = MenuItemCompat.getActionProvider(undoMenuItem) as? RtlCompliantActionProvider
// Set the proper click target for the undo button's ActionProvider
undoActionProvider?.clickHandler = { _, menuItem -> onOptionsItemSelected(menuItem) }
undoMenuItem.isVisible = undoState.hasAction && (currentFragment is StudyOptionsFragment)
undoMenuItem.title = undoState.label
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
onBackPressedDispatcher.onBackPressed()
true
}
R.id.action_undo -> {
launchCatchingTask {
undoAndShowSnackbar()
// TODO why are we going to the Reviewer from here? Desktop doesn't do this

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment was originally added by @lukstbit at dd4c35d. The code to open the reviewer was initially added in 3e7ad6a9e6. I see no reason to keep it. The behaviour of the undo button seems simpler if we just... only perform the undo. The desktop reviewer doesn't open the reviewer when undo is pressed from the study options view, so lukstbit's point is valid.

Reviewer
.getIntent(this@StudyOptionsActivity)
.apply { flags = Intent.FLAG_ACTIVITY_FORWARD_RESULT }
.also { startActivity(it) }
finish()
}
true
}
else -> return super.onOptionsItemSelected(item)
}
}

override fun onResume() {
super.onResume()
refreshUndoState()
Expand All @@ -162,7 +162,7 @@ class StudyOptionsActivity :
}
if (undoState != newUndoState) {
undoState = newUndoState
invalidateOptionsMenu()
invalidateMenu()
}
}
}
Expand Down
Loading