diff --git a/WordPress/src/main/java/org/wordpress/android/ui/voicetocontent/RecordingUseCase.kt b/WordPress/src/main/java/org/wordpress/android/ui/voicetocontent/RecordingUseCase.kt index 3552ea314fe2..069d10464522 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/voicetocontent/RecordingUseCase.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/voicetocontent/RecordingUseCase.kt @@ -14,7 +14,6 @@ class RecordingUseCase @Inject constructor( audioRecorder.startRecording(onRecordingFinished) } - @Suppress("ReturnCount") fun stopRecording() { audioRecorder.stopRecording() } @@ -22,5 +21,8 @@ class RecordingUseCase @Inject constructor( fun recordingUpdates(): Flow { return audioRecorder.recordingUpdates() } -} + fun endRecordingSession() { + audioRecorder.endRecordingSession() + } +} 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 ff491318e2bc..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 @@ -64,12 +66,44 @@ class VoiceToContentDialogFragment : BottomSheetDialogFragment() { behavior.skipCollapsed = true behavior.state = BottomSheetBehavior.STATE_EXPANDED + behavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() { + @SuppressLint("SwitchIntDef") + override fun onStateChanged(bottomSheet: View, newState: Int) { + when (newState) { + BottomSheetBehavior.STATE_HIDDEN, + BottomSheetBehavior.STATE_COLLAPSED -> { + onBottomSheetClosed() + } + } + } + + override fun onSlide(bottomSheet: View, slideOffset: Float) { + // Handle the slide offset if needed + } + }) + // 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 6bb4264b368a..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( @@ -104,6 +107,10 @@ class VoiceToContentViewModel @Inject constructor( isStarted = true } + fun onBottomSheetClosed() { + recordingUseCase.endRecordingSession() + } + // Recording private fun updateRecordingData(recordingUpdate: RecordingUpdate) { _recordingUpdate.value = recordingUpdate @@ -125,6 +132,7 @@ class VoiceToContentViewModel @Inject constructor( private fun startRecording() { transitionToRecording() + disableDialogCancelableOutsideTouch() recordingUseCase.startRecording { audioRecorderResult -> when (audioRecorderResult) { is Success -> { @@ -143,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 @@ -206,6 +218,7 @@ class VoiceToContentViewModel @Inject constructor( private fun onClose() { logger.track(Stat.VOICE_TO_CONTENT_BUTTON_CLOSE_TAPPED) + recordingUseCase.endRecordingSession() _dismiss.postValue(Unit) } diff --git a/WordPress/src/main/java/org/wordpress/android/util/audio/AudioRecorder.kt b/WordPress/src/main/java/org/wordpress/android/util/audio/AudioRecorder.kt index 73f957289151..12c0a01b2f65 100644 --- a/WordPress/src/main/java/org/wordpress/android/util/audio/AudioRecorder.kt +++ b/WordPress/src/main/java/org/wordpress/android/util/audio/AudioRecorder.kt @@ -90,7 +90,7 @@ class AudioRecorder( } } - override fun stopRecording() { + private fun clearResources() { try { recorder?.apply { stop() @@ -104,7 +104,11 @@ class AudioRecorder( _isPaused.value = false _isRecording.value = false } - // return filePath + } + + override fun stopRecording() { + clearResources() + // return the filePath onRecordingFinished(Success(filePath)) } @@ -136,6 +140,10 @@ class AudioRecorder( } } + override fun endRecordingSession() { + clearResources() + } + override fun recordingUpdates(): Flow = recordingUpdates @Suppress("MagicNumber") diff --git a/WordPress/src/main/java/org/wordpress/android/util/audio/IAudioRecorder.kt b/WordPress/src/main/java/org/wordpress/android/util/audio/IAudioRecorder.kt index 73ab0ca30725..78cbd55efbad 100644 --- a/WordPress/src/main/java/org/wordpress/android/util/audio/IAudioRecorder.kt +++ b/WordPress/src/main/java/org/wordpress/android/util/audio/IAudioRecorder.kt @@ -9,6 +9,7 @@ interface IAudioRecorder { fun pauseRecording() fun resumeRecording() fun recordingUpdates(): Flow + fun endRecordingSession() sealed class AudioRecorderResult { data class Success(val recordingPath: String) : AudioRecorderResult()