Skip to content
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

fix: multimedia options opening more than once #17455

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ class MultimediaActivity : AnkiActivity(), BaseSnackbarBuilderProvider {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_multimedia)
setTransparentStatusBar()

val toolbar: MaterialToolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)

// avoid recreating the fragment on configuration changes
if (savedInstanceState != null) {
return
}

val toolbar: MaterialToolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)

val fragmentClassName =
requireNotNull(intent.getStringExtra(MULTIMEDIA_FRAGMENT_NAME_EXTRA)) {
"'$MULTIMEDIA_FRAGMENT_NAME_EXTRA' extra should be provided"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,16 @@ class MultimediaImageFragment : MultimediaFragment(R.layout.fragment_multimedia_

private lateinit var selectedImageOptions: ImageOptions

/** Keeps track of the process in case `Don't keep activities` in turned on*/
private var hasStartedImageSelection = false

/**
* Launches an activity to pick an image from the device's gallery.
* This launcher is registered using `ActivityResultContracts.StartActivityForResult()`.
*/
private val pickImageLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
hasStartedImageSelection = false
when (result.resultCode) {
Activity.RESULT_CANCELED -> {
if (viewModel.currentMultimediaUri.value == null) {
Expand Down Expand Up @@ -144,6 +148,7 @@ class MultimediaImageFragment : MultimediaFragment(R.layout.fragment_multimedia_
@NeedsTest("Works fine without permission as we use Camera as feature")
private val cameraLauncher =
registerForActivityResult(ActivityResultContracts.TakePicture()) { isPictureTaken ->
hasStartedImageSelection = false
when {
!isPictureTaken && viewModel.currentMultimediaUri.value == null -> {
val resultData = Intent().apply {
Expand All @@ -169,6 +174,7 @@ class MultimediaImageFragment : MultimediaFragment(R.layout.fragment_multimedia_
/** Launches an activity to crop the image, using the [ImageCropper] */
private val imageCropperLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
hasStartedImageSelection = false
when (result.resultCode) {
Activity.RESULT_OK -> {
result.data?.let {
Expand Down Expand Up @@ -251,6 +257,8 @@ class MultimediaImageFragment : MultimediaFragment(R.layout.fragment_multimedia_

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
hasStartedImageSelection = savedInstanceState?.getBoolean("HAS_STARTED_IMAGE_SELECTION", false) ?: false

ankiCacheDirectory = FileUtil.getAnkiCacheDirectory(requireContext(), "temp-photos")
if (ankiCacheDirectory == null) {
Timber.e("createUI() failed to get cache directory")
Expand All @@ -273,6 +281,11 @@ class MultimediaImageFragment : MultimediaFragment(R.layout.fragment_multimedia_
setupDoneButton()
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putBoolean("HAS_STARTED_IMAGE_SELECTION", hasStartedImageSelection)
}

private fun handleImageUri() {
if (imageUri != null) {
view?.findViewById<TextView>(R.id.no_image_textview)?.visibility = View.GONE
Expand All @@ -283,6 +296,12 @@ class MultimediaImageFragment : MultimediaFragment(R.layout.fragment_multimedia_
}

private fun handleSelectedImageOptions() {
if (hasStartedImageSelection) {
Timber.d("Image selection already in progress, skipping.")
return
}
hasStartedImageSelection = true

when (selectedImageOptions) {
ImageOptions.GALLERY -> {
Timber.d("MultimediaImageFragment:: Opening gallery")
Expand Down Expand Up @@ -500,6 +519,7 @@ class MultimediaImageFragment : MultimediaFragment(R.layout.fragment_multimedia_

private fun requestCrop() {
val imageUri = viewModel.currentMultimediaUri.value ?: return
hasStartedImageSelection = true
val intent = com.ichi2.imagecropper.ImageCropperLauncher.ImageUri(imageUri).getIntent(requireContext())
imageCropperLauncher.launch(intent)
}
Expand Down