Skip to content

Commit

Permalink
TW-1183: Update message_content
Browse files Browse the repository at this point in the history
  • Loading branch information
hieutbui authored and hoangdat committed Mar 27, 2024
1 parent 0d3fe0b commit 3778c01
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 124 deletions.
44 changes: 44 additions & 0 deletions lib/pages/chat/events/formatted_text_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'package:fluffychat/pages/chat/events/html_message.dart';
import 'package:flutter/material.dart';
import 'package:matrix/matrix.dart' hide Visibility;

class FormattedTextWidget extends StatelessWidget {
final Event event;
final double fontSize;
final Widget endOfBubbleWidget;
final TextStyle? linkStyle;

const FormattedTextWidget({
super.key,
required this.event,
required this.fontSize,
required this.endOfBubbleWidget,
this.linkStyle,
});

@override
Widget build(BuildContext context) {
var html = event.formattedText;

if (event.messageType == MessageTypes.Emote) {
html = '* $html';
}
final bigEmotes =
event.onlyEmotes && event.numberEmotes > 0 && event.numberEmotes <= 10;

return HtmlMessage(
html: html,
defaultTextStyle: Theme.of(context).textTheme.bodyLarge,
linkStyle: linkStyle,
room: event.room,
emoteSize: bigEmotes ? fontSize * 3 : fontSize * 1.5,
bottomWidgetSpan: Visibility(
visible: false,
maintainSize: true,
maintainAnimation: true,
maintainState: true,
child: endOfBubbleWidget,
),
);
}
}
62 changes: 18 additions & 44 deletions lib/pages/chat/events/message_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:fluffychat/pages/chat/events/call_invite_content.dart';
import 'package:fluffychat/pages/chat/events/encrypted_content.dart';
import 'package:fluffychat/pages/chat/events/event_video_player.dart';
import 'package:fluffychat/pages/chat/events/message_content_style.dart';
import 'package:fluffychat/pages/chat/events/formatted_text_widget.dart';
import 'package:fluffychat/pages/chat/events/redacted_content.dart';
import 'package:fluffychat/pages/chat/events/sending_image_info_widget.dart';
import 'package:fluffychat/pages/chat/events/sending_video_widget.dart';
Expand All @@ -23,7 +24,6 @@ import 'package:matrix/matrix.dart' hide Visibility;

import 'audio_player.dart';
import 'cute_events.dart';
import 'html_message.dart';
import 'image_bubble.dart';
import 'map_bubble.dart';
import 'message_download_content.dart';
Expand Down Expand Up @@ -132,33 +132,14 @@ class MessageContent extends StatelessWidget
!event.redacted &&
event.isRichMessage &&
containedLink.isEmpty) {
var html = event.formattedText;

if (event.messageType == MessageTypes.Emote) {
html = '* $html';
}
final bigEmotes = event.onlyEmotes &&
event.numberEmotes > 0 &&
event.numberEmotes <= 10;
return Padding(
padding: MessageContentStyle.emojiPadding,
child: HtmlMessage(
html: html,
defaultTextStyle: Theme.of(context).textTheme.bodyLarge,
linkStyle: TextStyle(
color: Theme.of(context).colorScheme.secondary,
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,
),
child: FormattedTextWidget(
event: event,
linkStyle:
MessageContentStyle.linkStyleMessageContent(context),
fontSize: fontSize,
endOfBubbleWidget: endOfBubbleWidget,
),
);
}
Expand Down Expand Up @@ -215,31 +196,24 @@ class MessageContent extends StatelessWidget
hideReply: true,
),
builder: (context, snapshot) {
final text = snapshot.data ??
final localizedBody = snapshot.data ??
event.calcLocalizedBodyFallback(
MatrixLocals(L10n.of(context)!),
hideReply: true,
);
return TwakeLinkPreview(
key: ValueKey('TwakeLinkPreview%${event.eventId}%'),
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,
),
uri: Uri.parse(text.getFirstValidUrl() ?? ''),
event: event,
localizedBody: localizedBody,
ownMessage: ownMessage,
onLinkTap: (url) =>
UrlLauncher(context, url: url.toString()).launchUrl(),
endOfBubbleWidget: endOfBubbleWidget,
fontSize: fontSize,
linkStyle:
MessageContentStyle.linkStyleMessageContent(context),
richTextStyle:
Theme.of(context).textTheme.bodyLarge?.copyWith(
color: Theme.of(context).colorScheme.onSurface,
),
);
},
);
Expand Down
5 changes: 5 additions & 0 deletions lib/pages/chat/events/message_content_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ class MessageContentStyle {
left: 8.0,
right: 8.0,
);

static TextStyle? linkStyleMessageContent(BuildContext context) =>
Theme.of(context).textTheme.bodyLarge?.copyWith(
color: Theme.of(context).colorScheme.secondary,
);
}
51 changes: 0 additions & 51 deletions lib/pages/chat/events/message_text_context.dart

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import 'package:fluffychat/domain/app_state/preview_url/get_preview_url_success.dart';
import 'package:fluffychat/pages/chat/events/formatted_text_widget.dart';
import 'package:fluffychat/presentation/extensions/media/url_preview_extension.dart';
import 'package:fluffychat/utils/string_extension.dart';
import 'package:fluffychat/utils/url_launcher.dart';
import 'package:fluffychat/widgets/clean_rich_text.dart';
import 'package:fluffychat/widgets/mixins/get_preview_url_mixin.dart';
import 'package:fluffychat/widgets/twake_components/twake_preview_link/twake_link_preview_item.dart';
import 'package:fluffychat/widgets/twake_components/twake_preview_link/twake_link_preview_item_style.dart';
import 'package:fluffychat/widgets/twake_components/twake_preview_link/twake_link_view.dart';
import 'package:flutter/material.dart';
import 'package:linagora_design_flutter/linagora_design_flutter.dart';
import 'package:matrix_link_text/link_text.dart';
import 'package:matrix/matrix.dart' hide Visibility;
import 'package:skeletonizer/skeletonizer.dart';

