Skip to content
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
78 changes: 42 additions & 36 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 @@ -49,6 +51,10 @@ class StudyOptionsActivity :
ChangeManager.Subscriber {
private var undoState = UndoState()

init {
ChangeManager.subscribe(this)
}
Comment on lines +54 to +56

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.

Realized that the undo button was not disappearing if there was nothing to undo; forgot to do a ChangeManager.subscribe


override fun onCreate(savedInstanceState: Bundle?) {
if (showedActivityFailedScreen(savedInstanceState)) {
return
Expand All @@ -62,6 +68,7 @@ class StudyOptionsActivity :
loadStudyOptionsFragment()
}
setResult(RESULT_OK)
addMenuProvider(menuProvider)

setFragmentResultListener(REQUEST_KEY) { _, bundle ->
when (CustomStudyAction.fromBundle(bundle)) {
Expand Down Expand Up @@ -91,10 +98,43 @@ 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)
// TODO: Ideally, the undo button should be owned by StudyOptionsFragment or be provided by a unified UndoMenuProvider
// Checking the current fragment from the activity to decide whether the button should be visible is hacky; see #21339
Comment on lines +119 to +120

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.

TODO added!

undoMenuItem.isVisible = undoState.hasAction && (currentFragment is StudyOptionsFragment)
undoMenuItem.title = undoState.label
}

override fun onMenuItemSelected(item: MenuItem): Boolean =
when (item.itemId) {
R.id.action_undo -> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Add logs here

Timber.i("Undoing last action from study options screen")
launchCatchingTask {
undoAndShowSnackbar()
}
true
}
else -> false
}
}

private fun loadStudyOptionsFragment() {
val currentFragment = StudyOptionsFragment()
supportFragmentManager.commit {
Expand All @@ -105,39 +145,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 @@ -148,7 +155,6 @@ class StudyOptionsActivity :
handler: Any?,
) {
refreshUndoState()
(currentFragment as? StudyOptionsFragment)?.refreshInterface()
}

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