From a2c13cc4bdb6043f4b524856bf67c23271012abf Mon Sep 17 00:00:00 2001 From: Inex Code Date: Thu, 22 Oct 2020 10:19:57 +0300 Subject: [PATCH] fix: return text field to the previous state after editing message closes #9 --- lib/views/chat.dart | 46 +++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/lib/views/chat.dart b/lib/views/chat.dart index 4d736d8..58117d5 100644 --- a/lib/views/chat.dart +++ b/lib/views/chat.dart @@ -97,6 +97,8 @@ class _ChatState extends State<_Chat> { String inputText = ''; + String pendingText = ''; + bool get _canLoadMore => timeline.events.last.type != EventTypes.RoomCreate; void requestHistory() async { @@ -201,12 +203,13 @@ class _ChatState extends State<_Chat> { if (sendController.text.isEmpty) return; room.sendTextEvent(sendController.text, inReplyTo: replyEvent, editEventId: editEvent?.eventId); - sendController.text = ''; + sendController.text = pendingText; setState(() { - inputText = ''; + inputText = pendingText; replyEvent = null; editEvent = null; + pendingText = ''; }); } @@ -353,6 +356,19 @@ class _ChatState extends State<_Chat> { inputFocus.requestFocus(); } + void editAction(Event event) { + setState(() { + pendingText = sendController.text; + editEvent = event; + inputText = sendController.text = editEvent + .getDisplayEvent(timeline) + .getLocalizedBody(MatrixLocals(L10n.of(context)), + withSenderNamePrefix: false, hideReply: true); + selectedEvents.clear(); + }); + inputFocus.requestFocus(); + } + void _scrollToEventId(String eventId, {BuildContext context}) async { var eventIndex = getFilteredEvents().indexWhere((e) => e.eventId == eventId); @@ -521,15 +537,7 @@ class _ChatState extends State<_Chat> { forwardEventsAction(context, event: event); break; case 'edit': - setState(() { - editEvent = event; - sendController.text = editEvent - .getDisplayEvent(timeline) - .getLocalizedBody(MatrixLocals(L10n.of(context)), - withSenderNamePrefix: false, hideReply: true); - selectedEvents.clear(); - }); - inputFocus.requestFocus(); + editAction(event); break; default: } @@ -643,17 +651,7 @@ class _ChatState extends State<_Chat> { selectedEvents.first.senderId == client.userID) IconButton( icon: Icon(Icons.edit), - onPressed: () { - setState(() { - editEvent = selectedEvents.first; - sendController.text = editEvent - .getDisplayEvent(timeline) - .getLocalizedBody(MatrixLocals(L10n.of(context)), - withSenderNamePrefix: false, hideReply: true); - selectedEvents.clear(); - }); - inputFocus.requestFocus(); - }, + onPressed: () => editAction(selectedEvents.first), ), IconButton( icon: Icon(Icons.content_copy), @@ -910,6 +908,10 @@ class _ChatState extends State<_Chat> { IconButton( icon: Icon(Icons.close), onPressed: () => setState(() { + if (editEvent != null) { + inputText = sendController.text = pendingText; + pendingText = ''; + } replyEvent = null; editEvent = null; }),