-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
refactor: use MenuProvider in StudyOptionsActivity, remove starting reviewer in undo #21339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -49,6 +51,10 @@ class StudyOptionsActivity : | |
| ChangeManager.Subscriber { | ||
| private var undoState = UndoState() | ||
|
|
||
| init { | ||
| ChangeManager.subscribe(this) | ||
| } | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| if (showedActivityFailedScreen(savedInstanceState)) { | ||
| return | ||
|
|
@@ -62,6 +68,7 @@ class StudyOptionsActivity : | |
| loadStudyOptionsFragment() | ||
| } | ||
| setResult(RESULT_OK) | ||
| addMenuProvider(menuProvider) | ||
|
|
||
| setFragmentResultListener(REQUEST_KEY) { _, bundle -> | ||
| when (CustomStudyAction.fromBundle(bundle)) { | ||
|
|
@@ -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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 -> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
|
@@ -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 | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
|
@@ -148,7 +155,6 @@ class StudyOptionsActivity : | |
| handler: Any?, | ||
| ) { | ||
| refreshUndoState() | ||
| (currentFragment as? StudyOptionsFragment)?.refreshInterface() | ||
| } | ||
|
|
||
| private fun refreshUndoState() { | ||
|
|
@@ -162,7 +168,7 @@ class StudyOptionsActivity : | |
| } | ||
| if (undoState != newUndoState) { | ||
| undoState = newUndoState | ||
| invalidateOptionsMenu() | ||
| invalidateMenu() | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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