@@ -31,32 +31,15 @@ class StreamMessagePreviewText extends StatelessWidget {
3131 final translatedMessage = message.translate (translationLanguage);
3232 final previewMessage = translatedMessage.replaceMentions (linkify: false );
3333
34- final previewText = _getPreviewText (context, previewMessage, currentUser);
35-
36- final mentionedUsers = message.mentionedUsers;
37- final mentionedUsersRegex = RegExp (
38- mentionedUsers.map ((it) => '@${it .name }' ).join ('|' ),
39- );
40-
41- final previewTextSpan = TextSpan (
42- children: [
43- ...previewText.splitByRegExp (mentionedUsersRegex).map (
44- (text) {
45- // Bold the text if it is a mention user.
46- if (mentionedUsers.any ((it) => '@${it .name }' == text)) {
47- return TextSpan (
48- text: text,
49- style: textStyle? .copyWith (fontWeight: FontWeight .bold),
50- );
51- }
52-
53- return TextSpan (
54- text: text,
55- style: textStyle,
56- );
57- },
58- )
59- ],
34+ final config = StreamChatConfiguration .of (context);
35+ final formatter = config.messagePreviewFormatter;
36+
37+ final previewTextSpan = formatter.formatMessage (
38+ context,
39+ previewMessage,
40+ channel: channel,
41+ currentUser: currentUser,
42+ textStyle: textStyle,
6043 );
6144
6245 return Text .rich (
@@ -66,144 +49,4 @@ class StreamMessagePreviewText extends StatelessWidget {
6649 textAlign: TextAlign .start,
6750 );
6851 }
69-
70- String _getPreviewText (
71- BuildContext context,
72- Message message,
73- User currentUser,
74- ) {
75- final translations = context.translations;
76-
77- if (message.isDeleted) {
78- return translations.messageDeletedLabel;
79- }
80-
81- if (message.isSystem) {
82- return message.text ?? translations.systemMessageLabel;
83- }
84-
85- if (message.poll case final poll? ) {
86- return _pollPreviewText (context, poll, currentUser);
87- }
88-
89- final previewText = _previewMessageContextText (context, message);
90- if (previewText == null ) return translations.emptyMessagePreviewText;
91-
92- if (channel case final channel? ) {
93- if (message.user? .id == currentUser.id) {
94- return '${translations .youText }: $previewText ' ;
95- }
96-
97- if (channel.memberCount > 2 ) {
98- return '${message .user ?.name }: $previewText ' ;
99- }
100- }
101-
102- return previewText;
103- }
104-
105- String _pollPreviewText (
106- BuildContext context,
107- Poll poll,
108- User currentUser,
109- ) {
110- final translations = context.translations;
111-
112- // If the poll already contains some votes, we will preview the latest voter
113- // and the poll name
114- if (poll.latestVotes.firstOrNull? .user case final latestVoter? ) {
115- if (latestVoter.id == currentUser.id) {
116- final youVoted = translations.pollYouVotedText;
117- return '📊 $youVoted : "${poll .name }"' ;
118- }
119-
120- final someoneVoted = translations.pollSomeoneVotedText (latestVoter.name);
121- return '📊 $someoneVoted : "${poll .name }"' ;
122- }
123-
124- // Otherwise, we will show the creator of the poll and the poll name
125- if (poll.createdBy case final creator? ) {
126- if (creator.id == currentUser.id) {
127- final youCreated = translations.pollYouCreatedText;
128- return '📊 $youCreated : "${poll .name }"' ;
129- }
130-
131- final someoneCreated = translations.pollSomeoneCreatedText (creator.name);
132- return '📊 $someoneCreated : "${poll .name }"' ;
133- }
134-
135- // Otherwise, we will show the poll name if it exists.
136- if (poll.name.trim () case final pollName when pollName.isNotEmpty) {
137- return '📊 $pollName ' ;
138- }
139-
140- // If nothing else, we will show the default poll emoji.
141- return '📊' ;
142- }
143-
144- String ? _previewMessageContextText (
145- BuildContext context,
146- Message message,
147- ) {
148- final translations = context.translations;
149-
150- final messageText = switch (message.text) {
151- final messageText? when messageText.isNotEmpty => messageText,
152- _ => null ,
153- };
154-
155- // If the message contains some attachments, we will show the first one
156- // and the text if it exists.
157- if (message.attachments.firstOrNull case final attachment? ) {
158- final attachmentIcon = switch (attachment.type) {
159- AttachmentType .audio => '🎧' ,
160- AttachmentType .file => '📄' ,
161- AttachmentType .image => '📷' ,
162- AttachmentType .video => '📹' ,
163- AttachmentType .giphy => '/giphy' ,
164- AttachmentType .voiceRecording => '🎤' ,
165- _ => null ,
166- };
167-
168- final attachmentTitle = switch (attachment.type) {
169- AttachmentType .audio => messageText ?? translations.audioAttachmentText,
170- AttachmentType .file => attachment.title ?? messageText,
171- AttachmentType .image => messageText ?? translations.imageAttachmentText,
172- AttachmentType .video => messageText ?? translations.videoAttachmentText,
173- AttachmentType .giphy => messageText,
174- AttachmentType .voiceRecording => translations.voiceRecordingText,
175- _ => null ,
176- };
177-
178- if (attachmentIcon != null || attachmentTitle != null ) {
179- return [attachmentIcon, attachmentTitle].nonNulls.join (' ' );
180- }
181- }
182-
183- return messageText;
184- }
185- }
186-
187- extension on String {
188- List <String > splitByRegExp (RegExp regex) {
189- // If the pattern is empty, return the whole string
190- if (regex.pattern.isEmpty) return [this ];
191-
192- final result = < String > [];
193- var start = 0 ;
194-
195- for (final match in regex.allMatches (this )) {
196- if (match.start > start) {
197- result.add (substring (start, match.start));
198- }
199- result.add (match.group (0 )! );
200- start = match.end;
201- }
202-
203- if (start < length) {
204- result.add (substring (start));
205- }
206-
207- return result;
208- }
20952}
0 commit comments