From 9aa4aec2b5f3cdd617da32d95f71ca44cb0ac792 Mon Sep 17 00:00:00 2001 From: Annmarie Ziegler Date: Fri, 14 Jun 2024 14:06:22 -0400 Subject: [PATCH] Handle closing the bottom sheet on outside touch dynamically --- .../VoiceToContentDialogFragment.kt | 27 ++++++++++++++----- .../voicetocontent/VoiceToContentViewModel.kt | 8 ++++++ 2 files changed, 28 insertions(+), 7 deletions(-) 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 dc57bde0b915..9e99790777ae 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 @@ -56,6 +56,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( @@ -129,6 +132,7 @@ class VoiceToContentViewModel @Inject constructor( private fun startRecording() { transitionToRecording() + disableDialogCancelableOutsideTouch() recordingUseCase.startRecording { audioRecorderResult -> when (audioRecorderResult) { is Success -> { @@ -147,6 +151,10 @@ class VoiceToContentViewModel @Inject constructor( } } + private fun disableDialogCancelableOutsideTouch() { + _isCancelableOutsideTouch.value = false + } + @Suppress("ReturnCount") private fun getRecordingFile(recordingPath: String): File? { if (recordingPath.isEmpty()) return null