ref(ui): Use titleLink instead of ogScrapeUrl for handling link attachment.

Signed-off-by: xsahil03x <[email protected]>
This commit is contained in:
Sahil Kumar
2021-09-16 16:41:17 +05:30
committed by xsahil03x
parent e7fc5e4133
commit 6af4e07787
8 changed files with 49 additions and 57 deletions
@@ -19,42 +19,36 @@ class AttachmentTitle extends StatelessWidget {
final Attachment attachment; final Attachment attachment;
@override @override
Widget build(BuildContext context) => GestureDetector( Widget build(BuildContext context) {
onTap: () { final normalizedTitleLink = attachment.titleLink?.replaceFirst(
if (attachment.titleLink != null) { RegExp(r'https?://(www\.)?'),
launchURL(context, attachment.titleLink); '',
} );
}, return GestureDetector(
child: Padding( onTap: () {
padding: const EdgeInsets.all(8), final titleLink = attachment.titleLink;
child: Column( if (titleLink != null) launchURL(context, titleLink);
mainAxisSize: MainAxisSize.min, },
crossAxisAlignment: CrossAxisAlignment.stretch, child: Padding(
children: <Widget>[ padding: const EdgeInsets.all(8),
if (attachment.title != null) child: Column(
Text( mainAxisSize: MainAxisSize.min,
attachment.title!, crossAxisAlignment: CrossAxisAlignment.stretch,
overflow: TextOverflow.ellipsis, children: <Widget>[
style: messageTheme.messageTextStyle?.copyWith( if (attachment.title != null)
color: StreamChatTheme.of(context).colorTheme.accentPrimary, Text(
fontWeight: FontWeight.bold, attachment.title!,
), overflow: TextOverflow.ellipsis,
style: messageTheme.messageTextStyle?.copyWith(
color: StreamChatTheme.of(context).colorTheme.accentPrimary,
fontWeight: FontWeight.bold,
), ),
if (attachment.titleLink != null || ),
attachment.ogScrapeUrl != null) if (normalizedTitleLink != null)
Text( Text(normalizedTitleLink, style: messageTheme.messageTextStyle),
Uri.parse(attachment.titleLink ?? attachment.ogScrapeUrl!) ],
.authority
.split('.')
.reversed
.take(2)
.toList()
.reversed
.join('.'),
style: messageTheme.messageTextStyle,
),
],
),
), ),
); ),
);
}
} }
@@ -258,7 +258,8 @@ class FileAttachment extends AttachmentWidget {
visualDensity: VisualDensity.compact, visualDensity: VisualDensity.compact,
splashRadius: 16, splashRadius: 16,
onPressed: () { onPressed: () {
launchURL(context, attachment.assetUrl); final assetUrl = attachment.assetUrl;
if (assetUrl != null) launchURL(context, assetUrl);
}, },
); );
} }
@@ -1575,7 +1575,7 @@ class MessageInputState extends State<MessageInput> {
Widget _buildReplyToMessage() { Widget _buildReplyToMessage() {
if (!_hasQuotedMessage) return const Offstage(); if (!_hasQuotedMessage) return const Offstage();
final containsUrl = widget.quotedMessage!.attachments final containsUrl = widget.quotedMessage!.attachments
.any((element) => element.ogScrapeUrl != null) == .any((element) => element.titleLink != null) ==
true; true;
return QuotedMessageWidget( return QuotedMessageWidget(
reverse: true, reverse: true,
@@ -991,7 +991,7 @@ class _MessageListViewState extends State<MessageListView> {
final isOnlyEmoji = message.text?.isOnlyEmoji ?? false; final isOnlyEmoji = message.text?.isOnlyEmoji ?? false;
final hasUrlAttachment = final hasUrlAttachment =
message.attachments.any((it) => it.ogScrapeUrl != null) == true; message.attachments.any((it) => it.titleLink != null) == true;
final borderSide = final borderSide =
isOnlyEmoji || hasUrlAttachment || (isMyMessage && !hasFileAttachment) isOnlyEmoji || hasUrlAttachment || (isMyMessage && !hasFileAttachment)
@@ -566,12 +566,11 @@ class _MessageWidgetState extends State<MessageWidget>
bool get isOnlyEmoji => widget.message.text?.isOnlyEmoji == true; bool get isOnlyEmoji => widget.message.text?.isOnlyEmoji == true;
bool get hasNonUrlAttachments => widget.message.attachments bool get hasNonUrlAttachments =>
.where((it) => it.ogScrapeUrl == null) widget.message.attachments.where((it) => it.titleLink == null).isNotEmpty;
.isNotEmpty;
bool get hasUrlAttachments => bool get hasUrlAttachments =>
widget.message.attachments.any((it) => it.ogScrapeUrl != null) == true; widget.message.attachments.any((it) => it.titleLink != null) == true;
bool get showBottomRow => bool get showBottomRow =>
showThreadReplyIndicator || showThreadReplyIndicator ||
@@ -975,9 +974,9 @@ class _MessageWidgetState extends State<MessageWidget>
Widget _buildUrlAttachment() { Widget _buildUrlAttachment() {
final urlAttachment = widget.message.attachments final urlAttachment = widget.message.attachments
.firstWhere((element) => element.ogScrapeUrl != null); .firstWhere((element) => element.titleLink != null);
final host = Uri.parse(urlAttachment.ogScrapeUrl!).host; final host = Uri.parse(urlAttachment.titleLink!).host;
final splitList = host.split('.'); final splitList = host.split('.');
final hostName = splitList.length == 3 ? splitList[1] : splitList[0]; final hostName = splitList.length == 3 ? splitList[1] : splitList[0];
final hostDisplayName = urlAttachment.authorName?.capitalize() ?? final hostDisplayName = urlAttachment.authorName?.capitalize() ??
@@ -1143,7 +1142,7 @@ class _MessageWidgetState extends State<MessageWidget>
final attachmentGroups = <String, List<Attachment>>{}; final attachmentGroups = <String, List<Attachment>>{};
widget.message.attachments widget.message.attachments
.where((element) => element.ogScrapeUrl == null && element.type != null) .where((element) => element.titleLink == null && element.type != null)
.forEach((e) { .forEach((e) {
if (attachmentGroups[e.type] == null) { if (attachmentGroups[e.type] == null) {
attachmentGroups[e.type!] = []; attachmentGroups[e.type!] = [];
@@ -97,8 +97,8 @@ class QuotedMessageWidget extends StatelessWidget {
bool get _hasAttachments => message.attachments.isNotEmpty == true; bool get _hasAttachments => message.attachments.isNotEmpty == true;
bool get _containsScrapeUrl => bool get _containsLinkAttachment =>
message.attachments.any((element) => element.ogScrapeUrl != null) == true; message.attachments.any((element) => element.titleLink != null) == true;
bool get _containsText => message.text?.isNotEmpty == true; bool get _containsText => message.text?.isNotEmpty == true;
@@ -198,9 +198,9 @@ class QuotedMessageWidget extends StatelessWidget {
Widget _parseAttachments(BuildContext context) { Widget _parseAttachments(BuildContext context) {
Widget child; Widget child;
Attachment attachment; Attachment attachment;
if (_containsScrapeUrl) { if (_containsLinkAttachment) {
attachment = message.attachments.firstWhere( attachment = message.attachments.firstWhere(
(element) => element.ogScrapeUrl != null, (element) => element.titleLink != null,
); );
child = _buildUrlAttachment(attachment); child = _buildUrlAttachment(attachment);
} else { } else {
@@ -280,7 +280,7 @@ class QuotedMessageWidget extends StatelessWidget {
}; };
Color? _getBackgroundColor(BuildContext context) { Color? _getBackgroundColor(BuildContext context) {
if (_containsScrapeUrl) { if (_containsLinkAttachment) {
return StreamChatTheme.of(context).colorTheme.linkBg; return StreamChatTheme.of(context).colorTheme.linkBg;
} }
return messageTheme.messageBackgroundColor; return messageTheme.messageBackgroundColor;
@@ -30,10 +30,8 @@ class UrlAttachment extends StatelessWidget {
final chatThemeData = StreamChatTheme.of(context); final chatThemeData = StreamChatTheme.of(context);
return GestureDetector( return GestureDetector(
onTap: () { onTap: () {
launchURL( final titleLink = urlAttachment.titleLink;
context, if (titleLink != null) launchURL(context, titleLink);
urlAttachment.ogScrapeUrl,
);
}, },
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch, crossAxisAlignment: CrossAxisAlignment.stretch,
@@ -7,8 +7,8 @@ import 'package:url_launcher/url_launcher.dart';
import 'package:stream_chat_flutter/src/extension.dart'; import 'package:stream_chat_flutter/src/extension.dart';
/// Launch URL /// Launch URL
Future<void> launchURL(BuildContext context, String? url) async { Future<void> launchURL(BuildContext context, String url) async {
if (url != null && await canLaunch(url)) { if (await canLaunch(url)) {
await launch(url); await launch(url);
} else { } else {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(