From d1d016fffde95e01634adc33344c56b447e97201 Mon Sep 17 00:00:00 2001 From: MinhDV Date: Thu, 2 Nov 2023 16:45:35 +0700 Subject: [PATCH] fix: Chat room lagging by highlight keyword --- lib/pages/chat/chat.dart | 1 - lib/pages/chat/events/html_message.dart | 8 +- lib/pages/chat/events/message_content.dart | 109 +++++++----------- .../chat/events/message_download_content.dart | 22 +--- 4 files changed, 46 insertions(+), 94 deletions(-) diff --git a/lib/pages/chat/chat.dart b/lib/pages/chat/chat.dart index 3db494ea73..a8d51c33ae 100644 --- a/lib/pages/chat/chat.dart +++ b/lib/pages/chat/chat.dart @@ -145,7 +145,6 @@ class ChatController extends State final ValueNotifier showEmojiPickerNotifier = ValueNotifier(false); FocusNode inputFocus = FocusNode(); FocusNode keyboardFocus = FocusNode(); - final highlightKeywordNotifier = ValueNotifier(''); Timer? typingCoolDown; Timer? typingTimeout; diff --git a/lib/pages/chat/events/html_message.dart b/lib/pages/chat/events/html_message.dart index 60f924d0d7..ec0dd81c61 100644 --- a/lib/pages/chat/events/html_message.dart +++ b/lib/pages/chat/events/html_message.dart @@ -3,7 +3,6 @@ import 'package:fluffychat/config/setting_keys.dart'; import 'package:fluffychat/pages/chat/chat.dart'; import 'package:fluffychat/pages/image_viewer/image_viewer.dart'; import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart'; -import 'package:fluffychat/utils/string_extension.dart'; import 'package:fluffychat/utils/url_launcher.dart'; import 'package:fluffychat/widgets/pill.dart'; import 'package:flutter/material.dart'; @@ -16,7 +15,6 @@ import 'package:fluffychat/widgets/matrix.dart'; class HtmlMessage extends StatelessWidget { final String html; - final String? highlightText; final int? maxLines; final Room room; final Event event; @@ -29,7 +27,6 @@ class HtmlMessage extends StatelessWidget { const HtmlMessage({ Key? key, required this.html, - this.highlightText, this.chatController, required this.event, this.maxLines, @@ -48,7 +45,7 @@ class HtmlMessage extends StatelessWidget { // We do *not* do this in an AST and just with simple regex here, as riot-web tends to create // miss-matching tags, and this way we actually correctly identify what we want to strip and, well, // strip it. - var renderHtml = html.replaceAll( + final renderHtml = html.replaceAll( RegExp( '.*', caseSensitive: false, @@ -57,9 +54,6 @@ class HtmlMessage extends StatelessWidget { ), '', ); - if (highlightText != null) { - renderHtml = renderHtml.htmlHighlightText(highlightText); - } // there is no need to pre-validate the html, as we validate it while rendering diff --git a/lib/pages/chat/events/message_content.dart b/lib/pages/chat/events/message_content.dart index 9e43ee1055..e04f80898e 100644 --- a/lib/pages/chat/events/message_content.dart +++ b/lib/pages/chat/events/message_content.dart @@ -19,7 +19,6 @@ import 'package:fluffychat/utils/url_launcher.dart'; import 'package:fluffychat/widgets/twake_components/twake_preview_link/twake_link_preview.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; -import 'package:flutter_matrix_html/color_extension.dart'; import 'package:matrix/matrix.dart' hide Visibility; import 'audio_player.dart'; @@ -111,7 +110,6 @@ class MessageContent extends StatelessWidget with PlayVideoActionMixin { MessageDownloadContent( event, onFileTapped: controller.onFileTapped, - highlightNotifier: controller.highlightKeywordNotifier, ), Padding( padding: const EdgeInsets.symmetric(vertical: 4), @@ -136,36 +134,30 @@ class MessageContent extends StatelessWidget with PlayVideoActionMixin { event.numberEmotes <= 10; return Padding( padding: const EdgeInsets.only(left: 8.0, right: 8.0), - child: ValueListenableBuilder( - valueListenable: controller.highlightKeywordNotifier, - builder: (context, highlightText, child) { - return HtmlMessage( - event: event, - html: html, - highlightText: highlightText, - defaultTextStyle: - Theme.of(context).textTheme.bodyLarge?.copyWith( - // color: textColor, - fontSize: bigEmotes ? fontSize * 3 : fontSize, - ), - linkStyle: TextStyle( - color: Theme.of(context).colorScheme.secondary, - fontSize: bigEmotes ? fontSize * 3 : fontSize, - decoration: TextDecoration.underline, - decorationColor: textColor.withAlpha(150), - ), - room: event.room, - emoteSize: bigEmotes ? fontSize * 3 : fontSize * 1.5, - bottomWidgetSpan: Visibility( - visible: false, - maintainSize: true, - maintainAnimation: true, - maintainState: true, - child: endOfBubbleWidget, - ), - chatController: controller, - ); - }, + child: HtmlMessage( + event: event, + html: html, + defaultTextStyle: + Theme.of(context).textTheme.bodyLarge?.copyWith( + // color: textColor, + fontSize: bigEmotes ? fontSize * 3 : fontSize, + ), + linkStyle: TextStyle( + color: Theme.of(context).colorScheme.secondary, + fontSize: bigEmotes ? fontSize * 3 : fontSize, + decoration: TextDecoration.underline, + decorationColor: textColor.withAlpha(150), + ), + room: event.room, + emoteSize: bigEmotes ? fontSize * 3 : fontSize * 1.5, + bottomWidgetSpan: Visibility( + visible: false, + maintainSize: true, + maintainAnimation: true, + maintainState: true, + child: endOfBubbleWidget, + ), + chatController: controller, ), ); } @@ -227,44 +219,25 @@ class MessageContent extends StatelessWidget with PlayVideoActionMixin { MatrixLocals(L10n.of(context)!), hideReply: true, ); - - return ValueListenableBuilder( - valueListenable: controller.highlightKeywordNotifier, - builder: (context, highlightText, child) { - return TwakeLinkPreview( - text: text, - textStyle: - Theme.of(context).textTheme.bodyLarge?.copyWith( - color: Theme.of(context).colorScheme.onSurface, - ), - linkStyle: - Theme.of(context).textTheme.bodyLarge?.copyWith( - color: Theme.of(context).colorScheme.secondary, - ), - childWidget: Visibility( - visible: false, - maintainSize: true, - maintainAnimation: true, - maintainState: true, - child: endOfBubbleWidget, + return TwakeLinkPreview( + text: text, + textStyle: Theme.of(context).textTheme.bodyLarge?.copyWith( + color: Theme.of(context).colorScheme.onSurface, ), - uri: Uri.parse(text.getFirstValidUrl() ?? ''), - ownMessage: ownMessage, - onLinkTap: (url) => - UrlLauncher(context, url.toString()).launchUrl(), - textSpanBuilder: (text, textStyle, recognizer) => - TextSpan( - children: text?.buildHighlightTextSpans( - highlightText, - style: textStyle, - highlightStyle: textStyle?.copyWith( - backgroundColor: CssColor.fromCss('gold'), - ), - recognizer: recognizer, - ), + linkStyle: Theme.of(context).textTheme.bodyLarge?.copyWith( + color: Theme.of(context).colorScheme.secondary, ), - ); - }, + childWidget: Visibility( + visible: false, + maintainSize: true, + maintainAnimation: true, + maintainState: true, + child: endOfBubbleWidget, + ), + uri: Uri.parse(text.getFirstValidUrl() ?? ''), + ownMessage: ownMessage, + onLinkTap: (url) => + UrlLauncher(context, url.toString()).launchUrl(), ); }, ); diff --git a/lib/pages/chat/events/message_download_content.dart b/lib/pages/chat/events/message_download_content.dart index e080d3cd45..668330c7b0 100644 --- a/lib/pages/chat/events/message_download_content.dart +++ b/lib/pages/chat/events/message_download_content.dart @@ -12,14 +12,12 @@ class MessageDownloadContent extends StatelessWidget { final Event event; final void Function(Event event)? onFileTapped; - final ValueNotifier? highlightNotifier; final String? highlightText; const MessageDownloadContent( this.event, { Key? key, this.onFileTapped, - this.highlightNotifier, this.highlightText, }) : super(key: key); @@ -62,22 +60,10 @@ class MessageDownloadContent extends StatelessWidget { crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.max, children: [ - if (highlightNotifier != null) ...[ - ValueListenableBuilder( - valueListenable: highlightNotifier!, - builder: (context, highlightText, child) { - return _FileNameText( - filename: filename, - highlightText: highlightText, - ); - }, - ), - ] else ...[ - _FileNameText( - filename: filename, - highlightText: highlightText, - ), - ], + _FileNameText( + filename: filename, + highlightText: highlightText, + ), Row( children: [ if (sizeString != null)