From f6fbbc0aa3286225b07bbfc55b5601829224068c Mon Sep 17 00:00:00 2001 From: Quang Huy Nguyen Date: Tue, 18 Jun 2024 20:39:00 +0700 Subject: [PATCH] TW-1777: Changing greeting message: include tag in greeting message (#1881) --- lib/pages/chat/events/html_message.dart | 3 +- lib/pages/chat/input_bar/input_bar.dart | 4 +- lib/pages/chat_draft/draft_chat.dart | 54 +++++++++++++------ .../chat_draft/draft_chat_input_row.dart | 1 + 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/lib/pages/chat/events/html_message.dart b/lib/pages/chat/events/html_message.dart index d6e7d98114..f7189aa397 100644 --- a/lib/pages/chat/events/html_message.dart +++ b/lib/pages/chat/events/html_message.dart @@ -180,7 +180,8 @@ class HtmlMessage extends StatelessWidget { final user = room.getUser(identifier); final displayName = user?.displayName ?? identifier; return MentionedUser( - displayName: displayName.displayMentioned, + displayName: + !room.isDirectChat ? displayName.displayMentioned : displayName, url: url, onTap: !room.isDirectChat ? onTap : null, textStyle: !room.isDirectChat diff --git a/lib/pages/chat/input_bar/input_bar.dart b/lib/pages/chat/input_bar/input_bar.dart index ec998cd63c..1ebac66e21 100644 --- a/lib/pages/chat/input_bar/input_bar.dart +++ b/lib/pages/chat/input_bar/input_bar.dart @@ -41,6 +41,7 @@ class InputBar extends StatefulWidget { final ValueKey? typeAheadKey; final ValueNotifier? showEmojiPickerNotifier; final SuggestionsController>? suggestionsController; + final bool isDraftChat; const InputBar({ this.room, @@ -60,6 +61,7 @@ class InputBar extends StatefulWidget { this.rawKeyboardFocusNode, this.suggestionsController, this.showEmojiPickerNotifier, + this.isDraftChat = false, super.key, }); @@ -261,7 +263,7 @@ class _InputBarState extends State with PasteImageMixin { } void insertSuggestion(Map suggestion) { - if (widget.room!.isDirectChat) return; + if (widget.room!.isDirectChat && !widget.isDraftChat) return; final replaceText = widget.controller!.text .substring(0, widget.controller!.selection.baseOffset); var startText = ''; diff --git a/lib/pages/chat_draft/draft_chat.dart b/lib/pages/chat_draft/draft_chat.dart index a44689f328..dedc30a039 100644 --- a/lib/pages/chat_draft/draft_chat.dart +++ b/lib/pages/chat_draft/draft_chat.dart @@ -89,6 +89,8 @@ class DraftChatController extends State ValueNotifier showEmojiPickerNotifier = ValueNotifier(false); + final ValueNotifier _userProfile = ValueNotifier(null); + EmojiPickerType emojiPickerType = EmojiPickerType.keyboard; final isSendingNotifier = ValueNotifier(false); @@ -146,6 +148,9 @@ class DraftChatController extends State void initState() { scrollController.addListener(_updateScrollController); keyboardVisibilityController.onChange.listen(_keyboardListener); + WidgetsBinding.instance.addPostFrameCallback((_) { + _getProfile(); + }); super.initState(); } @@ -157,6 +162,7 @@ class DraftChatController extends State focusSuggestionController.dispose(); inputText.dispose(); showEmojiPickerNotifier.dispose(); + _userProfile.dispose(); super.dispose(); } @@ -166,6 +172,22 @@ class DraftChatController extends State Matrix.of(context).setActiveClient(c); }); + Future _triggerTagGreetingMessage() async { + if (_userProfile.value == null) { + return sendController.value.text; + } else { + final displayName = _userProfile.value?.displayName; + if (sendController.value.text.contains(displayName ?? '') == true) { + return sendController.value.text.replaceAll( + "${displayName ?? ''}!", + presentationContact?.matrixId ?? '', + ); + } else { + return sendController.value.text; + } + } + } + Future sendText({ OnRoomCreatedFailed onCreateRoomFailed, }) async { @@ -175,11 +197,12 @@ class DraftChatController extends State selection: const TextSelection.collapsed(offset: 0), ); inputFocus.unfocus(); + final textEvent = await _triggerTagGreetingMessage(); isSendingNotifier.value = true; _createRoom( onRoomCreatedSuccess: (room) { room.sendTextEvent( - sendController.text, + textEvent, ); }, onRoomCreatedFailed: onCreateRoomFailed, @@ -206,12 +229,11 @@ class DraftChatController extends State final room = Matrix.of(context).client.getRoomById(success.roomId); if (room != null) { onRoomCreatedSuccess?.call(room); - final user = await _getProfile(); context.go( '/rooms/${room.id}/', extra: ChatRouterInputArgument( type: ChatRouterInputArgumentType.draft, - data: user.displayName ?? + data: _userProfile.value?.displayName ?? presentationContact?.displayName ?? room.name, ), @@ -361,26 +383,28 @@ class DraftChatController extends State } Future handleDraftAction(BuildContext context) async { - Profile? profile; - try { - profile = await _getProfile(); - } catch (e) { - Logs().e('Error getting profile: $e'); - } inputFocus.requestFocus(); sendController.value = TextEditingValue( text: L10n.of(context)!.draftChatHookPhrase( - profile?.displayName ?? presentationContact?.displayName ?? '', + _userProfile.value?.displayName ?? + presentationContact?.displayName ?? + '', ), ); onInputBarChanged(sendController.text); } - Future _getProfile() async { - return await Matrix.of(context).client.getProfileFromUserId( - presentationContact!.matrixId!, - getFromRooms: false, - ); + Future _getProfile() async { + try { + final profile = await Matrix.of(context).client.getProfileFromUserId( + presentationContact!.matrixId!, + getFromRooms: false, + ); + _userProfile.value = profile; + } catch (e) { + Logs().e('Error _getProfile profile: $e'); + _userProfile.value = null; + } } void hideKeyboardChatScreen() { diff --git a/lib/pages/chat_draft/draft_chat_input_row.dart b/lib/pages/chat_draft/draft_chat_input_row.dart index 6134473c9e..c88fc16819 100644 --- a/lib/pages/chat_draft/draft_chat_input_row.dart +++ b/lib/pages/chat_draft/draft_chat_input_row.dart @@ -110,6 +110,7 @@ class DraftChatInputRow extends StatelessWidget { ), onChanged: onInputBarChanged, focusSuggestionController: focusSuggestionController, + isDraftChat: true, ); } }