Skip to content

Commit

Permalink
Refactor to handle onPause and onResume requests
Browse files Browse the repository at this point in the history
  • Loading branch information
zwarm committed Jun 17, 2024
1 parent 5d56725 commit f9fd01a
Showing 1 changed file with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
Expand Down Expand Up @@ -63,6 +64,16 @@ fun VoiceToContentScreen(
val recordingUpdate by viewModel.recordingUpdate.observeAsState(initial = RecordingUpdate())
val configuration = LocalConfiguration.current
val screenHeight = configuration.screenHeightDp.dp
val isRecording by viewModel.isRecording.collectAsState()

DisposableEffect(Unit) {
onDispose {
if (isRecording) {
viewModel.pauseRecording()
}
}
}

// Adjust the bottom sheet height based on orientation
val bottomSheetHeight = if (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
screenHeight // Full height in landscape
Expand All @@ -82,13 +93,16 @@ fun VoiceToContentScreen(
.nestedScroll(rememberNestedScrollInteropConnection()) // Enable nested scrolling for the bottom sheet
.verticalScroll(rememberScrollState()) // Enable vertical scrolling for the bottom sheet
) {
VoiceToContentView(state, recordingUpdate)
VoiceToContentView(state, recordingUpdate, isRecording)
}
}
}

@Composable
fun VoiceToContentView(state: VoiceToContentUiState, recordingUpdate: RecordingUpdate) {
fun VoiceToContentView(state: VoiceToContentUiState,
recordingUpdate: RecordingUpdate,
isRecording: Boolean
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
Expand All @@ -102,7 +116,7 @@ fun VoiceToContentView(state: VoiceToContentUiState, recordingUpdate: RecordingU
else -> {
Header(state.header)
SecondaryHeader(state.secondaryHeader, recordingUpdate)
RecordingPanel(state, recordingUpdate)
RecordingPanel(state, recordingUpdate, isRecording)
}
}
}
Expand Down Expand Up @@ -199,10 +213,7 @@ fun formatTime(remainingTimeInSeconds: Int, maxDurationInSeconds: Int): String {
val minutes = remainingTimeInSeconds / 60
val seconds = remainingTimeInSeconds % 60

val value = if (minutes == 1) default
else String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds)

return value
return String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds)
}

@Composable
Expand All @@ -211,17 +222,17 @@ fun getDefaultTimeString(maxDurationInSeconds: Int): String {
return "00:00"
}

// Calculate minutes and seconds
val minutes = (maxDurationInSeconds - 1) / 60
val seconds = (maxDurationInSeconds - 1) % 60

// Format and return the default time string
return String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds)
}


@Composable
fun RecordingPanel(model: VoiceToContentUiState, recordingUpdate: RecordingUpdate) {
fun RecordingPanel(model: VoiceToContentUiState,
recordingUpdate: RecordingUpdate,
isRecording: Boolean
) {
model.recordingPanel?.let {
Row(
verticalAlignment = Alignment.CenterVertically,
Expand All @@ -248,7 +259,7 @@ fun RecordingPanel(model: VoiceToContentUiState, recordingUpdate: RecordingUpdat
} else if (model.uiStateType == VoiceToContentUIStateType.INELIGIBLE_FOR_FEATURE) {
InEligible(model = it)
}
MicToStopIcon(it)
MicToStopIcon(it, isRecording=isRecording)
Spacer(modifier = Modifier.height(16.dp))
Text(
text = stringResource(id = it.actionLabel),
Expand Down Expand Up @@ -385,7 +396,7 @@ fun PreviewInitializingView() {
hasPermission = false
)
)
VoiceToContentView(state = state, recordingUpdate = RecordingUpdate())
VoiceToContentView(state = state, recordingUpdate = RecordingUpdate(), isRecording = false)
}
}

Expand All @@ -407,7 +418,7 @@ fun PreviewReadyToRecordView() {
isEligibleForFeature = true
)
)
VoiceToContentView(state = state, recordingUpdate = RecordingUpdate())
VoiceToContentView(state = state, recordingUpdate = RecordingUpdate(), isRecording = false)
}
}

Expand All @@ -428,7 +439,7 @@ fun PreviewNotEligibleToRecordView() {
upgradeUrl = "https://www.wordpress.com"
)
)
VoiceToContentView(state = state, recordingUpdate = RecordingUpdate())
VoiceToContentView(state = state, recordingUpdate = RecordingUpdate(), isRecording = false)
}
}

Expand All @@ -451,7 +462,7 @@ fun PreviewRecordingView() {
isEligibleForFeature = true
)
)
VoiceToContentView(state = state, recordingUpdate = RecordingUpdate())
VoiceToContentView(state = state, recordingUpdate = RecordingUpdate(), isRecording = true)
}
}

Expand All @@ -464,6 +475,6 @@ fun PreviewProcessingView() {
uiStateType = VoiceToContentUIStateType.PROCESSING,
header = HeaderUIModel(label = R.string.voice_to_content_processing_label, onClose = { })
)
VoiceToContentView(state = state, recordingUpdate = RecordingUpdate())
VoiceToContentView(state = state, recordingUpdate = RecordingUpdate(), isRecording = false)
}
}

0 comments on commit f9fd01a

Please sign in to comment.