Skip to content

[GSoC'24] Fix: cloze number incorrect on undo #16779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class InstantEditorViewModel : ViewModel(), OnErrorListener {
// List to store the cloze integers
private val intClozeList = mutableListOf<Int>()

/** The number of words which are marked as cloze deletions */
@VisibleForTesting
val clozeDeletionCount get() = intClozeList.size

private val _currentClozeMode = MutableStateFlow(InstantNoteEditorActivity.ClozeMode.INCREMENT)

val currentClozeMode: StateFlow<InstantNoteEditorActivity.ClozeMode> = _currentClozeMode.asStateFlow()
Expand Down Expand Up @@ -178,14 +182,14 @@ class InstantEditorViewModel : ViewModel(), OnErrorListener {
}

private fun shouldResetClozeNumber(number: Int) {
val index = intClozeList.indexOf(number)
if (index != -1) {
intClozeList.removeAt(index)
}
intClozeList.remove(number)

// Reset cloze number if the list is empty
if (intClozeList.isEmpty()) {
_currentClozeNumber.value = 1
} else {
// not null for sure
_currentClozeNumber.value = intClozeList.maxOrNull()!! + 1
}
}

Expand Down Expand Up @@ -248,7 +252,7 @@ class InstantEditorViewModel : ViewModel(), OnErrorListener {
if (currentClozeMode.value == InstantNoteEditorActivity.ClozeMode.INCREMENT) {
incrementClozeNumber()
}
intClozeList.add(currentClozeNumber)
intClozeList.add(clozeNumber)

val punctuation: String? = matcher?.groups?.get(2)?.value
if (!punctuation.isNullOrEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,37 @@ class InstantEditorViewModelTest : RobolectricTest() {
}
}

@Test
fun `test cloze number reset to 1`() = runViewModelTest {
val sentenceArray = mutableListOf("Hello", "world")

toggleAllClozeDeletions(sentenceArray)

assertEquals("all clozes are detected", clozeDeletionCount, 2)

toggleAllClozeDeletions(sentenceArray)

assertEquals("all cloze deletions are removed", clozeDeletionCount, 0)
assertEquals("cloze number is reset if there are no clozes", currentClozeNumber, 1)
}

@Test
fun `test cloze number is reset to max value from cloze list`() = runViewModelTest {
val sentenceArray = mutableListOf("Hello", "world", "this", "is", "test", "sentence")

// cloze on the first 3 words
toggleClozeDeletions(sentenceArray, 0, 1, 2)

// disable cloze on the first 2, leaving "this" as {{c3::
toggleClozeDeletions(sentenceArray, 0, 1)

assertEquals("cloze number is 'Current Cloze Number + 1'", currentClozeNumber, 4)

// remove the remaining cloze all clozes
toggleClozeDeletion(sentenceArray, 2)
assertEquals("cloze number is reset if all clozes are removed", currentClozeNumber, 1)
}

@Test
fun testSavingNoteWithNoCloze() = runViewModelTest {
editorNote.setField(0, "Hello")
Expand Down Expand Up @@ -201,3 +232,24 @@ class InstantEditorViewModelTest : RobolectricTest() {
}
}
}

context (InstantEditorViewModel)
private fun toggleAllClozeDeletions(words: MutableList<String>) {
for (index in words.indices) {
words[index] = buildClozeText(words[index])
}
}

context (InstantEditorViewModel)
@Suppress("SameParameterValue")
private fun toggleClozeDeletions(words: MutableList<String>, vararg indices: Int) {
for (index in indices) {
words[index] = buildClozeText(words[index])
}
}

context (InstantEditorViewModel)
@Suppress("SameParameterValue")
private fun toggleClozeDeletion(words: MutableList<String>, index: Int) {
words[index] = buildClozeText(words[index])
}