feat: Added url attachments

This commit is contained in:
Deven Joshi
2020-11-10 19:32:46 +05:30
parent 73f5b33489
commit 755dd511ed
+165 -29
View File
@@ -1,6 +1,7 @@
import 'dart:math'; import 'dart:math';
import 'dart:ui'; import 'dart:ui';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:emojis/emoji.dart'; import 'package:emojis/emoji.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
@@ -343,6 +344,83 @@ class _MessageWidgetState extends State<MessageWidget> {
); );
} }
Widget _buildUrlAttachment() {
var urlAttachment = widget.message.attachments
.firstWhere((element) => element.ogScrapeUrl != null);
var host = Uri.parse(urlAttachment.ogScrapeUrl).host;
var splitList = host.split('.');
var hostName = splitList.length == 3 ? splitList[1] : splitList[0];
var hostDisplayName =
_getWebsiteName(hostName.toLowerCase()) ?? hostName.capitalize();
return Column(
children: [
SizedBox(
height: 16.0,
),
Container(
margin: EdgeInsets.symmetric(horizontal: 8.0),
child: Stack(
clipBehavior: Clip.antiAlias,
children: [
CachedNetworkImage(imageUrl: urlAttachment.imageUrl),
Positioned(
left: 0.0,
bottom: 0.0,
child: Container(
child: Padding(
padding:
const EdgeInsets.only(top: 8.0, left: 8.0, right: 8.0),
child: Text(
hostDisplayName,
style: TextStyle(
fontWeight: FontWeight.w700,
color: Color(0xFF006CFF),
),
),
),
decoration: BoxDecoration(
borderRadius:
BorderRadius.only(topRight: Radius.circular(16.0)),
color: Color(0xFFE9F2FF),
),
),
),
],
),
clipBehavior: Clip.antiAlias,
decoration: BoxDecoration(borderRadius: BorderRadius.circular(8.0)),
),
Padding(
padding: widget.textPadding,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (urlAttachment.title != null)
Text(
urlAttachment.title,
maxLines: 1,
style: TextStyle(
fontWeight: FontWeight.w700,
fontSize: 12.0,
),
),
if (urlAttachment.text != null)
Text(
urlAttachment.text,
style: TextStyle(
fontWeight: FontWeight.w400,
fontSize: 12.0,
),
),
],
),
),
],
);
}
Padding _buildBottomRow(double leftPadding) { Padding _buildBottomRow(double leftPadding) {
return Padding( return Padding(
padding: EdgeInsets.only( padding: EdgeInsets.only(
@@ -525,7 +603,8 @@ class _MessageWidgetState extends State<MessageWidget> {
List<Widget> _parseAttachments(BuildContext context) { List<Widget> _parseAttachments(BuildContext context) {
final images = widget.message.attachments final images = widget.message.attachments
?.where((element) => element.type == 'image') ?.where((element) =>
element.type == 'image' && element.ogScrapeUrl == null)
?.toList() ?? ?.toList() ??
[]; [];
@@ -548,7 +627,9 @@ class _MessageWidgetState extends State<MessageWidget> {
]; ];
} }
return widget.message.attachments?.map((attachment) { return widget.message.attachments
?.where((element) => element.ogScrapeUrl == null)
?.map((attachment) {
final attachmentBuilder = widget.attachmentBuilders[attachment.type]; final attachmentBuilder = widget.attachmentBuilders[attachment.type];
if (attachmentBuilder == null) { if (attachmentBuilder == null) {
@@ -757,28 +838,35 @@ class _MessageWidgetState extends State<MessageWidget> {
Widget child = Transform( Widget child = Transform(
transform: Matrix4.rotationY(widget.reverse ? pi : 0), transform: Matrix4.rotationY(widget.reverse ? pi : 0),
alignment: Alignment.center, alignment: Alignment.center,
child: Padding( child: Column(
padding: widget.textPadding, children: [
child: Column( Padding(
crossAxisAlignment: CrossAxisAlignment.end, padding: widget.textPadding,
children: [ child: Column(
getFailedMessageWidget(context), crossAxisAlignment: CrossAxisAlignment.end,
widget.textBuilder != null children: [
? widget.textBuilder(context, widget.message) getFailedMessageWidget(context),
: MessageText( widget.textBuilder != null
onLinkTap: widget.onLinkTap, ? widget.textBuilder(context, widget.message)
message: widget.message, : MessageText(
onMentionTap: widget.onMentionTap, onLinkTap: widget.onLinkTap,
messageTheme: isOnlyEmoji message: widget.message,
? widget.messageTheme.copyWith( onMentionTap: widget.onMentionTap,
messageText: messageTheme: isOnlyEmoji
widget.messageTheme.messageText.copyWith( ? widget.messageTheme.copyWith(
fontSize: 40, messageText:
)) widget.messageTheme.messageText.copyWith(
: widget.messageTheme, fontSize: 40,
), ))
], : widget.messageTheme,
), ),
],
),
),
if (widget.message.attachments
.any((element) => element.ogScrapeUrl != null))
_buildUrlAttachment(),
],
), ),
); );
@@ -796,11 +884,18 @@ class _MessageWidgetState extends State<MessageWidget> {
} }
Color _getBackgroundColor() { Color _getBackgroundColor() {
return (widget.message.status == MessageSendingStatus.FAILED || if ((widget.message.status == MessageSendingStatus.FAILED ||
widget.message.status == MessageSendingStatus.FAILED_UPDATE || widget.message.status == MessageSendingStatus.FAILED_UPDATE ||
widget.message.status == MessageSendingStatus.FAILED_DELETE) widget.message.status == MessageSendingStatus.FAILED_DELETE)) {
? Color(0xffd0021B).withOpacity(.1) return Color(0xffd0021B).withOpacity(.1);
: widget.messageTheme.messageBackgroundColor; }
if (widget.message.attachments
.any((element) => element.ogScrapeUrl != null)) {
return Color(0xFFE9F2FF);
}
return widget.messageTheme.messageBackgroundColor;
} }
void retryMessage(BuildContext context) { void retryMessage(BuildContext context) {
@@ -825,4 +920,45 @@ class _MessageWidgetState extends State<MessageWidget> {
return; return;
} }
} }
String _getWebsiteName(String hostName) {
switch (hostName) {
case 'reddit':
return 'Reddit';
case 'youtube':
return 'Youtube';
case 'wikipedia':
return 'Wikipedia';
case 'twitter':
return 'Twitter';
case 'facebook':
return 'Facebook';
case 'amazon':
return 'Amazon';
case 'yelp':
return 'Yelp';
case 'imdb':
return 'IMDB';
case 'pinterest':
return 'Pinterest';
case 'tripadvisor':
return 'TripAdvisor';
case 'instagram':
return 'Instagram';
case 'walmart':
return 'Walmart';
case 'craigslist':
return 'Craigslist';
case 'ebay':
return 'eBay';
case 'linkedin':
return 'LinkedIn';
case 'google':
return 'Google';
case 'apple':
return 'Apple';
default:
return null;
}
}
} }