Skip to content

Commit

Permalink
fix: Chat room lagging by highlight keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
drminh2807 authored and hoangdat committed Nov 3, 2023
1 parent a29744d commit d1d016f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 94 deletions.
1 change: 0 additions & 1 deletion lib/pages/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ class ChatController extends State<Chat>
final ValueNotifier<bool> showEmojiPickerNotifier = ValueNotifier(false);
FocusNode inputFocus = FocusNode();
FocusNode keyboardFocus = FocusNode();
final highlightKeywordNotifier = ValueNotifier<String>('');

Timer? typingCoolDown;
Timer? typingTimeout;
Expand Down
8 changes: 1 addition & 7 deletions lib/pages/chat/events/html_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand All @@ -29,7 +27,6 @@ class HtmlMessage extends StatelessWidget {
const HtmlMessage({
Key? key,
required this.html,
this.highlightText,
this.chatController,
required this.event,
this.maxLines,
Expand All @@ -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(
'<mx-reply>.*</mx-reply>',
caseSensitive: false,
Expand All @@ -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

Expand Down
109 changes: 41 additions & 68 deletions lib/pages/chat/events/message_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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),
Expand All @@ -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,
),
);
}
Expand Down Expand Up @@ -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(),
);
},
);
Expand Down
22 changes: 4 additions & 18 deletions lib/pages/chat/events/message_download_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ class MessageDownloadContent extends StatelessWidget {
final Event event;
final void Function(Event event)? onFileTapped;

final ValueNotifier<String>? highlightNotifier;
final String? highlightText;

const MessageDownloadContent(
this.event, {
Key? key,
this.onFileTapped,
this.highlightNotifier,
this.highlightText,
}) : super(key: key);

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d1d016f

Please sign in to comment.