Skip to content

Commit

Permalink
fix(previewer): cards' ord couldn't be changed
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed Apr 10, 2024
1 parent e44266c commit 400b72e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class TemplatePreviewerViewModel(
private val note: Deferred<Note>
private val templateNames: Deferred<List<String>>
private val clozeOrds: Deferred<List<Int>>?
override var currentCard: Deferred<Card>
override lateinit var currentCard: Deferred<Card>

init {
note = asyncIO {
Expand All @@ -73,17 +73,7 @@ class TemplatePreviewerViewModel(
tags = arguments.tags
}
}
currentCard = asyncIO {
val note = note.await()
withCol {
note.ephemeralCard(
col = this,
ord = ordFlow.value,
customNoteType = notetype,
fillEmpty = fillEmpty
)
}
}

if (isCloze) {
val clozeNumbers = asyncIO {
val note = note.await()
Expand Down Expand Up @@ -114,7 +104,18 @@ class TemplatePreviewerViewModel(
return
}
launchCatchingIO {
ordFlow.collectLatest {
ordFlow.collectLatest { ord ->
currentCard = asyncIO {
val note = note.await()
withCol {
note.ephemeralCard(
col = this,
ord = ord,
customNoteType = notetype,
fillEmpty = fillEmpty
)
}
}
showQuestion()
loadAndPlaySounds(CardSide.QUESTION)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.mockito.kotlin.mock
import kotlin.test.assertNotEquals

@RunWith(AndroidJUnit4::class)
class TemplatePreviewerViewModelTest : JvmTest() {
Expand All @@ -46,6 +47,17 @@ class TemplatePreviewerViewModelTest : JvmTest() {
assertThat(ordFlow.value, equalTo(3))
}

@Test
fun `card ords are changed`() {
runClozeTest(fields = mutableListOf("{{c1::one}} {{c2::bar}}")) {
onPageFinished(false)
val ord1 = currentCard.await().ord
onTabSelected(1)
val ord2 = currentCard.await().ord
assertNotEquals(ord1, ord2)
}
}

private fun runClozeTest(ord: Int = 0, fields: MutableList<String>? = null, block: suspend TemplatePreviewerViewModel.() -> Unit) = runTest {
val notetype = col.notetypes.byName("Cloze")!!
val arguments = TemplatePreviewerArguments(
Expand Down

0 comments on commit 400b72e

Please sign in to comment.