From 3002a516d23907938438b1cb7f4f948e42be6bcd Mon Sep 17 00:00:00 2001 From: Ashish Yadav <48384865+criticalAY@users.noreply.github.com> Date: Sat, 16 Nov 2024 21:30:00 +0530 Subject: [PATCH] fix: multimedia options opening more than once --- AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt b/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt index be13af4e3f0f..bb1c4f93b411 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt @@ -170,6 +170,7 @@ import com.ichi2.utils.positiveButton import com.ichi2.utils.show import com.ichi2.utils.title import com.ichi2.widget.WidgetStatus +import kotlinx.coroutines.Job import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch import org.json.JSONArray @@ -201,6 +202,8 @@ class NoteEditor : AnkiFragment(R.layout.note_editor), DeckSelectionListener, Su private var isTagsEdited = false private var isFieldEdited = false + private var multimediaActionJob: Job? = null + /** * Flag which forces the calling activity to rebuild it's definition of current card from scratch */ @@ -1741,8 +1744,11 @@ class NoteEditor : AnkiFragment(R.layout.note_editor), DeckSelectionListener, Su * @param fieldIndex the index of the field in the note where the multimedia content should be added */ private fun handleMultimediaActions(fieldIndex: Int) { + // Cancel any existing subscription to avoid duplicate listeners + multimediaActionJob?.cancel() + // Based on the type of multimedia action received, perform the corresponding operation - lifecycleScope.launch { + multimediaActionJob = lifecycleScope.launch { val note: MultimediaEditableNote = getCurrentMultimediaEditableNote() if (note.isEmpty) return@launch