Skip to content

Commit

Permalink
ações linkadas
Browse files Browse the repository at this point in the history
TODO: separar esse comit da nullidade do menu.findItem
  • Loading branch information
BrayanDSO committed Dec 1, 2024
1 parent a9dc970 commit 13f0097
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.ichi2.anki.preferences

import android.content.Context
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
Expand All @@ -30,7 +31,6 @@ import com.ichi2.anki.preferences.reviewer.MenuDisplayType
import com.ichi2.anki.preferences.reviewer.ToolbarItem
import com.ichi2.anki.preferences.reviewer.ToolbarItemsAdapter
import com.ichi2.anki.preferences.reviewer.ToolbarItemsTouchHelperCallback
import com.ichi2.anki.showThemedToast
import com.ichi2.anki.utils.ext.sharedPrefs

class ReviewerToolbarButtonsFragment : Fragment(R.layout.preferences_reviewer_toolbar_buttons) {
Expand Down Expand Up @@ -70,8 +70,8 @@ class ReviewerToolbarButtonsFragment : Fragment(R.layout.preferences_reviewer_to
MenuDisplayType.DISABLED.setPreferenceValue(prefs, disabledItems2)

menu.clear()
addItemsToMenu(menu, alwaysShowItems, MenuItem.SHOW_AS_ACTION_ALWAYS)
addItemsToMenu(menu, onMenuItems, MenuItem.SHOW_AS_ACTION_NEVER)
addItemsToMenu(menu, alwaysShowItems, MenuItem.SHOW_AS_ACTION_ALWAYS, requireContext())
addItemsToMenu(menu, onMenuItems, MenuItem.SHOW_AS_ACTION_NEVER, requireContext())
}
}
val itemTouchHelper = ItemTouchHelper(callback)
Expand All @@ -86,24 +86,37 @@ class ReviewerToolbarButtonsFragment : Fragment(R.layout.preferences_reviewer_to
itemTouchHelper.attachToRecyclerView(this)
}

addItemsToMenu(menu, alwaysItems, MenuItem.SHOW_AS_ACTION_ALWAYS)
addItemsToMenu(menu, menuOnlyItems, MenuItem.SHOW_AS_ACTION_NEVER)
addItemsToMenu(menu, alwaysItems, MenuItem.SHOW_AS_ACTION_ALWAYS, requireContext())
addItemsToMenu(menu, menuOnlyItems, MenuItem.SHOW_AS_ACTION_NEVER, requireContext())

super.onViewCreated(view, savedInstanceState)
}

