Skip to content

Commit

Permalink
fix: incorrect ord if a cloze is missing
Browse files Browse the repository at this point in the history
e.g.:

- {{c1::foo}} la {{c3::bar}}
- no clozes
- {{c23::1st cloze doesnt exist}}
  • Loading branch information
BrayanDSO committed Mar 8, 2024
1 parent 2385c66 commit 288e53c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
24 changes: 20 additions & 4 deletions AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags
if (event.isCtrlPressed) {
Timber.i("Ctrl+P: Preview Pressed")
if (allowSaveAndPreview()) {
performPreview()
launchCatchingTask { performPreview() }
}
}
}
Expand Down Expand Up @@ -1101,7 +1101,7 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags
R.id.action_preview -> {
Timber.i("NoteEditor:: Preview button pressed")
if (allowSaveAndPreview()) {
performPreview()
launchCatchingTask { performPreview() }
}
return true
}
Expand Down Expand Up @@ -1213,17 +1213,33 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags
// ----------------------------------------------------------------------------
@VisibleForTesting
@NeedsTest("previewing newlines")
fun performPreview() {
@NeedsTest("cards with a cloze notetype but no cloze in fields are previewed as empty card")
@NeedsTest("clozes that don't start at '1' are correctly displayed")
suspend fun performPreview() {
val convertNewlines = shouldReplaceNewlines()
fun String?.toFieldText(): String = NoteService.convertToHtmlNewline(this.toString(), convertNewlines)
val fields = editFields?.mapTo(mutableListOf()) { it!!.fieldText.toFieldText() } ?: mutableListOf()
val tags = selectedTags ?: mutableListOf()

val ord = if (editorNote!!.notetype.isCloze) {
val tempNote = withCol { Note.fromNotetypeId(editorNote!!.notetype.id) }
tempNote.fields = fields // makes possible to get the cloze numbers from the fields
val clozeNumbers = withCol { clozeNumbersInNote(tempNote) }
if (clozeNumbers.isNotEmpty()) {
clozeNumbers.first() - 1
} else {
0
}
} else {
currentEditedCard?.ord ?: 0
}

val args = TemplatePreviewerArguments(
notetypeFile = NotetypeFile(this, editorNote!!.notetype),
fields = fields,
tags = tags,
id = editorNote!!.id,
ord = currentEditedCard?.ord ?: 0,
ord = ord,
fillEmpty = false
)
val intent = TemplatePreviewerFragment.getIntent(this, args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.ichi2.anki.NotetypeFile
import com.ichi2.anki.launchCatchingIO
import com.ichi2.anki.reviewer.CardSide
import com.ichi2.anki.utils.ext.ifNullOrEmpty
import com.ichi2.annotations.NeedsTest
import com.ichi2.libanki.Note
import com.ichi2.libanki.NotetypeJson
import kotlinx.coroutines.Job
Expand All @@ -36,6 +37,7 @@ import org.intellij.lang.annotations.Language
class TemplatePreviewerViewModel(arguments: TemplatePreviewerArguments) : CardViewerViewModel() {
private val notetype = arguments.notetype
private val fillEmpty = arguments.fillEmpty
private val isCloze = notetype.isCloze

/**
* identifies which of the card templates or cloze deletions it corresponds to
Expand All @@ -46,6 +48,7 @@ class TemplatePreviewerViewModel(arguments: TemplatePreviewerArguments) : CardVi

private lateinit var note: Note
private lateinit var templateNames: List<String>
private var clozeNumbers: List<Int>? = null
private var initJob: Job? = null

init {
Expand All @@ -61,11 +64,12 @@ class TemplatePreviewerViewModel(arguments: TemplatePreviewerArguments) : CardVi
tags = arguments.tags
}

templateNames = if (notetype.isCloze) {
if (isCloze) {
clozeNumbers = withCol { clozeNumbersInNote(note) }
val tr = CollectionManager.TR
withCol { clozeNumbersInNote(note) }.map { tr.cardTemplatesCard(it) }
templateNames = clozeNumbers!!.map { tr.cardTemplatesCard(it) }
} else {
notetype.templatesNames
templateNames = notetype.templatesNames
}
}.also {
it.invokeOnCompletion {
Expand Down Expand Up @@ -119,11 +123,26 @@ class TemplatePreviewerViewModel(arguments: TemplatePreviewerArguments) : CardVi
return templateNames
}

fun onTabSelected(ord: Int) {
launchCatchingIO { ordFlow.emit(ord) }
@NeedsTest("the correct cloze ord is shown for the tab")
fun onTabSelected(position: Int) {
launchCatchingIO {
val ord = if (isCloze) {
clozeNumbers!![position] - 1
} else {
position
}
ordFlow.emit(ord)
}
}

fun getCurrentTabIndex(): Int = ordFlow.value
@NeedsTest("tab is selected if the first cloze isn't '1'")
fun getCurrentTabIndex(): Int {
return if (isCloze) {
clozeNumbers!!.indexOf(ordFlow.value) + 1
} else {
ordFlow.value
}
}

/* *********************************************************************************************
*************************************** Internal methods ***************************************
Expand Down
3 changes: 2 additions & 1 deletion AnkiDroid/src/test/java/com/ichi2/anki/NoteEditorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import com.ichi2.libanki.Decks.Companion.CURRENT_DECK
import com.ichi2.libanki.Note
import com.ichi2.libanki.NotetypeJson
import com.ichi2.testutils.AnkiAssert.assertDoesNotThrow
import kotlinx.coroutines.runBlocking
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.*
import org.junit.Ignore
Expand Down Expand Up @@ -234,7 +235,7 @@ class NoteEditorTest : RobolectricTest() {
fun previewWorksWithNoError() {
// #6923 regression test - Low value - Could not make this fail as onSaveInstanceState did not crash under Robolectric.
val editor = getNoteEditorAddingNote(DECK_LIST, NoteEditor::class.java)
assertDoesNotThrow { editor.performPreview() }
assertDoesNotThrow { runBlocking { editor.performPreview() } }
}

@Test
Expand Down

0 comments on commit 288e53c

Please sign in to comment.