From eb5c8b905f3236d9b2a8d74f663662a14a7fc737 Mon Sep 17 00:00:00 2001 From: Mathieu B Date: Sat, 25 Mar 2023 12:34:57 +0100 Subject: [PATCH] the app doesn't crash anymore when timeout or server down --- .../broillet/jarvis/android/pages/MainPage.kt | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/ch/broillet/jarvis/android/pages/MainPage.kt b/app/src/main/java/ch/broillet/jarvis/android/pages/MainPage.kt index 4140481..c0cd405 100644 --- a/app/src/main/java/ch/broillet/jarvis/android/pages/MainPage.kt +++ b/app/src/main/java/ch/broillet/jarvis/android/pages/MainPage.kt @@ -31,6 +31,7 @@ import com.github.squti.androidwaverecorder.RecorderState import com.github.squti.androidwaverecorder.WaveRecorder import org.json.JSONObject import java.io.File +import java.io.IOException import kotlin.concurrent.thread @@ -148,6 +149,7 @@ fun RecordingFooterButton( var isListening: Boolean by remember { mutableStateOf(false) } var isProcessing: Boolean by remember { mutableStateOf(false) } + var isError: Boolean by remember { mutableStateOf(false) } // Add a listener for the waveRecorder to record when isListening is true and then process the audio when done listening audioRecorder.waveRecorder.onStateChangeListener = { @@ -157,12 +159,18 @@ fun RecordingFooterButton( thread { isListening = false isProcessing = true + isError = false - processMessage( - processAudio(audioRecorder.getOutputFile()), - navController, - uiState - ) + try { + processMessage( + processAudio(audioRecorder.getOutputFile()), + navController, + uiState + ) + } catch (e: IOException) { + isError = true + e.printStackTrace() + } isProcessing = false audioRecorder.getOutputFile().delete() @@ -192,7 +200,7 @@ fun RecordingFooterButton( DotsFlashing(7.dp, 3, 300, MaterialTheme.colorScheme.secondary, 2.dp) } else { Icon( - painter = painterResource(id = R.drawable.ic_baseline_mic_24), + painter = painterResource(id = (if (isError) R.drawable.ic_baseline_error_24 else R.drawable.ic_baseline_mic_24)), contentDescription = "microphone" ) }