fix: if there's an error while building transcript in tts card, show error card (#1281)

pull/1544/head
ggurdin 11 months ago committed by GitHub
parent c8bf68e4cd
commit 06817ba4cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -35,6 +35,7 @@ class MessageSpeechToTextCardState extends State<MessageSpeechToTextCard> {
bool _fetchingTranscription = true; bool _fetchingTranscription = true;
Object? error; Object? error;
STTToken? selectedToken; STTToken? selectedToken;
TextSpan? transcriptText;
String? get l1Code => String? get l1Code =>
MatrixState.pangeaController.languageController.activeL1Code(); MatrixState.pangeaController.languageController.activeL1Code();
@ -70,6 +71,7 @@ class MessageSpeechToTextCardState extends State<MessageSpeechToTextCard> {
} }
TextSpan _buildTranscriptText(BuildContext context) { TextSpan _buildTranscriptText(BuildContext context) {
try {
final Transcript transcript = speechToTextResponse!.transcript; final Transcript transcript = speechToTextResponse!.transcript;
final List<InlineSpan> spans = []; final List<InlineSpan> spans = [];
String remainingFullText = transcript.text; String remainingFullText = transcript.text;
@ -79,9 +81,6 @@ class MessageSpeechToTextCardState extends State<MessageSpeechToTextCard> {
text: remainingFullText, text: remainingFullText,
style: BotStyle.text( style: BotStyle.text(
context, context,
existingStyle: TextStyle(
color: Theme.of(context).colorScheme.onSurface,
),
setColor: false, setColor: false,
), ),
); );
@ -89,9 +88,11 @@ class MessageSpeechToTextCardState extends State<MessageSpeechToTextCard> {
for (final token in transcript.sttTokens) { for (final token in transcript.sttTokens) {
final offset = remainingFullText.indexOf(token.token.text.content); final offset = remainingFullText.indexOf(token.token.text.content);
if (offset == -1) continue;
final length = token.length; final length = token.length;
if (remainingFullText.substring(0, offset).trim().isNotEmpty) { if (remainingFullText.substring(0, offset).trim().isNotEmpty) {
remainingFullText = remainingFullText.substring(offset);
continue; continue;
} }
@ -140,12 +141,21 @@ class MessageSpeechToTextCardState extends State<MessageSpeechToTextCard> {
} }
return TextSpan(children: spans); return TextSpan(children: spans);
} catch (err, s) {
ErrorHandler.logError(e: err, s: s);
setState(() => error = err);
return const TextSpan(text: '');
}
} }
@override @override
void initState() { void initState() {
super.initState(); super.initState();
getSpeechToText(); getSpeechToText().then((_) {
if (mounted) {
setState(() => transcriptText = _buildTranscriptText(context));
}
});
} }
String? get wordsPerMinuteString => String? get wordsPerMinuteString =>
@ -158,7 +168,7 @@ class MessageSpeechToTextCardState extends State<MessageSpeechToTextCard> {
} }
// done fetchig but not results means some kind of error // done fetchig but not results means some kind of error
if (speechToTextResponse == null) { if (speechToTextResponse == null || error != null) {
return CardErrorWidget( return CardErrorWidget(
error: error ?? "Failed to fetch speech to text", error: error ?? "Failed to fetch speech to text",
maxWidth: AppConfig.toolbarMinWidth, maxWidth: AppConfig.toolbarMinWidth,
@ -173,7 +183,7 @@ class MessageSpeechToTextCardState extends State<MessageSpeechToTextCard> {
children: [ children: [
const SizedBox(height: 8), const SizedBox(height: 8),
RichText( RichText(
text: _buildTranscriptText(context), text: transcriptText!,
), ),
if (widget.messageEvent.senderId == Matrix.of(context).client.userID) if (widget.messageEvent.senderId == Matrix.of(context).client.userID)
Column( Column(

Loading…
Cancel
Save