Skip to content

Commit

Permalink
Handle closing the bottom sheet on outside touch dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
zwarm committed Jun 14, 2024
1 parent fc7a0fb commit 9aa4aec
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
}

Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class VoiceToContentViewModel @Inject constructor(
private val _onIneligibleForVoiceToContent = MutableLiveData<String>()
val onIneligibleForVoiceToContent = _onIneligibleForVoiceToContent as LiveData<String>

private val _isCancelableOutsideTouch = MutableLiveData(true)
val isCancelableOutsideTouch: LiveData<Boolean> get() = _isCancelableOutsideTouch

private var isStarted = false

private val _state = MutableStateFlow(VoiceToContentUiState(
Expand Down Expand Up @@ -129,6 +132,7 @@ class VoiceToContentViewModel @Inject constructor(

private fun startRecording() {
transitionToRecording()
disableDialogCancelableOutsideTouch()
recordingUseCase.startRecording { audioRecorderResult ->
when (audioRecorderResult) {
is Success -> {
Expand All @@ -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
Expand Down

0 comments on commit 9aa4aec

Please sign in to comment.