Skip to content

Commit

Permalink
refactor: make queueState not null
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed May 27, 2024
1 parent 6a0bd0e commit d74b603
Showing 1 changed file with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
CardViewerViewModel(cardMediaPlayer),
PostRequestHandler {

private var queueState: Deferred<CurrentQueueState?> = asyncIO {
withCol { sched.currentQueueState() }
private var queueState: Deferred<CurrentQueueState> = 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<Boolean>()

Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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()
}
}

Expand All @@ -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
Expand Down

0 comments on commit d74b603

Please sign in to comment.