Skip to content

Commit

Permalink
new reviewer
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed Dec 25, 2024
1 parent 76a659b commit b01b109
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 19 deletions.
6 changes: 3 additions & 3 deletions AnkiDroid/src/main/java/com/ichi2/anki/Reviewer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1666,11 +1666,11 @@ open class Reviewer :
const val ACTION_SNACKBAR_TIME = 500
}

override fun executeAction(
override fun processAction(
action: ViewerCommand,
forBinding: ReviewerBinding,
binding: ReviewerBinding,
): Boolean {
if (CardSide.fromAnswer(isDisplayingAnswer) != forBinding.side) return false
if (binding.side != CardSide.BOTH && CardSide.fromAnswer(isDisplayingAnswer) != binding.side) return false
return executeCommand(action, null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@
*/
package com.ichi2.anki.preferences.reviewer

import android.content.SharedPreferences
import android.view.KeyEvent
import androidx.annotation.DrawableRes
import androidx.annotation.IdRes
import androidx.annotation.StringRes
import com.ichi2.anki.Flag
import com.ichi2.anki.R
import com.ichi2.anki.cardviewer.ScreenAction
import com.ichi2.anki.preferences.reviewer.MenuDisplayType.ALWAYS
import com.ichi2.anki.preferences.reviewer.MenuDisplayType.DISABLED
import com.ichi2.anki.preferences.reviewer.MenuDisplayType.MENU_ONLY
import com.ichi2.anki.reviewer.Binding
import com.ichi2.anki.reviewer.Binding.ModifierKeys
import com.ichi2.anki.reviewer.Binding.ModifierKeys.Companion.ctrl
import com.ichi2.anki.reviewer.CardSide
import com.ichi2.anki.reviewer.ReviewerBinding

/**
* @param menuId menu Id of the action
Expand All @@ -34,18 +42,18 @@ import com.ichi2.anki.preferences.reviewer.MenuDisplayType.MENU_ONLY
enum class ViewerAction(
@IdRes val menuId: Int,
@DrawableRes val drawableRes: Int?,
@StringRes val titleRes: Int = R.string.empty_string,
@StringRes override val titleRes: Int = R.string.empty_string,
val defaultDisplayType: MenuDisplayType? = null,
val parentMenu: ViewerAction? = null,
) {
) : ScreenAction<ReviewerBinding> {
// Always
UNDO(R.id.action_undo, R.drawable.ic_undo_white, R.string.undo, ALWAYS),

// Menu only
REDO(R.id.action_redo, R.drawable.ic_redo, R.string.redo, MENU_ONLY),
FLAG_MENU(R.id.action_flag, R.drawable.ic_flag_transparent, R.string.menu_flag, MENU_ONLY),
MARK(R.id.action_mark, R.drawable.ic_star, R.string.menu_mark_note, MENU_ONLY),
EDIT_NOTE(R.id.action_edit_note, R.drawable.ic_mode_edit_white, R.string.cardeditor_title_edit_card, MENU_ONLY),
EDIT(R.id.action_edit_note, R.drawable.ic_mode_edit_white, R.string.cardeditor_title_edit_card, MENU_ONLY),
BURY_MENU(R.id.action_bury, R.drawable.ic_flip_to_back_white, R.string.menu_bury, MENU_ONLY),
SUSPEND_MENU(R.id.action_suspend, R.drawable.ic_suspend, R.string.menu_suspend, MENU_ONLY),
DELETE(R.id.action_delete, R.drawable.ic_delete_white, R.string.menu_delete_note, MENU_ONLY),
Expand Down Expand Up @@ -79,8 +87,73 @@ enum class ViewerAction(
FLAG_PURPLE(Flag.PURPLE.id, Flag.PURPLE.drawableRes, parentMenu = FLAG_MENU),
;

override val preferenceKey: String get() = "binding_$name"

override fun getBindings(prefs: SharedPreferences): List<ReviewerBinding> {
val prefValue = prefs.getString(preferenceKey, null) ?: return defaultBindings
return ReviewerBinding.fromPreferenceString(prefValue)
}

private val defaultBindings: List<ReviewerBinding> get() =
when (this) {
UNDO -> listOf(keycode(KeyEvent.KEYCODE_Z, ctrl()))
REDO -> listOf(keycode(KeyEvent.KEYCODE_Z, ModifierKeys(shift = true, ctrl = true, alt = false)))
MARK -> listOf(unicode('*'))
EDIT -> listOf(keycode(KeyEvent.KEYCODE_E))
ADD_NOTE -> listOf(keycode(KeyEvent.KEYCODE_A))
BURY_NOTE -> listOf(unicode('='))
BURY_CARD -> listOf(unicode('-'))
SUSPEND_NOTE -> listOf(unicode('!'))
SUSPEND_CARD -> listOf(unicode('@'))
// No default gestures
DELETE,
CARD_INFO,
USER_ACTION_1,
USER_ACTION_2,
USER_ACTION_3,
USER_ACTION_4,
USER_ACTION_5,
USER_ACTION_6,
USER_ACTION_7,
USER_ACTION_8,
USER_ACTION_9,
// Menu flag actions. They set the flag, but don't toggle it
UNSET_FLAG,
FLAG_RED,
FLAG_ORANGE,
FLAG_BLUE,
FLAG_GREEN,
FLAG_PINK,
FLAG_TURQUOISE,
FLAG_PURPLE,
// Menu only
DECK_OPTIONS,
BURY_MENU,
SUSPEND_MENU,
FLAG_MENU,
-> emptyList()
}

fun isSubMenu() = ViewerAction.entries.any { it.parentMenu == this }

private fun keycode(
keycode: Int,
keys: ModifierKeys = ModifierKeys.none(),
side: CardSide = CardSide.BOTH,
): ReviewerBinding {
val binding = Binding.keyCode(keycode, keys)
return ReviewerBinding(binding = binding, side = side)
}

private fun unicode(
unicodeChar: Char,
keys: ModifierKeys = ModifierKeys.none(),
side: CardSide = CardSide.BOTH,
): ReviewerBinding {
val binding = Binding.unicode(unicodeChar, keys)
return ReviewerBinding(binding = binding, side = side)
}

companion object {
fun fromId(
@IdRes id: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ class PreviewerFragment :
return true
}

override fun executeAction(
override fun processAction(
action: PreviewerAction,
forBinding: PreviewerBinding,
binding: PreviewerBinding,
): Boolean {
when (action) {
PreviewerAction.MARK -> viewModel.toggleMark()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ import timber.log.Timber
import java.util.Objects

fun interface BindingProcessor<B : MappableBinding, A : ScreenAction<B>> {
fun executeAction(
fun processAction(
action: A,
forBinding: B,
binding: B,
): Boolean
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ class ScreenKeyMap<M : MappableBinding, A : ScreenAction<M>>(
val bindings = Binding.possibleKeyBindings(event)
for (binding in bindings) {
val (mappableBinding, action) = bindingMap[binding] ?: continue
ret = ret or processor.executeAction(action, mappableBinding)
ret = ret or processor.processAction(action, mappableBinding)
}

return ret
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.content.Intent
import android.os.Bundle
import android.text.SpannableString
import android.text.style.UnderlineSpan
import android.view.KeyEvent
import android.view.Menu
import android.view.MenuItem
import android.view.View
Expand All @@ -43,6 +44,7 @@ import com.google.android.material.button.MaterialButton
import com.google.android.material.textview.MaterialTextView
import com.ichi2.anki.AbstractFlashcardViewer.Companion.RESULT_NO_MORE_CARDS
import com.ichi2.anki.CollectionManager
import com.ichi2.anki.DispatchKeyEventListener
import com.ichi2.anki.Flag
import com.ichi2.anki.NoteEditor
import com.ichi2.anki.R
Expand All @@ -57,7 +59,7 @@ import com.ichi2.anki.preferences.reviewer.ViewerAction.BURY_NOTE
import com.ichi2.anki.preferences.reviewer.ViewerAction.CARD_INFO
import com.ichi2.anki.preferences.reviewer.ViewerAction.DECK_OPTIONS
import com.ichi2.anki.preferences.reviewer.ViewerAction.DELETE
import com.ichi2.anki.preferences.reviewer.ViewerAction.EDIT_NOTE
import com.ichi2.anki.preferences.reviewer.ViewerAction.EDIT
import com.ichi2.anki.preferences.reviewer.ViewerAction.FLAG_BLUE
import com.ichi2.anki.preferences.reviewer.ViewerAction.FLAG_GREEN
import com.ichi2.anki.preferences.reviewer.ViewerAction.FLAG_MENU
Expand All @@ -84,6 +86,10 @@ import com.ichi2.anki.preferences.reviewer.ViewerAction.USER_ACTION_8
import com.ichi2.anki.preferences.reviewer.ViewerAction.USER_ACTION_9
import com.ichi2.anki.previewer.CardViewerActivity
import com.ichi2.anki.previewer.CardViewerFragment
import com.ichi2.anki.reviewer.BindingProcessor
import com.ichi2.anki.reviewer.CardSide
import com.ichi2.anki.reviewer.ReviewerBinding
import com.ichi2.anki.reviewer.ScreenKeyMap
import com.ichi2.anki.snackbar.BaseSnackbarBuilderProvider
import com.ichi2.anki.snackbar.SnackbarBuilder
import com.ichi2.anki.snackbar.showSnackbar
Expand All @@ -98,7 +104,9 @@ import kotlinx.coroutines.launch
class ReviewerFragment :
CardViewerFragment(R.layout.reviewer2),
BaseSnackbarBuilderProvider,
ActionMenuView.OnMenuItemClickListener {
ActionMenuView.OnMenuItemClickListener,
BindingProcessor<ReviewerBinding, ViewerAction>,
DispatchKeyEventListener {
override val viewModel: ReviewerViewModel by viewModels {
ReviewerViewModel.factory(CardMediaPlayer())
}
Expand All @@ -110,6 +118,8 @@ class ReviewerFragment :
anchorView = this@ReviewerFragment.view?.findViewById(R.id.buttons_area)
}

private lateinit var keyMap: ScreenKeyMap<ReviewerBinding, ViewerAction>

override fun onStop() {
super.onStop()
if (!requireActivity().isChangingConfigurations) {
Expand All @@ -122,6 +132,7 @@ class ReviewerFragment :
savedInstanceState: Bundle?,
) {
super.onViewCreated(view, savedInstanceState)
keyMap = ScreenKeyMap(sharedPrefs(), ViewerAction.entries, this)

view.findViewById<MaterialToolbar>(R.id.toolbar).apply {
setNavigationOnClickListener { requireActivity().onBackPressedDispatcher.onBackPressed() }
Expand Down Expand Up @@ -154,15 +165,12 @@ class ReviewerFragment :
}
}

// TODO
override fun onMenuItemClick(item: MenuItem): Boolean {
if (item.hasSubMenu()) return false
val action = ViewerAction.fromId(item.itemId)
private fun executeAction(action: ViewerAction): Boolean {
when (action) {
ADD_NOTE -> launchAddNote()
CARD_INFO -> launchCardInfo()
DECK_OPTIONS -> launchDeckOptions()
EDIT_NOTE -> launchEditNote()
EDIT -> launchEditNote()
DELETE -> viewModel.deleteNote()
MARK -> viewModel.toggleMark()
REDO -> viewModel.redo()
Expand Down Expand Up @@ -195,6 +203,25 @@ class ReviewerFragment :
return true
}

override fun dispatchKeyEvent(event: KeyEvent): Boolean {
if (event.action != KeyEvent.ACTION_DOWN) return false
return keyMap.onKeyDown(event)
}

override fun processAction(
action: ViewerAction,
binding: ReviewerBinding,
): Boolean {
if (binding.side != CardSide.BOTH && CardSide.fromAnswer(viewModel.showingAnswer.value) != binding.side) return false
return executeAction(action)
}

override fun onMenuItemClick(item: MenuItem): Boolean {
if (item.hasSubMenu()) return false
val action = ViewerAction.fromId(item.itemId)
return executeAction(action)
}

private fun setupAnswerButtons(view: View) {
val hideAnswerButtons = sharedPrefs().getBoolean(getString(R.string.hide_answer_buttons_key), false)
if (hideAnswerButtons) {
Expand Down

0 comments on commit b01b109

Please sign in to comment.