diff --git a/WordPress/src/main/java/org/wordpress/android/ui/voicetocontent/VoiceToContentDialogFragment.kt b/WordPress/src/main/java/org/wordpress/android/ui/voicetocontent/VoiceToContentDialogFragment.kt index 5fdd8d1aa90c..7375ae03ffc2 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/voicetocontent/VoiceToContentDialogFragment.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/voicetocontent/VoiceToContentDialogFragment.kt @@ -2,6 +2,7 @@ package org.wordpress.android.ui.voicetocontent import android.annotation.SuppressLint import android.app.Dialog +import android.content.DialogInterface import android.content.Intent import android.net.Uri import android.os.Bundle @@ -18,6 +19,7 @@ import org.wordpress.android.ui.compose.theme.AppTheme import org.wordpress.android.R import org.wordpress.android.util.audio.IAudioRecorder.Companion.REQUIRED_RECORDING_PERMISSIONS import android.provider.Settings +import android.util.Log import android.widget.FrameLayout import androidx.compose.material.ExperimentalMaterialApi import com.google.android.material.bottomsheet.BottomSheetBehavior @@ -68,15 +70,10 @@ class VoiceToContentDialogFragment : BottomSheetDialogFragment() { @SuppressLint("SwitchIntDef") override fun onStateChanged(bottomSheet: View, newState: Int) { when (newState) { - BottomSheetBehavior.STATE_HIDDEN -> { - // Bottom sheet is hidden, you can listen for this event here - viewModel.onBottomSheetClosed() - } + BottomSheetBehavior.STATE_HIDDEN, BottomSheetBehavior.STATE_COLLAPSED -> { - // Bottom sheet is collapsed, you can listen for this event here - viewModel.onBottomSheetClosed() + onBottomSheetClosed() } - // Handle other states if necessary } } @@ -88,9 +85,25 @@ class VoiceToContentDialogFragment : BottomSheetDialogFragment() { // Disable touch interception by the bottom sheet to allow nested scrolling bottomSheet.setOnTouchListener { _, _ -> false } } + + // Observe the ViewModel to update the cancelable state of closing on outside touch + viewModel.isCancelableOutsideTouch.observe(this) { cancelable -> + Log.i(javaClass.simpleName, "***=> disable outside touch") + dialog.setCanceledOnTouchOutside(cancelable) + } + return dialog } + override fun onDismiss(dialog: DialogInterface) { + super.onDismiss(dialog) + viewModel.onBottomSheetClosed() + } + + private fun onBottomSheetClosed() { + dismiss() + } + private fun observeViewModel() { viewModel.requestPermission.observe(viewLifecycleOwner) { requestAllPermissionsForRecording() diff --git a/WordPress/src/main/java/org/wordpress/android/ui/voicetocontent/VoiceToContentViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/voicetocontent/VoiceToContentViewModel.kt index a96c0c71ef0c..68fb9d361a32 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/voicetocontent/VoiceToContentViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/voicetocontent/VoiceToContentViewModel.kt @@ -57,6 +57,9 @@ class VoiceToContentViewModel @Inject constructor( private val _onIneligibleForVoiceToContent = MutableLiveData() val onIneligibleForVoiceToContent = _onIneligibleForVoiceToContent as LiveData + private val _isCancelableOutsideTouch = MutableLiveData(true) + val isCancelableOutsideTouch: LiveData get() = _isCancelableOutsideTouch + private var isStarted = false private val _state = MutableStateFlow(VoiceToContentUiState( @@ -130,6 +133,7 @@ class VoiceToContentViewModel @Inject constructor( private fun startRecording() { transitionToRecording() + disableDialogCancelableOutsideTouch() recordingUseCase.startRecording { audioRecorderResult -> when (audioRecorderResult) { is Success -> { @@ -149,6 +153,10 @@ class VoiceToContentViewModel @Inject constructor( } } + private fun disableDialogCancelableOutsideTouch() { + _isCancelableOutsideTouch.value = false + } + @Suppress("ReturnCount") private fun getRecordingFile(recordingPath: String): File? { if (recordingPath.isEmpty()) return null