Skip to content

Commit

Permalink
refactor: add typedAnswer param to typeAnsFilter
Browse files Browse the repository at this point in the history
separated from the next commit to ease the reviewing process
  • Loading branch information
BrayanDSO committed Dec 22, 2024
1 parent b74e8ab commit 952993c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,18 @@ abstract class CardViewerViewModel(
*************************************** Internal methods ***************************************
********************************************************************************************* */

protected abstract suspend fun typeAnsFilter(text: String): String
protected abstract suspend fun typeAnsFilter(
text: String,
typedAnswer: String? = null,
): String

private suspend fun bodyClass(): String = bodyClassForCardOrd(currentCard.await().ord)

/** From the [desktop code](https://github.com/ankitects/anki/blob/1ff55475b93ac43748d513794bcaabd5d7df6d9d/qt/aqt/reviewer.py#L358) */
private suspend fun mungeQA(text: String): String = typeAnsFilter(prepareCardTextForDisplay(text))
private suspend fun mungeQA(
text: String,
typedAnswer: String? = null,
): String = typeAnsFilter(prepareCardTextForDisplay(text), typedAnswer)

private suspend fun prepareCardTextForDisplay(text: String): String =
Sound.addPlayButtons(
Expand All @@ -132,13 +138,13 @@ abstract class CardViewerViewModel(
eval.emit("_showQuestion(${Json.encodeToString(question)}, ${Json.encodeToString(answer)}, '${bodyClass()}');")
}

protected open suspend fun showAnswer() {
protected open suspend fun showAnswer(typedAnswer: String? = null) {
Timber.v("showAnswer()")
showingAnswer.emit(true)

val card = currentCard.await()
val answerData = withCol { card.answer(this) }
val answer = mungeQA(answerData)
val answer = mungeQA(answerData, typedAnswer)

eval.emit("_showAnswer(${Json.encodeToString(answer)}, '${bodyClass()}');")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ class PreviewerViewModel(
}

/** From the [desktop code](https://github.com/ankitects/anki/blob/1ff55475b93ac43748d513794bcaabd5d7df6d9d/qt/aqt/reviewer.py#L671) */
override suspend fun typeAnsFilter(text: String): String =
override suspend fun typeAnsFilter(
text: String,
typedAnswer: String?,
): String =
if (showingAnswer.value) {
val typeAnswer = TypeAnswer.getInstance(currentCard.await(), text)
typeAnswer?.answerFilter() ?: text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ class TemplatePreviewerViewModel(
}

// https://github.com/ankitects/anki/blob/df70564079f53e587dc44f015c503fdf6a70924f/qt/aqt/clayout.py#L579
override suspend fun typeAnsFilter(text: String): String =
override suspend fun typeAnsFilter(
text: String,
typedAnswer: String?,
): String =
if (showingAnswer.value) {
val typeAnswer = TypeAnswer.getInstance(currentCard.await(), text)
if (typeAnswer?.expectedAnswer?.isEmpty() == true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,10 @@ class ReviewerViewModel(
}

// TODO
override suspend fun typeAnsFilter(text: String): String = text
override suspend fun typeAnsFilter(
text: String,
typedAnswer: String?,
): String = text

private suspend fun updateUndoAndRedoLabels() {
undoLabelFlow.emit(withCol { undoLabel() })
Expand Down

0 comments on commit 952993c

Please sign in to comment.