From f17ae1f0d2c38c6dd2465151e926bc2e6c34d62f Mon Sep 17 00:00:00 2001 From: Brayan Oliveira <69634269+brayandso@users.noreply.github.com> Date: Thu, 20 Jun 2024 11:40:10 -0300 Subject: [PATCH] feat(new reviewer): flags --- .../ui/windows/reviewer/ReviewerFragment.kt | 26 +++++++++++++++++++ .../ui/windows/reviewer/ReviewerViewModel.kt | 13 ++++++++++ AnkiDroid/src/main/res/menu/reviewer2.xml | 10 +++++++ 3 files changed, 49 insertions(+) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/ReviewerFragment.kt b/AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/ReviewerFragment.kt index 8e2395b0de8c..0d9697b46d25 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/ReviewerFragment.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/ReviewerFragment.kt @@ -35,6 +35,7 @@ import com.google.android.material.appbar.MaterialToolbar import com.google.android.material.button.MaterialButton import com.ichi2.anki.AbstractFlashcardViewer.Companion.RESULT_NO_MORE_CARDS import com.ichi2.anki.CollectionManager +import com.ichi2.anki.Flag import com.ichi2.anki.NoteEditor import com.ichi2.anki.R import com.ichi2.anki.cardviewer.CardMediaPlayer @@ -123,6 +124,14 @@ class ReviewerFragment : 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) @@ -169,7 +178,24 @@ class ReviewerFragment : } } + private fun setupFlagMenu(menu: Menu) { + val submenu = menu.findItem(R.id.action_flag).subMenu + lifecycleScope.launch { + for ((flag, name) in Flag.queryDisplayNames()) { + submenu?.add(Menu.NONE, flag.id, Menu.NONE, name) + ?.setIcon(flag.drawableRes) + } + } + + viewModel.flagCodeFlow.flowWithLifecycle(lifecycle) + .collectLatestIn(lifecycleScope) { flagCode -> + menu.findItem(R.id.action_flag).setIcon(Flag.fromCode(flagCode).drawableRes) + } + } + private fun setupMenuItems(menu: Menu) { + 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) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/ReviewerViewModel.kt b/AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/ReviewerViewModel.kt index 8ca532768468..d1ba5f9c4b97 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/ReviewerViewModel.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/ReviewerViewModel.kt @@ -24,6 +24,7 @@ import anki.frontend.SetSchedulingStatesRequest import com.ichi2.anki.CollectionManager import com.ichi2.anki.CollectionManager.withCol import com.ichi2.anki.Ease +import com.ichi2.anki.Flag import com.ichi2.anki.NoteEditor import com.ichi2.anki.Reviewer import com.ichi2.anki.asyncIO @@ -66,6 +67,7 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) : } var isQueueFinishedFlow = MutableSharedFlow() val isMarkedFlow = MutableStateFlow(false) + val flagCodeFlow = MutableStateFlow(Flag.NONE.code) val actionFeedbackFlow = MutableSharedFlow() val canBuryNoteFlow = MutableStateFlow(true) val canSuspendNoteFlow = MutableStateFlow(true) @@ -139,6 +141,16 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) : } } + fun setFlag(flag: Flag) { + launchCatchingIO { + val card = currentCard.await() + undoableOp { + setUserFlagForCards(listOf(card.id), flag.code) + } + flagCodeFlow.emit(flag.code) + } + } + fun onStateMutationCallback() { statesMutated = true } @@ -357,6 +369,7 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) : showQuestion() loadAndPlaySounds(CardSide.QUESTION) updateMarkedStatus() + flagCodeFlow.emit(card.userFlag()) canBuryNoteFlow.emit(isBuryNoteAvailable(card)) canSuspendNoteFlow.emit(isSuspendNoteAvailable(card)) } diff --git a/AnkiDroid/src/main/res/menu/reviewer2.xml b/AnkiDroid/src/main/res/menu/reviewer2.xml index 22c1fb6466b5..f58f78c5a4d8 100644 --- a/AnkiDroid/src/main/res/menu/reviewer2.xml +++ b/AnkiDroid/src/main/res/menu/reviewer2.xml @@ -29,6 +29,16 @@ android:icon="@drawable/ic_redo" tools:title="Redo" app:showAsAction="never"/> + + + + +