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

[Voice to Content] Clear recorder resources #20983

Merged
merged 6 commits into from
Jun 17, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ class RecordingUseCase @Inject constructor(
audioRecorder.startRecording(onRecordingFinished)
}

@Suppress("ReturnCount")
fun stopRecording() {
audioRecorder.stopRecording()
}

fun recordingUpdates(): Flow<RecordingUpdate> {
return audioRecorder.recordingUpdates()
}
}

fun endRecordingSession() {
audioRecorder.endRecordingSession()
}
}
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 @@ -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()
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 @@ -104,6 +107,10 @@ class VoiceToContentViewModel @Inject constructor(
isStarted = true
}

fun onBottomSheetClosed() {
recordingUseCase.endRecordingSession()
}

// Recording
private fun updateRecordingData(recordingUpdate: RecordingUpdate) {
_recordingUpdate.value = recordingUpdate
Expand All @@ -125,6 +132,7 @@ class VoiceToContentViewModel @Inject constructor(

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class AudioRecorder(
}
}

override fun stopRecording() {
private fun clearResources() {
try {
recorder?.apply {
stop()
Expand All @@ -104,7 +104,11 @@ class AudioRecorder(
_isPaused.value = false
_isRecording.value = false
}
// return filePath
}

override fun stopRecording() {
clearResources()
// return the filePath
onRecordingFinished(Success(filePath))
}

Expand Down Expand Up @@ -136,6 +140,10 @@ class AudioRecorder(
}
}

override fun endRecordingSession() {
clearResources()
}

override fun recordingUpdates(): Flow<RecordingUpdate> = recordingUpdates

@Suppress("MagicNumber")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface IAudioRecorder {
fun pauseRecording()
fun resumeRecording()
fun recordingUpdates(): Flow<RecordingUpdate>
fun endRecordingSession()

sealed class AudioRecorderResult {
data class Success(val recordingPath: String) : AudioRecorderResult()
Expand Down
Loading