Skip to content

Commit

Permalink
queueState
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed May 1, 2024
1 parent 01a7192 commit 533a39f
Showing 1 changed file with 20 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
PostRequestHandler,
ChangeManager.Subscriber {

private var queueState: CurrentQueueState? = null
private var queueState: Deferred<CurrentQueueState?> = asyncIO {
withCol { sched.currentQueueState() }
}
var isQueueFinishedFlow = MutableSharedFlow<Boolean>()

val againNextTime: MutableStateFlow<String?> = MutableStateFlow(null)
Expand All @@ -87,8 +89,7 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
withCol { config.get("estTimes") ?: true }
}
override var currentCard: Deferred<Card> = asyncIO {
updateQueueState()
queueState!!.topCard
queueState.await()!!.topCard
}

override val server = AnkiServer(this).also { it.start() }
Expand Down Expand Up @@ -129,6 +130,11 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
launchCatchingIO {
updateUndoRedo()
}
launchCatchingIO {
queueState.await()?.let { state ->
countsFlow.emit(state.counts to state.countsIndex)
}
}
}

/* *********************************************************************************************
Expand Down Expand Up @@ -237,10 +243,6 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
}
}

fun getTotalCount(): Int {
return queueState?.counts?.count() ?: 0
}

suspend fun getDeleteNoteDialogStrippedCardContent(): String {
val card = currentCard.await()
val cardQuestion = withCol { card.question(this, true) }
Expand Down Expand Up @@ -289,7 +291,7 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :

private fun answerCard(ease: Ease) {
launchCatchingIO {
queueState?.let {
queueState.await()?.let {
undoableOp(ReviewerOp.ANSWER_CARD) { sched.answerCard(it, ease.value) }
updateCurrentCard()
}
Expand All @@ -307,15 +309,11 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
autoAdvance.onShowQuestion()
}

private suspend fun updateQueueState() {
queueState = withCol {
sched.currentQueueState()
}
}

private suspend fun updateCurrentCard() {
updateQueueState()
val state = queueState
queueState = asyncIO {
withCol { sched.currentQueueState() }
}
val state = queueState.await()
if (state == null) {
isQueueFinishedFlow.emit(true)
} else {
Expand All @@ -334,7 +332,7 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
}

private suspend fun updateNextTimes() {
val state = queueState
val state = queueState.await()
if (!shouldShowNextTimes.await() || state == null) return

val (again, hard, good, easy) = withCol { sched.describeNextStates(state.states) }
Expand All @@ -358,7 +356,7 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
}

private suspend fun runStateMutationHook() {
val state = queueState ?: return
val state = queueState.await() ?: return
val js = state.customSchedulingJs
if (js.isEmpty()) {
statesMutated = true
Expand All @@ -370,8 +368,8 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
)
}

private fun getSchedulingStatesWithContext(): ByteArray {
val state = queueState ?: return ByteArray(0)
private suspend fun getSchedulingStatesWithContext(): ByteArray {
val state = queueState.await() ?: return ByteArray(0)
return state.schedulingStatesWithContext().toBuilder()
.mergeStates(
state.states.toBuilder().mergeCurrent(
Expand All @@ -383,8 +381,8 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
.toByteArray()
}

private fun setSchedulingStates(bytes: ByteArray): ByteArray {
val state = queueState ?: return ByteArray(0)
private suspend fun setSchedulingStates(bytes: ByteArray): ByteArray {
val state = queueState.await() ?: return ByteArray(0)
val req = SetSchedulingStatesRequest.parseFrom(bytes)
if (req.key == stateMutationKey) {
state.states = req.states
Expand Down

0 comments on commit 533a39f

Please sign in to comment.