Scrolling the Card state sheet can clear a selected state

Open Beginner friendly
#23,545 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
66/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
android, kotlin
Domain
mobile, testing

Research direction

Start with CardStateBottomSheetFragment and its adapter, then inspect CardStateBottomSheetFragmentTest.kt. The issue describes a row recycled from the checked New state to the unchecked Review state clearing the selection without a tap. Reproduce that sequence in the test and confirm the selected New state remains in searchViewModel.filtersFlow.

Written by the indexing model from the issue text.

Description

code-scan

Assisted-by: GPT-6.2-Sol xhigh & GPT-6.2-Astra xhigh

[EXISTING] Scrolling the Card state sheet can clear a selected state

⚠️ This issue existed before the PR and should likely be split off into a separate issue.

Subject: [EXISTING] Scrolling the Card state sheet can clear a selected state

Reusing a checked card state row for an unchecked one can clear the state selection without a tap.

---
diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/browser/search/CardStateBottomSheetFragmentTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/browser/search/CardStateBottomSheetFragmentTest.kt
index 191dfb3bf6..84165bc650 100644
--- a/AnkiDroid/src/test/java/com/ichi2/anki/browser/search/CardStateBottomSheetFragmentTest.kt
+++ b/AnkiDroid/src/test/java/com/ichi2/anki/browser/search/CardStateBottomSheetFragmentTest.kt
@@ -18,6 +18,8 @@ import org.hamcrest.Matchers.equalTo
 import org.junit.Test
 import org.junit.runner.RunWith
 import org.robolectric.Shadows.shadowOf
+import kotlin.test.assertEquals
+import kotlin.test.assertTrue
 
 /** Test of [CardStateBottomSheetFragment] */
 @RunWith(AndroidJUnit4::class)
@@ -37,4 +39,27 @@ class CardStateBottomSheetFragmentTest : RobolectricTest() {
                 assertThat(checkbox.contentDescription, equalTo("New" as CharSequence))
             }
         }
+
+    @Test
+    fun `recycling a card state row retains the selected state`() =
+        withCardBrowserFragment(useSearchView = true) {
+            requireNotNull(searchView).show()
+            advanceRobolectricLooperUntil { searchViewModel.isScreenOpenFlow.value }
+            searchViewModel.setCardStateFilter(listOf(CardState.New))
+
+            val sheet = CardStateBottomSheetFragment()
+            sheet.show(childFragmentManager, CardStateBottomSheetFragment.TAG)
+            advanceRobolectricLooper()
+            val list = sheet.requireView().findViewById<RecyclerView>(R.id.list)
+            val adapter = sheet.adapter
+            val holder = adapter.onCreateViewHolder(list, 0)
+            adapter.onBindViewHolder(holder, CardState.New.ordinal)
+            assertTrue(holder.binding.checkbox.isChecked)
+
+            adapter.onViewRecycled(holder)
+            adapter.onBindViewHolder(holder, CardState.Review.ordinal)
+            advanceRobolectricLooper()
+
+            assertEquals(listOf(CardState.New), searchViewModel.filtersFlow.value.cardStates)
+        }
 }

Originally posted by @david-allison in https://github.com/ankidroid/Anki-Android/pull/23355#pullrequestreview-5968098762

Dominant language
Kotlin
Stars
12.3k
Forks
3.1k
Avg merge
1d 12h
Merged PRs (30d)
377

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ankidroid/Anki-Android

All issues in ankidroid/Anki-Android

Similar issues

More Kotlin issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.