Skip to content

Commit

Permalink
feat(new reviewer): flags
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed Jun 20, 2024
1 parent 97e72c9 commit f17ae1f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -66,6 +67,7 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
}
var isQueueFinishedFlow = MutableSharedFlow<Boolean>()
val isMarkedFlow = MutableStateFlow(false)
val flagCodeFlow = MutableStateFlow(Flag.NONE.code)
val actionFeedbackFlow = MutableSharedFlow<String>()
val canBuryNoteFlow = MutableStateFlow(true)
val canSuspendNoteFlow = MutableStateFlow(true)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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))
}
Expand Down
10 changes: 10 additions & 0 deletions AnkiDroid/src/main/res/menu/reviewer2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
android:icon="@drawable/ic_redo"
tools:title="Redo"
app:showAsAction="never"/>
<item
android:id="@+id/action_flag"
android:title="@string/menu_flag_card"
android:icon="@drawable/ic_flag_transparent"
app:showAsAction="never"
>
<menu>
<!-- flags are added dynamically -->
</menu>
</item>
<item
android:id="@+id/action_mark"
android:icon="@drawable/ic_star_border_white"
Expand Down

0 comments on commit f17ae1f

Please sign in to comment.