From f8cc559ab3747bd162f5f3718faa444793b20cb0 Mon Sep 17 00:00:00 2001 From: Krille Fear Date: Sat, 30 Oct 2021 10:39:00 +0200 Subject: [PATCH] fix: Record voice messages --- lib/pages/chat.dart | 6 ++--- lib/pages/recording_dialog.dart | 47 +++++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/lib/pages/chat.dart b/lib/pages/chat.dart index 37e20c36..08e26e85 100644 --- a/lib/pages/chat.dart +++ b/lib/pages/chat.dart @@ -356,13 +356,13 @@ class ChatController extends State { void voiceMessageAction() async { if (await Record().hasPermission() == false) return; - final result = await showDialog( + final result = await showDialog( context: context, useRootNavigator: false, builder: (c) => const RecordingDialog(), ); if (result == null) return; - final audioFile = File(result['path']); + final audioFile = File(result.path); final file = MatrixAudioFile( bytes: audioFile.readAsBytesSync(), name: audioFile.path, @@ -373,7 +373,7 @@ class ChatController extends State { room.sendFileEvent(file, inReplyTo: replyEvent, extraContent: { 'info': { ...file.info, - 'duration': result['duration'], + 'duration': result.duration, } }), ); diff --git a/lib/pages/recording_dialog.dart b/lib/pages/recording_dialog.dart index 70c7a410..a62c08ad 100644 --- a/lib/pages/recording_dialog.dart +++ b/lib/pages/recording_dialog.dart @@ -73,6 +73,16 @@ class _RecordingDialogState extends State { super.dispose(); } + void _stopAndSend() async { + _recorderSubscription?.cancel(); + await _audioRecorder.stop(); + Navigator.of(context, rootNavigator: false) + .pop(RecordingResult( + path: _recordedPath, + duration: _duration.inMilliseconds, + )); + } + @override Widget build(BuildContext context) { const maxDecibalWidth = 64.0; @@ -130,14 +140,7 @@ class _RecordingDialogState extends State { ), if (error != true) CupertinoDialogAction( - onPressed: () async { - _recorderSubscription?.cancel(); - await _audioRecorder.stop(); - Navigator.of(context, rootNavigator: false).pop({ - 'path': _recordedPath, - 'duration': _duration.inMilliseconds, - }); - }, + onPressed: _stopAndSend, child: Text(L10n.of(context).send.toUpperCase()), ), ], @@ -157,12 +160,7 @@ class _RecordingDialogState extends State { ), if (error != true) TextButton( - onPressed: () async { - _recorderSubscription?.cancel(); - await _audioRecorder.stop(); - Navigator.of(context, rootNavigator: false) - .pop(_recordedPath); - }, + onPressed: _stopAndSend, child: Row( mainAxisSize: MainAxisSize.min, children: [ @@ -176,3 +174,24 @@ class _RecordingDialogState extends State { ); } } + +class RecordingResult { + final String path; + final int duration; + + const RecordingResult({ + @required this.path, + @required this.duration, + }); + + factory RecordingResult.fromJson(Map json) => + RecordingResult( + path: json['path'], + duration: json['duration'], + ); + + Map toJson() => { + 'path': path, + 'duration': duration, + }; +}