Skip to content

Commit

Permalink
Make undo button work in StudyOptions (ankidroid#12852)
Browse files Browse the repository at this point in the history
  • Loading branch information
alyoshenka authored Nov 27, 2022
1 parent 3f47ecf commit ebd845f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import androidx.annotation.VisibleForTesting
import androidx.appcompat.content.res.AppCompatResources
import androidx.appcompat.widget.Toolbar
import androidx.core.text.HtmlCompat
import androidx.core.view.MenuItemCompat
import androidx.fragment.app.Fragment
import com.ichi2.anim.ActivityTransitionAnimation
import com.ichi2.anim.ActivityTransitionAnimation.slide
Expand All @@ -48,6 +49,7 @@ import com.ichi2.libanki.Collection
import com.ichi2.libanki.Consts
import com.ichi2.libanki.Decks
import com.ichi2.libanki.Utils
import com.ichi2.ui.RtlCompliantActionProvider
import com.ichi2.utils.FragmentFactoryUtils.instantiate
import com.ichi2.utils.HtmlUtils.convertNewlinesToHtml
import com.ichi2.utils.KotlinCleanup
Expand Down Expand Up @@ -384,6 +386,11 @@ class StudyOptionsFragment : Fragment(), Toolbar.OnMenuItemClickListener {
}
// Switch on or off unbury depending on if there are cards to unbury
menu.findItem(R.id.action_unbury).isVisible = col != null && col!!.sched.haveBuried()
// Set the proper click target for the undo button's ActionProvider
val undoActionProvider: RtlCompliantActionProvider? = MenuItemCompat.getActionProvider(
menu.findItem(R.id.action_undo)
) as? RtlCompliantActionProvider
undoActionProvider?.clickHandler = { _, menuItem -> onMenuItemClick(menuItem) }
// Switch on or off undo depending on whether undo is available
if (col == null || !col!!.undoAvailable()) {
menu.findItem(R.id.action_undo).isVisible = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ class RtlCompliantActionProvider(context: Context) : ActionProviderCompat(contex
@VisibleForTesting
val mActivity: Activity

/**
* The action to perform when clicking the associated menu item. By default this delegates to
* the activity's [Activity.onOptionsItemSelected] callback, but a custom action can be set and
* used instead.
*/
var clickHandler: (Activity, MenuItem) -> Unit = { activity, menuItem ->
activity.onOptionsItemSelected(menuItem)
}

override fun onCreateActionView(forItem: MenuItem): View {
val actionView = ImageButton(context, null, R.attr.actionButtonStyle)
TooltipCompat.setTooltipText(actionView, forItem.title)
Expand All @@ -42,7 +51,7 @@ class RtlCompliantActionProvider(context: Context) : ActionProviderCompat(contex
if (!forItem.isEnabled) {
return@setOnClickListener
}
mActivity.onOptionsItemSelected(forItem)
clickHandler(mActivity, forItem)
}
return actionView
}
Expand Down

0 comments on commit ebd845f

Please sign in to comment.