1515 */
1616package com.ichi2.anki.ui.windows.reviewer
1717
18+ import androidx.lifecycle.SavedStateHandle
1819import anki.collection.OpChanges
1920import anki.collection.OpChangesAfterUndo
2021import anki.frontend.SetSchedulingStatesRequest
@@ -63,6 +64,7 @@ import com.ichi2.anki.ui.windows.reviewer.autoadvance.AutoAdvance
6364import com.ichi2.anki.utils.CollectionPreferences
6465import com.ichi2.anki.utils.Destination
6566import com.ichi2.anki.utils.ext.answerCard
67+ import com.ichi2.anki.utils.ext.cardStatsNoCardClean
6668import com.ichi2.anki.utils.ext.flag
6769import com.ichi2.anki.utils.ext.getLongOrNull
6870import com.ichi2.anki.utils.ext.setUserFlagForCards
@@ -74,8 +76,9 @@ import kotlinx.coroutines.flow.MutableStateFlow
7476import org.intellij.lang.annotations.Language
7577import timber.log.Timber
7678
77- class ReviewerViewModel :
78- CardViewerViewModel (),
79+ class ReviewerViewModel (
80+ val savedStateHandle : SavedStateHandle ,
81+ ) : CardViewerViewModel(),
7982 ChangeManager .Subscriber ,
8083 BindingProcessor <ReviewerBinding , ViewerAction > {
8184 private var queueState: Deferred <CurrentQueueState ?> =
@@ -251,6 +254,18 @@ class ReviewerViewModel :
251254 destinationFlow.emit(destination)
252255 }
253256
257+ private suspend fun emitPreviousCardInfoDestination () {
258+ val previousCardId: CardId ? = savedStateHandle.getLongOrNull(KEY_PREVIOUS_CARD_ID )
259+ if (previousCardId == null ) {
260+ Timber .i(" No previous answered card found, ignoring request for 'previous card info'" )
261+ actionFeedbackFlow.emit(TR .cardStatsNoCardClean())
262+ return
263+ }
264+ val destination = CardInfoDestination (previousCardId, TR .cardStatsPreviousCard(TR .decksStudy()))
265+ Timber .i(" Launching 'previous card info' for card %d" , previousCardId)
266+ destinationFlow.emit(destination)
267+ }
268+
254269 private suspend fun emitDeckOptionsDestination () {
255270 val deckId = withCol { decks.getCurrentId() }
256271 val isFiltered = withCol { decks.isFiltered(deckId) }
@@ -464,6 +479,7 @@ class ReviewerViewModel :
464479
465480 undoableOp(handler = this ) { sched.answerCard(answer) }
466481 answerFeedbackFlow.emit(rating)
482+ savedStateHandle[KEY_PREVIOUS_CARD_ID ] = card.id
467483
468484 val wasLeech = withCol { sched.stateIsLeech(answer.newState) }
469485 if (wasLeech) {
@@ -640,6 +656,7 @@ class ReviewerViewModel :
640656 when (action) {
641657 ViewerAction .ADD_NOTE -> emitAddNoteDestination()
642658 ViewerAction .CARD_INFO -> emitCardInfoDestination()
659+ ViewerAction .PREVIOUS_CARD_INFO -> emitPreviousCardInfoDestination()
643660 ViewerAction .DECK_OPTIONS -> emitDeckOptionsDestination()
644661 ViewerAction .EDIT -> emitEditNoteDestination()
645662 ViewerAction .TAG -> editNoteTags()
@@ -740,4 +757,8 @@ class ReviewerViewModel :
740757 }
741758 }
742759 }
760+
761+ companion object {
762+ private const val KEY_PREVIOUS_CARD_ID = " key_previous_card_id"
763+ }
743764}
0 commit comments