class TwakeLinkPreview extends StatefulWidget {
final Uri uri;
final int? preferredPointInTime;
final String text;
final Widget messageContentWidget;
final Event event;
final String localizedBody;
final bool ownMessage;
final Widget endOfBubbleWidget;
final double fontSize;
final TextStyle? linkStyle;
final TextStyle? richTextStyle;

const TwakeLinkPreview({
super.key,
required this.uri,
this.preferredPointInTime,
required this.text,
required this.messageContentWidget,
required this.event,
required this.localizedBody,
required this.endOfBubbleWidget,
required this.ownMessage,
required this.fontSize,
this.linkStyle,
this.richTextStyle,
});

@override
Expand All @@ -33,7 +39,9 @@ class TwakeLinkPreview extends StatefulWidget {

class TwakeLinkPreviewController extends State<TwakeLinkPreview>
with GetPreviewUrlMixin {
String? get firstValidUrl => widget.text.getFirstValidUrl();
String? get firstValidUrl => widget.localizedBody.getFirstValidUrl();

Uri get uri => Uri.parse(firstValidUrl ?? '');

static const twakeLinkViewKey = ValueKey('TwakeLinkPreviewKey');

Expand All @@ -45,7 +53,7 @@ class TwakeLinkPreviewController extends State<TwakeLinkPreview>
@override
void initState() {
if (firstValidUrl != null) {
getPreviewUrl(uri: widget.uri);
getPreviewUrl(uri: uri);
}
super.initState();
}
Expand All @@ -54,9 +62,29 @@ class TwakeLinkPreviewController extends State<TwakeLinkPreview>
Widget build(BuildContext context) {
return TwakeLinkView(
key: twakeLinkViewKey,
text: widget.text,
firstValidUrl: firstValidUrl,
messageContentWidget: widget.messageContentWidget,
body: widget.event.formattedText.isNotEmpty
? FormattedTextWidget(
event: widget.event,
linkStyle: widget.linkStyle,
fontSize: widget.fontSize,
endOfBubbleWidget: widget.endOfBubbleWidget,
)
: TwakeCleanRichText(
text: widget.localizedBody,
childWidget: Visibility(
visible: false,
maintainSize: true,
maintainAnimation: true,
maintainState: true,
child: widget.endOfBubbleWidget,
),
textStyle: widget.richTextStyle,
linkStyle: widget.linkStyle,
textAlign: TextAlign.start,
onLinkTap: (url) =>
UrlLauncher(context, url: url.toString()).launchUrl(),
),
previewItemWidget: ValueListenableBuilder(
valueListenable: getPreviewUrlStateNotifier,
builder: (context, state, child) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,42 @@ import 'package:fluffychat/widgets/twake_components/twake_preview_link/twake_lin
import 'package:flutter/material.dart';

class TwakeLinkView extends StatelessWidget {
final String text;
final Widget messageContentWidget;
final Widget body;
final Widget previewItemWidget;
final String? firstValidUrl;

const TwakeLinkView({
Key? key,
required this.text,
required this.messageContentWidget,
required this.body,
required this.previewItemWidget,
this.firstValidUrl,
}) : super(key: key);

@override
Widget build(BuildContext context) {
if (firstValidUrl == null) {
return _buildWidgetNoPreview(context);
return _buildMessageBody();
}

return _buildWidgetWithPreview(context, firstValidUrl!);
return _buildMessageWithPreview(context);
}

Widget _buildWidgetWithPreview(BuildContext context, String url) {
Widget _buildMessageWithPreview(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
previewItemWidget,
const SizedBox(height: 2),
Padding(
padding: TwakeLinkViewStyle.paddingWidgetNoPreview,
child: messageContentWidget,
),
const SizedBox(height: TwakeLinkViewStyle.previewToBodySpacing),
_buildMessageBody(),
],
);
}

Widget _buildWidgetNoPreview(BuildContext context) {
Widget _buildMessageBody() {
return Padding(
padding: TwakeLinkViewStyle.paddingWidgetNoPreview,
child: messageContentWidget,
padding: TwakeLinkViewStyle.paddingMessageBody,
child: body,
);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';

class TwakeLinkViewStyle {
static const EdgeInsetsDirectional paddingWidgetNoPreview =
static const EdgeInsetsDirectional paddingMessageBody =
EdgeInsetsDirectional.only(start: 8.0);

static const EdgeInsetsDirectional paddingCleanRichText =
Expand All @@ -10,4 +10,6 @@ class TwakeLinkViewStyle {
end: 8.0,
top: 8.0,
);

static const double previewToBodySpacing = 2.0;
}

0 comments on commit 3778c01

Please sign in to comment.