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

Conversation

criticalAY
Copy link
Contributor

Purpose / Description

Cloze number is reset to 1 or the current cloze number is max of the close number list

Fixes

Approach

How does this change address the problem?

How Has This Been Tested?

Oneplus nord ce

Checklist

Please, go through these checks before submitting the PR.

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • UI Changes: You have tested your change using the Google Accessibility Scanner

@criticalAY criticalAY added Needs Review GSoC Pull requests authored by a Google Summer of Code participant [Candidate/Selected], for GSoC mentors labels Jul 25, 2024
@david-allison
Copy link
Member

You should add a test as well

Copy link
Member

@ShridharGoel ShridharGoel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@ShridharGoel ShridharGoel added Needs Second Approval Has one approval, one more approval to merge and removed Needs Review labels Jul 25, 2024
Copy link
Member

@david-allison david-allison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel the tests are readable.

Subject: [PATCH] aa
---
Index: AnkiDroid/src/test/java/com/ichi2/anki/instanteditor/InstantEditorViewModelTest.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/instanteditor/InstantEditorViewModelTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/instanteditor/InstantEditorViewModelTest.kt
--- a/AnkiDroid/src/test/java/com/ichi2/anki/instanteditor/InstantEditorViewModelTest.kt	(revision 3ae8b861623eb072b52b0575a12329ed00460db8)
+++ b/AnkiDroid/src/test/java/com/ichi2/anki/instanteditor/InstantEditorViewModelTest.kt	(date 1722110794241)
@@ -58,34 +58,32 @@
     @Test
     fun `test cloze number reset to 1`() = runViewModelTest {
         val sentenceArray = mutableListOf("Hello", "world")
-        sentenceArray[0] = buildClozeText(sentenceArray[0])
-        sentenceArray[1] = buildClozeText(sentenceArray[1])
+        
+        toggleAllClozeDeletions(sentenceArray)
 
-        assertEquals(intClozeList.size, 2)
+        assertEquals("all clozes are detected", clozeDeletionCount, 2)
 
-        buildClozeText(sentenceArray[0])
-        buildClozeText(sentenceArray[1])
+        toggleAllClozeDeletions(sentenceArray)
 
-        assertEquals(intClozeList.size, 0)
-        assertEquals(currentClozeNumber, 1)
+        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")
 
-        sentenceArray[0] = buildClozeText(sentenceArray[0])
-        sentenceArray[1] = buildClozeText(sentenceArray[1])
-        sentenceArray[2] = buildClozeText(sentenceArray[2])
+        // cloze on the first 3 words
+        toggleClozeDeletions(sentenceArray, 0, 1, 2)
 
-        buildClozeText(sentenceArray[0])
-        buildClozeText(sentenceArray[1])
+        // disable cloze on the first 2, leaving "this" as {{c3::
+        toggleClozeDeletions(sentenceArray, 0, 1)
 
-        assertEquals(currentClozeNumber, 4)
+        assertEquals("cloze number is 'Current Cloze Number + 1'", currentClozeNumber, 4)
 
-        // removing all clozes
-        buildClozeText(sentenceArray[2])
-        assertEquals(currentClozeNumber, 1)
+        // remove the remaining cloze all clozes
+        toggleClozeDeletion(sentenceArray, 2)
+        assertEquals("cloze number is reset if all clozes are removed", currentClozeNumber, 1)
     }
 
     @Test
@@ -234,3 +232,25 @@
         }
     }
 }
+
+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])
+}
\ No newline at end of file
Index: AnkiDroid/src/main/java/com/ichi2/anki/instantnoteeditor/InstantEditorViewModel.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/instantnoteeditor/InstantEditorViewModel.kt b/AnkiDroid/src/main/java/com/ichi2/anki/instantnoteeditor/InstantEditorViewModel.kt
--- a/AnkiDroid/src/main/java/com/ichi2/anki/instantnoteeditor/InstantEditorViewModel.kt	(revision 3ae8b861623eb072b52b0575a12329ed00460db8)
+++ b/AnkiDroid/src/main/java/com/ichi2/anki/instantnoteeditor/InstantEditorViewModel.kt	(date 1722109604855)
@@ -60,7 +60,11 @@
         get() = _currentClozeNumber.value
 
     // List to store the cloze integers
-    val intClozeList = mutableListOf<Int>()
+    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)
 

@david-allison david-allison added the Needs Author Reply Waiting for a reply from the original author label Jul 27, 2024
@criticalAY criticalAY requested a review from david-allison July 28, 2024 10:48
@criticalAY criticalAY removed the Needs Author Reply Waiting for a reply from the original author label Jul 28, 2024
@david-allison david-allison added Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) squash-merge Squash & force push by author/maintainers requested and removed Needs Second Approval Has one approval, one more approval to merge labels Jul 28, 2024
* refactor: update the viewmodel tests
@criticalAY criticalAY removed the squash-merge Squash & force push by author/maintainers requested label Jul 28, 2024
@david-allison david-allison enabled auto-merge July 28, 2024 11:27
@david-allison david-allison added this pull request to the merge queue Jul 28, 2024
Merged via the queue into ankidroid:main with commit f52b627 Jul 28, 2024
8 checks passed
@github-actions github-actions bot added this to the 2.19 release milestone Jul 28, 2024
@github-actions github-actions bot removed the Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) label Jul 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GSoC Pull requests authored by a Google Summer of Code participant [Candidate/Selected], for GSoC mentors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG]: Instant Add Editor - wrong Cloze number
3 participants