Skip to content

Commit

Permalink
feat: Add a clear() method that clears the queue and stops any uttera… (
Browse files Browse the repository at this point in the history
#1117)

* feat: Add a clear() method that clears the queue and stops any utterances

* feat: add clear method to TextToSpeechPlugin and call it when TTS button is clicked to be disabled
  • Loading branch information
jessielw authored Nov 22, 2023
1 parent a57035c commit 7484f21
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import 'package:rtchat/models/channels.dart';
import 'package:rtchat/models/layout.dart';
import 'package:rtchat/models/tts.dart';
import 'package:rtchat/models/user.dart';
// import 'package:rtchat/tts_plugin.dart';
import 'package:rtchat/tts_plugin.dart';
import 'package:wakelock/wakelock.dart';
import 'dart:math' as math;

Expand Down Expand Up @@ -241,6 +241,10 @@ class _HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
// Toggle the enabled state
ttsModel.enabled = !ttsModel.enabled;

if (!ttsModel.enabled) {
await TextToSpeechPlugin.clear();
}

// if (ttsModel.enabled) {
// // Test speak method with a long string
// await TextToSpeechPlugin.speak(
Expand Down
21 changes: 21 additions & 0 deletions lib/tts_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ class TextToSpeechPlugin {
// Handle the error
}
}

static Future<void> clear() async {
try {
await _channel.invokeMethod('clear');
} catch (e) {
// Handle the error
}
}
}

class TTSQueue {
Expand Down Expand Up @@ -70,4 +78,17 @@ class TTSQueue {
Future<void> delete(String id) async {
_queue.removeWhere((speak) => speak['id'] == id);
}

void clear() {
_queue.clear();
_stopSpeaking();
}

Future<void> _stopSpeaking() async {
try {
await _channel.invokeMethod('stopSpeaking');
} catch (e) {
// handle the error;
}
}
}

0 comments on commit 7484f21

Please sign in to comment.