An open attachment menu adds media to the wrong field after a field reorder

Open
#23,740 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
android, kotlin
Domain
mobile

Research direction

Start with the proposed test in AnkiDroid/src/test/java/com/ichi2/anki/StaleAttachmentSheetTest.kt, which reorders fields via notetypes.changeNotetypeOfNotes and then records an attachment through MultimediaBottomSheet. Trace how MultimediaArgsStorage gets the field index when the sheet was opened before the reorder. Done when the test passes and the recording targets the Back field at index 0 after the reorder.

Written by the indexing model from the issue text.

Description

code-scan

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

[EXISTING] An open attachment menu adds media to the wrong field after a field reorder
Subject: [EXISTING] An open attachment menu adds media to the wrong field after a field reorder

If the note's fields are reordered while its attachment menu is open, the menu's choices still target the field's old position.

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

---
diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/StaleAttachmentSheetTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/StaleAttachmentSheetTest.kt
new file mode 100644
index 0000000000..37ccaa0010
--- /dev/null
+++ b/AnkiDroid/src/test/java/com/ichi2/anki/StaleAttachmentSheetTest.kt
@@ -0,0 +1,78 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package com.ichi2.anki
+
+import android.view.View
+import androidx.core.view.allViews
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import anki.notetypes.copy
+import com.ichi2.anki.CollectionManager.withCol
+import com.ichi2.anki.compat.CompatHelper.Companion.getSerializableExtraCompat
+import com.ichi2.anki.multimedia.MultimediaActivity
+import com.ichi2.anki.multimedia.MultimediaArgsStorage
+import com.ichi2.anki.multimedia.MultimediaBottomSheet
+import com.ichi2.testutils.withSplitPaneUiAsync
+import kotlinx.coroutines.test.runTest
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.Shadows.shadowOf
+import java.io.File
+
+@RunWith(AndroidJUnit4::class)
+class StaleAttachmentSheetTest : RobolectricTest() {
+    @Test
+    fun `an open attachment sheet records for its field after a field reorder`() =
+        runTest {
+            withSplitPaneUiAsync {
+                ensureCollectionLoadIsSynchronous()
+                setCardBrowserFragmented(true)
+                val type = addStandardNoteType("Back first", arrayOf("Back", "Front"), "{{Front}}", "{{Back}}")
+                val note = addBasicNote("Question", "Answer")
+                val cardId = note.firstCard().id
+                val browser = getBrowserWithNotes(0)
+                browser.viewModel.setQuery("deck:Default", forceRefresh = true)
+                browser.waitForSearchResults()
+                advanceRobolectricLooper()
+                val editor = requireNotNull(browser.fragment)
+
+                editor
+                    .requireView()
+                    .allViews
+                    .filterIsInstance<FieldEditLine>()
+                    .elementAt(1)
+                    .binding.mediaButton
+                    .performClick()
+                advanceRobolectricLooper()
+                val sheet = browser.supportFragmentManager.findFragmentByTag("MultimediaBottomSheet") as MultimediaBottomSheet
+
+                withCol {
+                    val target = notetypes.byName(type)!!.id
+                    val input =
+                        notetypes.changeNotetypeInfo(note.noteTypeId, target).input.copy {
+                            noteIds.add(note.id)
+                            newFields[0] = 1
+                            newFields[1] = 0
+                            newTemplates[0] = 0
+                        }
+                    notetypes.changeNotetypeOfNotes(input)
+                }
+                browser.viewModel.setQuery("deck:Default", forceRefresh = true)
+                browser.waitForSearchResults()
+                advanceRobolectricLooper()
+                assertThat("the current card is retained", editor.currentEditedCard!!.id, equalTo(cardId))
+                assertThat("Back moved to the first slot", editor.editFieldAt(0)!!.text.toString(), equalTo("Answer"))
+                assertThat("the Back attachment sheet remains open", sheet.dialog?.isShowing, equalTo(true))
+
+                sheet.requireView().findViewById<View>(R.id.multimedia_action_recording).performClick()
+                advanceRobolectricLooper()
+
+                val shadow = shadowOf(browser)
+                val intent = shadow.nextStartedActivityForResult?.intent ?: shadow.nextStartedActivity
+                val file = requireNotNull(requireNotNull(intent).getSerializableExtraCompat<File>(MultimediaActivity.EXTRA_FRAGMENT_ARGS))
+                val recordingFor = MultimediaArgsStorage.create(targetContext).read(file).index
+                assertThat("the recording is for Back, now the first field", recordingFor, equalTo(0))
+            }
+        }
+}

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

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

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.