feat(voice-input): Refactor voice input availability check logic

This commit is contained in:
cogwheel0
2025-11-27 20:18:42 +05:30
parent 9ea8949e4f
commit 32dab33fc4

View File

@@ -998,19 +998,28 @@ final voiceInputServiceProvider = Provider<VoiceInputService>((ref) {
Future<bool> voiceInputAvailable(Ref ref) async { Future<bool> voiceInputAvailable(Ref ref) async {
final service = ref.watch(voiceInputServiceProvider); final service = ref.watch(voiceInputServiceProvider);
if (!service.isSupportedPlatform) return false; if (!service.isSupportedPlatform) return false;
final initialized = await service.initialize();
if (!initialized) return false; // IMPORTANT:
switch (service.preference) { // Do NOT initialize STT or request microphone/speech permissions here.
case SttPreference.deviceOnly: // This provider is watched by the chat UI during app startup; calling
if (service.hasLocalStt) return true; // initialize() or checkPermissions() would trigger permission dialogs
if (!service.hasServerStt) return false; // before the user explicitly opts into voice features.
break; //
case SttPreference.serverOnly: // Instead, treat voice input as "available" based on platform support
// and configuration only. The actual initialization + permission flow
// happens on-demand via VoiceInputService.beginListening().
// If the user prefers server-only STT, only expose voice input when a
// server STT backend is configured.
if (service.preference == SttPreference.serverOnly) {
return service.hasServerStt; return service.hasServerStt;
} }
final hasPermission = await service.checkPermissions();
if (!hasPermission) return false; // For device-only (or mixed) preferences, assume voice input is
return service.isAvailable; // potentially available on supported platforms. Any missing
// permissions or lack of local STT support will be handled when
// beginListening() is called.
return true;
} }
final voiceInputStreamProvider = StreamProvider<String>((ref) { final voiceInputStreamProvider = StreamProvider<String>((ref) {