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 4a7372ead5c8..3561c4549a7c 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 @@ -40,12 +40,12 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) : CardViewerViewModel(cardMediaPlayer), PostRequestHandler { - private var queueState: Deferred = asyncIO { - withCol { sched.currentQueueState() } + private var queueState: Deferred = asyncIO { + // assumes that the Reviewer won't be launched if there isn't a queueState + withCol { sched.currentQueueState() }!! } override var currentCard = asyncIO { - // this assumes that the Reviewer won't be launched if there isn't a queueState - queueState.await()!!.topCard + queueState.await().topCard } var isQueueFinishedFlow = MutableSharedFlow() @@ -128,7 +128,7 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) : } private suspend fun runStateMutationHook() { - val state = queueState.await() ?: return + val state = queueState.await() val js = state.customSchedulingJs if (js.isEmpty()) { statesMutated = true @@ -141,7 +141,7 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) : } private suspend fun getSchedulingStatesWithContext(): ByteArray { - val state = queueState.await() ?: return ByteArray(0) + val state = queueState.await() return state.schedulingStatesWithContext().toBuilder() .mergeStates( state.states.toBuilder().mergeCurrent( @@ -154,7 +154,7 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) : } private suspend fun setSchedulingStates(bytes: ByteArray): ByteArray { - val state = queueState.await() ?: return ByteArray(0) + val state = queueState.await() val req = SetSchedulingStatesRequest.parseFrom(bytes) if (req.key == stateMutationKey) { state.states = req.states @@ -164,10 +164,9 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) : private fun answerCard(ease: Ease) { launchCatchingIO { - queueState.await()?.let { - undoableOp { sched.answerCard(it, ease.value) } - updateCurrentCard() - } + val state = queueState.await() + undoableOp { sched.answerCard(state, ease.value) } + updateCurrentCard() } } @@ -177,16 +176,17 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) : } private suspend fun updateCurrentCard() { - queueState = asyncIO { - withCol { - sched.currentQueueState() - } + val state = withCol { + sched.currentQueueState() } - queueState.await()?.let { - currentCard = CompletableDeferred(it.topCard) + if (state == null) { + isQueueFinishedFlow.emit(true) + } else { + queueState = CompletableDeferred(state) + currentCard = CompletableDeferred(state.topCard) showQuestion() loadAndPlaySounds(CardSide.QUESTION) - } ?: isQueueFinishedFlow.emit(true) + } } // TODO