diff --git a/lib/pangea/toolbar/widgets/message_selection_overlay.dart b/lib/pangea/toolbar/widgets/message_selection_overlay.dart index 0eaa7f65a..73451081f 100644 --- a/lib/pangea/toolbar/widgets/message_selection_overlay.dart +++ b/lib/pangea/toolbar/widgets/message_selection_overlay.dart @@ -94,8 +94,8 @@ class MessageOverlayController extends State bool showTranslation = false; String? translationText; - bool showTranscription = false; - String? transcriptText; + String? transcriptionText; + String? transcriptionError; double maxWidth = AppConfig.toolbarMinWidth; @@ -586,14 +586,20 @@ class MessageOverlayController extends State } } - void setShowTranscription(bool show, String? transcription) { - if (showTranscription == show) return; - if (show && transcription == null) return; - + void setTranscriptionText(String transcription) { if (mounted) { setState(() { - showTranscription = show; - transcriptText = show ? transcription : null; + transcriptionError = null; + transcriptionText = transcription; + }); + } + } + + void setTranscriptionError(String error) { + if (mounted) { + setState(() { + transcriptionText = null; + transcriptionError = error; }); } } diff --git a/lib/pangea/toolbar/widgets/overlay_message.dart b/lib/pangea/toolbar/widgets/overlay_message.dart index 8b4ccb611..c4e848851 100644 --- a/lib/pangea/toolbar/widgets/overlay_message.dart +++ b/lib/pangea/toolbar/widgets/overlay_message.dart @@ -4,6 +4,7 @@ import 'package:flutter/material.dart'; import 'package:matrix/matrix.dart'; import 'package:fluffychat/config/app_config.dart'; +import 'package:fluffychat/l10n/l10n.dart'; import 'package:fluffychat/pages/chat/chat.dart'; import 'package:fluffychat/pages/chat/events/message_content.dart'; import 'package:fluffychat/pages/chat/events/reply_content.dart'; @@ -136,8 +137,7 @@ class OverlayMessage extends StatelessWidget { final showTranslation = overlayController.showTranslation && overlayController.translationText != null; - final showTranscription = overlayController.showTranscription && - overlayController.transcriptText != null; + final showTranscription = pangeaMessageEvent?.isAudioMessage == true; final content = Container( decoration: BoxDecoration( @@ -278,17 +278,37 @@ class OverlayMessage extends StatelessWidget { ), child: Padding( padding: const EdgeInsets.all(12.0), - child: SingleChildScrollView( - child: Text( - overlayController.transcriptText!, - style: AppConfig.messageTextStyle( - event, - textColor, - ).copyWith( - fontStyle: FontStyle.italic, - ), - ), - ), + child: overlayController.transcriptionError != null + ? Row( + mainAxisSize: MainAxisSize.min, + children: [ + Icon( + Icons.error_outline, + color: Theme.of(context).colorScheme.error, + ), + const SizedBox(width: 8), + Text( + L10n.of(context).oopsSomethingWentWrong, + style: AppConfig.messageTextStyle( + event, + textColor, + ).copyWith(fontStyle: FontStyle.italic), + ), + ], + ) + : overlayController.transcriptionText != null + ? SingleChildScrollView( + child: Text( + overlayController.transcriptionText!, + style: AppConfig.messageTextStyle( + event, + textColor, + ).copyWith( + fontStyle: FontStyle.italic, + ), + ), + ) + : const LinearProgressIndicator(), ), ), sizeAnimation != null diff --git a/lib/pangea/toolbar/widgets/select_mode_buttons.dart b/lib/pangea/toolbar/widgets/select_mode_buttons.dart index ea93660c7..2b434a835 100644 --- a/lib/pangea/toolbar/widgets/select_mode_buttons.dart +++ b/lib/pangea/toolbar/widgets/select_mode_buttons.dart @@ -27,7 +27,7 @@ enum SelectMode { audio(Icons.volume_up), translate(Icons.translate), practice(Symbols.fitness_center), - transcription(Icons.translate); + speechTranslation(Icons.translate); final IconData icon; const SelectMode(this.icon); @@ -41,7 +41,7 @@ enum SelectMode { return l10n.translationTooltip; case SelectMode.practice: return l10n.practice; - case SelectMode.transcription: + case SelectMode.speechTranslation: return l10n.speechToTextTooltip; } } @@ -72,7 +72,7 @@ class SelectModeButtonsState extends State { ]; static List get audioModes => [ - SelectMode.transcription, + SelectMode.speechTranslation, SelectMode.practice, ]; @@ -91,9 +91,7 @@ class SelectModeButtonsState extends State { PangeaRepresentation? _repEvent; String? _translationError; - bool _isLoadingTranscription = false; SpeechToTextModel? _speechToTextResponse; - String? _transcriptionError; @override void initState() { @@ -113,6 +111,10 @@ class SelectModeButtonsState extends State { ); } }); + + if (messageEvent?.isAudioMessage == true) { + _loadTranscription(); + } } @override @@ -135,7 +137,6 @@ class SelectModeButtonsState extends State { setState(() { _audioError = null; _translationError = null; - _transcriptionError = null; }); widget.overlayController.updateSelectedSpan(null); @@ -143,10 +144,6 @@ class SelectModeButtonsState extends State { if (_selectedMode == SelectMode.translate) { widget.overlayController.setShowTranslation(false, null); } - - if (_selectedMode == SelectMode.transcription) { - widget.overlayController.setShowTranscription(false, null); - } } Future _updateMode(SelectMode? mode) async { @@ -181,20 +178,6 @@ class SelectModeButtonsState extends State { if (_selectedMode == SelectMode.translate) { await _loadTranslation(); - if (_repEvent == null) return; - widget.overlayController.setShowTranslation( - true, - _repEvent!.text, - ); - } - - if (_selectedMode == SelectMode.transcription) { - await _loadTranscription(); - if (_speechToTextResponse == null) return; - widget.overlayController.setShowTranscription( - true, - _speechToTextResponse!.transcript.text, - ); } } @@ -312,6 +295,14 @@ class SelectModeButtonsState extends State { try { await _fetchRepresentation(); + if (_repEvent == null) { + throw "No representation found for the selected language."; + } + + widget.overlayController.setShowTranslation( + true, + _repEvent!.text, + ); } catch (err) { _translationError = err.toString(); ErrorHandler.logError( @@ -326,22 +317,20 @@ class SelectModeButtonsState extends State { } Future _loadTranscription() async { - if (!mounted) return; - setState(() => _isLoadingTranscription = true); - try { await _fetchTranscription(); + widget.overlayController.setTranscriptionText( + _speechToTextResponse!.transcript.text, + ); } catch (err) { - _transcriptionError = err.toString(); + widget.overlayController.setTranscriptionError( + err.toString(), + ); ErrorHandler.logError( e: err, data: {}, ); } - - if (mounted) { - setState(() => _isLoadingTranscription = false); - } } bool get _isError { @@ -350,8 +339,6 @@ class SelectModeButtonsState extends State { return _audioError != null; case SelectMode.translate: return _translationError != null; - case SelectMode.transcription: - return _transcriptionError != null; default: return false; } @@ -363,8 +350,6 @@ class SelectModeButtonsState extends State { return _isLoadingAudio; case SelectMode.translate: return _isLoadingTranslation; - case SelectMode.transcription: - return _isLoadingTranscription; default: return false; }