Skip to content

Commit

Permalink
TW-1777: Changing greeting message: include tag in greeting message (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nqhhdev authored Jun 18, 2024
1 parent 68de53f commit f6fbbc0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
3 changes: 2 additions & 1 deletion lib/pages/chat/events/html_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lib/pages/chat/input_bar/input_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class InputBar extends StatefulWidget {
final ValueKey? typeAheadKey;
final ValueNotifier<bool>? showEmojiPickerNotifier;
final SuggestionsController<Map<String, String?>>? suggestionsController;
final bool isDraftChat;

const InputBar({
this.room,
Expand All @@ -60,6 +61,7 @@ class InputBar extends StatefulWidget {
this.rawKeyboardFocusNode,
this.suggestionsController,
this.showEmojiPickerNotifier,
this.isDraftChat = false,
super.key,
});

Expand Down Expand Up @@ -261,7 +263,7 @@ class _InputBarState extends State<InputBar> with PasteImageMixin {
}

void insertSuggestion(Map<String, String?> 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 = '';
Expand Down
54 changes: 39 additions & 15 deletions lib/pages/chat_draft/draft_chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ class DraftChatController extends State<DraftChat>

ValueNotifier<bool> showEmojiPickerNotifier = ValueNotifier(false);

final ValueNotifier<Profile?> _userProfile = ValueNotifier(null);

EmojiPickerType emojiPickerType = EmojiPickerType.keyboard;

final isSendingNotifier = ValueNotifier(false);
Expand Down Expand Up @@ -146,6 +148,9 @@ class DraftChatController extends State<DraftChat>
void initState() {
scrollController.addListener(_updateScrollController);
keyboardVisibilityController.onChange.listen(_keyboardListener);
WidgetsBinding.instance.addPostFrameCallback((_) {
_getProfile();
});
super.initState();
}

Expand All @@ -157,6 +162,7 @@ class DraftChatController extends State<DraftChat>
focusSuggestionController.dispose();
inputText.dispose();
showEmojiPickerNotifier.dispose();
_userProfile.dispose();
super.dispose();
}

Expand All @@ -166,6 +172,22 @@ class DraftChatController extends State<DraftChat>
Matrix.of(context).setActiveClient(c);
});

Future<String> _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<void> sendText({
OnRoomCreatedFailed onCreateRoomFailed,
}) async {
Expand All @@ -175,11 +197,12 @@ class DraftChatController extends State<DraftChat>
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,
Expand All @@ -206,12 +229,11 @@ class DraftChatController extends State<DraftChat>
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,
),
Expand Down Expand Up @@ -361,26 +383,28 @@ class DraftChatController extends State<DraftChat>
}

Future<void> 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<Profile> _getProfile() async {
return await Matrix.of(context).client.getProfileFromUserId(
presentationContact!.matrixId!,
getFromRooms: false,
);
Future<void> _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() {
Expand Down
1 change: 1 addition & 0 deletions lib/pages/chat_draft/draft_chat_input_row.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class DraftChatInputRow extends StatelessWidget {
),
onChanged: onInputBarChanged,
focusSuggestionController: focusSuggestionController,
isDraftChat: true,
);
}
}

0 comments on commit f6fbbc0

Please sign in to comment.