import 'package:cwtch/controllers/open_link_modal.dart'; import 'package:cwtch/models/contact.dart'; import 'package:cwtch/models/message.dart'; import 'package:cwtch/models/profile.dart'; import 'package:cwtch/third_party/linkify/flutter_linkify.dart'; import 'package:cwtch/widgets/malformedbubble.dart'; import 'package:cwtch/widgets/messageloadingbubble.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import '../settings.dart'; import 'messagebubbledecorations.dart'; class QuotedMessageBubble extends StatefulWidget { final Future quotedMessage; final String body; QuotedMessageBubble(this.body, this.quotedMessage); @override QuotedMessageBubbleState createState() => QuotedMessageBubbleState(); } class QuotedMessageBubbleState extends State { FocusNode _focus = FocusNode(); @override Widget build(BuildContext context) { var fromMe = Provider.of(context).senderHandle == Provider.of(context).onion; var borderRadiousEh = 15.0; DateTime messageDate = Provider.of(context).timestamp; // If the sender is not us, then we want to give them a nickname... var senderDisplayStr = ""; if (!fromMe) { ContactInfoState? contact = Provider.of(context).contactList.findContact(Provider.of(context).senderHandle); if (contact != null) { senderDisplayStr = contact.nickname; } else { senderDisplayStr = Provider.of(context).senderHandle; } } var wdgSender = SelectableText(senderDisplayStr, style: TextStyle(fontSize: 9.0, color: fromMe ? Provider.of(context).theme.messageFromMeTextColor : Provider.of(context).theme.messageFromOtherTextColor)); var showClickableLinks = Provider.of(context).isExperimentEnabled(ClickableLinksExperiment); var formatMessages = Provider.of(context).isExperimentEnabled(FormattingExperiment); var wdgMessage = SelectableLinkify( text: widget.body + '\u202F', // TODO: onOpen breaks the "selectable" functionality. Maybe something to do with gesture handler? options: LinkifyOptions(messageFormatting: formatMessages, parseLinks: showClickableLinks, looseUrl: true, defaultToHttps: true), linkifiers: [UrlLinkifier()], onOpen: showClickableLinks ? (link) { modalOpenLink(context, link); } : null, //key: Key(myKey), focusNode: _focus, style: TextStyle( color: fromMe ? Provider.of(context).theme.messageFromMeTextColor : Provider.of(context).theme.messageFromOtherTextColor, ), linkStyle: TextStyle(color: fromMe ? Provider.of(context).theme.messageFromMeTextColor : Provider.of(context).theme.messageFromOtherTextColor), codeStyle: TextStyle( // note: these colors are flipped color: fromMe ? Provider.of(context).theme.messageFromOtherTextColor : Provider.of(context).theme.messageFromMeTextColor, backgroundColor: fromMe ? Provider.of(context).theme.messageFromOtherBackgroundColor : Provider.of(context).theme.messageFromMeBackgroundColor), textAlign: TextAlign.left, textWidthBasis: TextWidthBasis.longestLine, ); var wdgQuote = FutureBuilder( future: widget.quotedMessage, builder: (context, snapshot) { if (snapshot.hasData) { try { var qMessage = (snapshot.data! as Message); // If the sender is not us, then we want to give them a nickname... String qMessageSender; // if we sent the quoted message then display our nickname if (qMessage.getMetadata().senderHandle == Provider.of(context).onion) { qMessageSender = Provider.of(context).nickname; } else { // default to handle qMessageSender = qMessage.getMetadata().senderHandle; // if we have the handle as a contact then replace with the nickname... ContactInfoState? contact = Provider.of(context).contactList.findContact(qMessageSender); if (contact != null) { qMessageSender = contact.nickname; } } var qTextColor = fromMe ? Provider.of(context).theme.messageFromOtherTextColor : Provider.of(context).theme.messageFromMeTextColor; var wdgReplyingTo = SelectableText( AppLocalizations.of(context)!.replyingTo.replaceAll("%1", qMessageSender), style: TextStyle(fontSize: 10, color: qTextColor.withOpacity(0.8)), ); // Swap the background color for quoted tweets.. return MouseRegion( cursor: SystemMouseCursors.click, child: GestureDetector( onTap: () { var messageInfo = Provider.of(context, listen: false).messageCache.getByContentHash(qMessage.getMetadata().contenthash); if (messageInfo != null) { var index = Provider.of(context, listen: false).messageCache.findIndex(messageInfo.metadata.messageID); if (index != null) { Provider.of(context, listen: false).messageScrollController.scrollTo(index: index, duration: Duration(milliseconds: 100)); } } }, child: Container( margin: EdgeInsets.all(5), padding: EdgeInsets.all(5), clipBehavior: Clip.antiAlias, decoration: BoxDecoration( color: fromMe ? Provider.of(context).theme.messageFromOtherBackgroundColor : Provider.of(context).theme.messageFromMeBackgroundColor, ), height: 75, child: Column(children: [ Align(alignment: Alignment.centerLeft, child: wdgReplyingTo), Flexible( child: Row(mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.center, children: [ Padding(padding: EdgeInsets.symmetric(vertical: 5.0, horizontal: 10.0), child: Icon(Icons.reply, size: 32, color: qTextColor)), Flexible(child: qMessage.getPreviewWidget(context)), ])) ])))); } catch (e) { return MalformedBubble(); } } else { // This should be almost instantly resolved, any failure likely means an issue in decoding... return MessageLoadingBubble(); } }, ); var wdgDecorations = MessageBubbleDecoration(ackd: Provider.of(context).ackd, errored: Provider.of(context).error, fromMe: fromMe, messageDate: messageDate); var error = Provider.of(context).error; return LayoutBuilder(builder: (context, constraints) { return RepaintBoundary( child: Container( child: Container( decoration: BoxDecoration( color: error ? malformedColor : (fromMe ? Provider.of(context).theme.messageFromMeBackgroundColor : Provider.of(context).theme.messageFromOtherBackgroundColor), border: Border.all( color: error ? malformedColor : (fromMe ? Provider.of(context).theme.messageFromMeBackgroundColor : Provider.of(context).theme.messageFromOtherBackgroundColor), width: 1), borderRadius: BorderRadius.only( topLeft: Radius.circular(borderRadiousEh), topRight: Radius.circular(borderRadiousEh), bottomLeft: fromMe ? Radius.circular(borderRadiousEh) : Radius.zero, bottomRight: fromMe ? Radius.zero : Radius.circular(borderRadiousEh), ), ), child: Padding( padding: EdgeInsets.all(9.0), child: Column( crossAxisAlignment: fromMe ? CrossAxisAlignment.end : CrossAxisAlignment.start, mainAxisAlignment: fromMe ? MainAxisAlignment.end : MainAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: fromMe ? [wdgQuote, wdgMessage, wdgDecorations] : [wdgSender, wdgQuote, wdgMessage, wdgDecorations]))))); }); } }