From 0e4c873e52ca576d32ec803e3c547db5219a2a52 Mon Sep 17 00:00:00 2001 From: Maximilian Krause Date: Thu, 21 Nov 2024 16:12:33 +0100 Subject: [PATCH] feat: some stability & alignment improvements --- .../src/main/java/com/voicekit/VoiceError.kt | 4 +++ .../main/java/com/voicekit/VoiceKitService.kt | 16 ++++++++-- example/src/App.tsx | 3 +- ios/Core/VoiceKitService.swift | 31 +++++++++++++++---- ios/Utils/VoiceError.swift | 4 +++ src/types/native.ts | 7 +++++ 6 files changed, 56 insertions(+), 9 deletions(-) diff --git a/android/src/main/java/com/voicekit/VoiceError.kt b/android/src/main/java/com/voicekit/VoiceError.kt index 82bd4e9..a1827f7 100644 --- a/android/src/main/java/com/voicekit/VoiceError.kt +++ b/android/src/main/java/com/voicekit/VoiceError.kt @@ -28,6 +28,10 @@ sealed class VoiceError( "ERR_PERMISSION_NOT_DETERMINED", "Speech recognition permission was not yet determined" ) + object InvalidState : VoiceError( + "ERR_INVALID_STATE", + "Invalid state, cannot perform action" + ) class Unknown(message: String = "An unknown error occurred") : VoiceError( "ERR_UNKNOWN", message diff --git a/android/src/main/java/com/voicekit/VoiceKitService.kt b/android/src/main/java/com/voicekit/VoiceKitService.kt index 61ca1f6..767d11b 100644 --- a/android/src/main/java/com/voicekit/VoiceKitService.kt +++ b/android/src/main/java/com/voicekit/VoiceKitService.kt @@ -59,13 +59,19 @@ class VoiceKitService(private val context: ReactApplicationContext) { audioManager?.setStreamVolume(AudioManager.STREAM_NOTIFICATION, previousNotificationVolume, AudioManager.FLAG_ALLOW_RINGER_MODES) } - fun startListening(options: ReadableMap, skipMuteBeep: Boolean = false): Boolean { + fun startListening(options: ReadableMap, skipMuteBeep: Boolean = false) { val currentActivity = context.currentActivity if (currentActivity == null) { Log.e(TAG, "Activity is null") throw VoiceError.Unknown("Activity is null") } + if (isListening) { + Log.w(TAG, "Already listening, aborting startListening") + sendEvent("RNVoiceKit.error", VoiceError.InvalidState) + return + } + this.options = options // TODO: We currently don't wait for the permission to be granted, but we should @@ -114,10 +120,16 @@ class VoiceKitService(private val context: ReactApplicationContext) { isListening = true - return true + return } fun stopListening() { + if (!isListening) { + Log.w(TAG, "Not listening, aborting stopListening") + sendEvent("RNVoiceKit.error", VoiceError.InvalidState) + return + } + lastResultTimer?.removeCallbacksAndMessages(null) lastResultTimer = null lastTranscription = null diff --git a/example/src/App.tsx b/example/src/App.tsx index 992d347..fdf393f 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -19,8 +19,9 @@ export default function App() { console.error('Error starting listening', error, error instanceof VoiceError ? error.details : null); }); }} + disabled={!available || listening} /> -