private fun addItemsToMenu(menu: Menu, items: List<ToolbarItem.Action>, menuActionType: Int) {
for (item in items) {
val action = item.action
menu.add(0, action.id, Menu.NONE, action.title).apply {
icon = ContextCompat.getDrawable(requireContext(), action.drawable)
setShowAsAction(menuActionType)
setOnMenuItemClickListener {
val title = getString(action.title)
showThemedToast(requireContext(), title, true)
true
// TODO mostrar ícones no menu
companion object {
private fun addItemsToMenu(
menu: Menu,
items: List<ToolbarItem.Action>,
menuActionType: Int,
context: Context
) {
for (item in items) {
val action = item.action
menu.add(0, action.id, Menu.NONE, action.title).apply {
icon = ContextCompat.getDrawable(context, action.drawable)
setShowAsAction(menuActionType)
}
}
}

fun setConfiguredMenu(menu: Menu, context: Context) {
val preferences = context.sharedPrefs()

val alwaysItems = MenuDisplayType.ALWAYS.getToolbarActions(preferences)
val menuOnlyItems = MenuDisplayType.MENU_ONLY.getToolbarActions(preferences)

addItemsToMenu(menu, alwaysItems, MenuItem.SHOW_AS_ACTION_ALWAYS, context)
addItemsToMenu(menu, menuOnlyItems, MenuItem.SHOW_AS_ACTION_NEVER, context)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ enum class ReviewerAction(
) {
// Always
FLAG(R.id.flag_menu, R.string.menu_flag_card, R.drawable.ic_flag_transparent, MenuDisplayType.ALWAYS),
UNDO(R.id.reviewer_undo, R.string.undo, R.drawable.ic_undo_white, MenuDisplayType.ALWAYS),
UNDO(R.id.action_undo, R.string.undo, R.drawable.ic_undo_white, MenuDisplayType.ALWAYS),

// Menu only
MARK(R.id.reviewer_mark, R.string.menu_mark_note, R.drawable.ic_star, MenuDisplayType.MENU_ONLY),
REDO(R.id.reviewer_redo, R.string.redo, R.drawable.ic_redo, MenuDisplayType.MENU_ONLY),
DELETE(R.id.reviewer_delete, R.string.menu_delete_note, R.drawable.ic_delete, MenuDisplayType.MENU_ONLY),
EDIT_NOTE(R.id.reviewer_edit_note, R.string.cardeditor_title_edit_card, R.drawable.ic_mode_edit_white, MenuDisplayType.MENU_ONLY),
DECK_OPTIONS(R.id.reviewer_deck_options, R.string.menu__deck_options, R.drawable.ic_tune_white, MenuDisplayType.MENU_ONLY),
MARK(R.id.action_mark, R.string.menu_mark_note, R.drawable.ic_star, MenuDisplayType.MENU_ONLY),
REDO(R.id.action_redo, R.string.redo, R.drawable.ic_redo, MenuDisplayType.MENU_ONLY),
DELETE(R.id.action_delete, R.string.menu_delete_note, R.drawable.ic_delete, MenuDisplayType.MENU_ONLY),
EDIT_NOTE(R.id.action_edit_note, R.string.cardeditor_title_edit_card, R.drawable.ic_mode_edit_white, MenuDisplayType.MENU_ONLY),
DECK_OPTIONS(R.id.action_deck_options, R.string.menu__deck_options, R.drawable.ic_tune_white, MenuDisplayType.MENU_ONLY),

// Disabled
CARD_INFO(R.id.reviewer_card_info, R.string.card_info_title, R.drawable.ic_dialog_info, MenuDisplayType.DISABLED),
ADD_NOTE(R.id.reviewer_add_note, R.string.menu_add_note, R.drawable.ic_add, MenuDisplayType.DISABLED),
CARD_INFO(R.id.action_card_info, R.string.card_info_title, R.drawable.ic_dialog_info, MenuDisplayType.DISABLED),
ADD_NOTE(R.id.action_add_note, R.string.menu_add_note, R.drawable.ic_add, MenuDisplayType.DISABLED),
USER_ACTION_1(R.id.user_action_1, R.string.user_action_1, R.drawable.user_action_1, MenuDisplayType.DISABLED),
USER_ACTION_2(R.id.user_action_2, R.string.user_action_2, R.drawable.user_action_2, MenuDisplayType.DISABLED),
USER_ACTION_3(R.id.user_action_3, R.string.user_action_3, R.drawable.user_action_3, MenuDisplayType.DISABLED),
Expand All @@ -48,6 +48,11 @@ enum class ReviewerAction(
USER_ACTION_6(R.id.user_action_6, R.string.user_action_6, R.drawable.user_action_6, MenuDisplayType.DISABLED),
USER_ACTION_7(R.id.user_action_7, R.string.user_action_7, R.drawable.user_action_7, MenuDisplayType.DISABLED),
USER_ACTION_8(R.id.user_action_8, R.string.user_action_8, R.drawable.user_action_8, MenuDisplayType.DISABLED),
USER_ACTION_9(R.id.user_action_9, R.string.user_action_9, R.drawable.user_action_9, MenuDisplayType.DISABLED)
;
USER_ACTION_9(R.id.user_action_9, R.string.user_action_9, R.drawable.user_action_9, MenuDisplayType.DISABLED);

companion object {
fun fromId(@IdRes id: Int): ReviewerAction {
return entries.first { it.id == id }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ import com.ichi2.anki.NoteEditor
import com.ichi2.anki.R
import com.ichi2.anki.cardviewer.CardMediaPlayer
import com.ichi2.anki.noteeditor.NoteEditorLauncher
import com.ichi2.anki.preferences.ReviewerToolbarButtonsFragment
import com.ichi2.anki.preferences.reviewer.ReviewerAction
import com.ichi2.anki.preferences.reviewer.ReviewerAction.*
import com.ichi2.anki.previewer.CardViewerActivity
import com.ichi2.anki.previewer.CardViewerFragment
import com.ichi2.anki.snackbar.BaseSnackbarBuilderProvider
Expand Down Expand Up @@ -123,36 +126,40 @@ class ReviewerFragment :

// TODO
override fun onMenuItemClick(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_add_note -> launchAddNote()
R.id.action_bury_card -> viewModel.buryCard()
R.id.action_bury_note -> viewModel.buryNote()
R.id.action_card_info -> launchCardInfo()
R.id.action_delete -> viewModel.deleteNote()
R.id.action_edit -> launchEditNote()
R.id.action_mark -> viewModel.toggleMark()
R.id.action_open_deck_options -> launchDeckOptions()
R.id.action_redo -> viewModel.redo()
R.id.action_suspend_card -> viewModel.suspendCard()
R.id.action_suspend_note -> viewModel.suspendNote()
R.id.action_undo -> viewModel.undo()
R.id.flag_none -> viewModel.setFlag(Flag.NONE)
R.id.flag_red -> viewModel.setFlag(Flag.RED)
R.id.flag_orange -> viewModel.setFlag(Flag.ORANGE)
R.id.flag_green -> viewModel.setFlag(Flag.GREEN)
R.id.flag_blue -> viewModel.setFlag(Flag.BLUE)
R.id.flag_pink -> viewModel.setFlag(Flag.PINK)
R.id.flag_turquoise -> viewModel.setFlag(Flag.TURQUOISE)
R.id.flag_purple -> viewModel.setFlag(Flag.PURPLE)
R.id.user_action_1 -> viewModel.userAction(1)
R.id.user_action_2 -> viewModel.userAction(2)
R.id.user_action_3 -> viewModel.userAction(3)
R.id.user_action_4 -> viewModel.userAction(4)
R.id.user_action_5 -> viewModel.userAction(5)
R.id.user_action_6 -> viewModel.userAction(6)
R.id.user_action_7 -> viewModel.userAction(7)
R.id.user_action_8 -> viewModel.userAction(8)
R.id.user_action_9 -> viewModel.userAction(9)
val action = ReviewerAction.fromId(item.itemId)
when (action) {
ADD_NOTE -> launchAddNote()
// R.id.action_bury_card -> viewModel.buryCard()
// R.id.action_bury_note -> viewModel.buryNote()
CARD_INFO -> launchCardInfo()
DELETE -> viewModel.deleteNote()
EDIT_NOTE -> launchEditNote()
MARK -> viewModel.toggleMark()
DECK_OPTIONS -> launchDeckOptions()
REDO -> viewModel.redo()
// R.id.action_suspend_card -> viewModel.suspendCard()
// R.id.action_suspend_note -> viewModel.suspendNote()
UNDO -> viewModel.undo()
// R.id.flag_none -> viewModel.setFlag(Flag.NONE)
// R.id.flag_red -> viewModel.setFlag(Flag.RED)
// R.id.flag_orange -> viewModel.setFlag(Flag.ORANGE)
// R.id.flag_green -> viewModel.setFlag(Flag.GREEN)
// R.id.flag_blue -> viewModel.setFlag(Flag.BLUE)
// R.id.flag_pink -> viewModel.setFlag(Flag.PINK)
// R.id.flag_turquoise -> viewModel.setFlag(Flag.TURQUOISE)
// R.id.flag_purple -> viewModel.setFlag(Flag.PURPLE)
USER_ACTION_1 -> viewModel.userAction(1)
USER_ACTION_2 -> viewModel.userAction(2)
USER_ACTION_3 -> viewModel.userAction(3)
USER_ACTION_4 -> viewModel.userAction(4)
USER_ACTION_5 -> viewModel.userAction(5)
USER_ACTION_6 -> viewModel.userAction(6)
USER_ACTION_7 -> viewModel.userAction(7)
USER_ACTION_8 -> viewModel.userAction(8)
USER_ACTION_9 -> viewModel.userAction(9)
FLAG -> {
// TODO
}
}
return true
}
Expand Down Expand Up @@ -252,18 +259,19 @@ class ReviewerFragment :
}

private fun setupMenuItems(menu: Menu) {
setupFlagMenu(menu)
ReviewerToolbarButtonsFragment.setConfiguredMenu(menu, requireContext())
// TODO setupFlagMenu(menu)

// TODO show that the card is marked somehow when the menu item is overflowed or not shown
val markItem = menu.findItem(R.id.action_mark)
viewModel.isMarkedFlow.flowWithLifecycle(lifecycle)
.collectLatestIn(lifecycleScope) { isMarked ->
if (isMarked) {
markItem.setIcon(R.drawable.ic_star)
markItem.setTitle(R.string.menu_unmark_note)
markItem?.setIcon(R.drawable.ic_star)
markItem?.setTitle(R.string.menu_unmark_note)
} else {
markItem.setIcon(R.drawable.ic_star_border_white)
markItem.setTitle(R.string.menu_mark_note)
markItem?.setIcon(R.drawable.ic_star_border_white)
markItem?.setTitle(R.string.menu_mark_note)
}
}

Expand All @@ -272,11 +280,11 @@ class ReviewerFragment :
viewModel.canBuryNoteFlow.flowWithLifecycle(lifecycle)
.collectLatestIn(lifecycleScope) { canBuryNote ->
if (canBuryNote) {
buryItem.isVisible = true
buryCardItem.isVisible = false
buryItem?.isVisible = true
buryCardItem?.isVisible = false
} else {
buryItem.isVisible = false
buryCardItem.isVisible = true
buryItem?.isVisible = false
buryCardItem?.isVisible = true
}
}

Expand All @@ -285,26 +293,26 @@ class ReviewerFragment :
viewModel.canSuspendNoteFlow.flowWithLifecycle(lifecycle)
.collectLatestIn(lifecycleScope) { canSuspendNote ->
if (canSuspendNote) {
suspendItem.isVisible = true
suspendCardItem.isVisible = false
suspendItem?.isVisible = true
suspendCardItem?.isVisible = false
} else {
suspendItem.isVisible = false
suspendItem.isVisible = true
suspendItem?.isVisible = false
suspendItem?.isVisible = true
}
}

val undoItem = menu.findItem(R.id.action_undo)
viewModel.undoLabelFlow.flowWithLifecycle(lifecycle)
.collectLatestIn(lifecycleScope) { label ->
undoItem.title = label ?: CollectionManager.TR.undoUndo()
undoItem.isEnabled = label != null
undoItem?.title = label ?: CollectionManager.TR.undoUndo()
undoItem?.isEnabled = label != null
}

val redoItem = menu.findItem(R.id.action_redo)
viewModel.redoLabelFlow.flowWithLifecycle(lifecycle)
.collectLatestIn(lifecycleScope) { label ->
redoItem.title = label ?: CollectionManager.TR.undoRedo()
redoItem.isEnabled = label != null
redoItem?.title = label ?: CollectionManager.TR.undoRedo()
redoItem?.isEnabled = label != null
}
}

Expand Down
1 change: 0 additions & 1 deletion AnkiDroid/src/main/res/layout/reviewer2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
app:layout_constraintTop_toTopOf="parent"
android:layout_height="wrap_content"
android:background="?attr/alternativeBackgroundColor"
app:menu="@menu/reviewer2"
>

<com.google.android.material.textview.MaterialTextView
Expand Down
18 changes: 9 additions & 9 deletions AnkiDroid/src/main/res/menu/reviewer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,47 +161,47 @@
android:icon="@drawable/ic_tune_white"
android:title="@string/menu__deck_options" />
<item
android:id="@+id/user_action_1"
android:id="@id/user_action_1"
android:icon="@drawable/user_action_1"
android:title="@string/user_action_1"
/>
<item
android:id="@+id/user_action_2"
android:id="@id/user_action_2"
android:icon="@drawable/user_action_2"
android:title="@string/user_action_2"
/>
<item
android:id="@+id/user_action_3"
android:id="@id/user_action_3"
android:icon="@drawable/user_action_3"
android:title="@string/user_action_3"
/>
<item
android:id="@+id/user_action_4"
android:id="@id/user_action_4"
android:icon="@drawable/user_action_4"
android:title="@string/user_action_4"
/>
<item
android:id="@+id/user_action_5"
android:id="@id/user_action_5"
android:icon="@drawable/user_action_5"
android:title="@string/user_action_5"
/>
<item
android:id="@+id/user_action_6"
android:id="@id/user_action_6"
android:icon="@drawable/user_action_6"
android:title="@string/user_action_6"
/>
<item
android:id="@+id/user_action_7"
android:id="@id/user_action_7"
android:icon="@drawable/user_action_7"
android:title="@string/user_action_7"
/>
<item
android:id="@+id/user_action_8"
android:id="@id/user_action_8"
android:icon="@drawable/user_action_8"
android:title="@string/user_action_8"
/>
<item
android:id="@+id/user_action_9"
android:id="@id/user_action_9"
android:icon="@drawable/user_action_9"
android:title="@string/user_action_9"
/>
Expand Down
26 changes: 18 additions & 8 deletions AnkiDroid/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,22 @@
<item type="id" name="flag_menu"/>


<item type="id" name="reviewer_add_note"/>
<item type="id" name="reviewer_undo"/>
<item type="id" name="reviewer_redo"/>
<item type="id" name="reviewer_mark"/>
<item type="id" name="reviewer_delete"/>
<item type="id" name="reviewer_deck_options"/>
<item type="id" name="reviewer_edit_note"/>
<item type="id" name="reviewer_card_info"/>
<item type="id" name="action_add_note"/>
<item type="id" name="action_undo"/>
<item type="id" name="action_redo"/>
<item type="id" name="action_mark"/>
<item type="id" name="action_delete"/>
<item type="id" name="action_deck_options"/>
<item type="id" name="action_edit_note"/>
<item type="id" name="action_card_info"/>

<item type="id" name="user_action_1"/>
<item type="id" name="user_action_2"/>
<item type="id" name="user_action_3"/>
<item type="id" name="user_action_4"/>
<item type="id" name="user_action_5"/>
<item type="id" name="user_action_6"/>
<item type="id" name="user_action_7"/>
<item type="id" name="user_action_8"/>
<item type="id" name="user_action_9"/>
</resources>

0 comments on commit 13f0097

Please sign in to comment.