LCOV - code coverage report
Current view: top level - lib/widgets - invitationbubble.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 0 100 0.0 %
Date: 2026-07-15 19:28:30 Functions: 0 0 -

          Line data    Source code
       1             : import 'package:cwtch/cwtch_icons_icons.dart';
       2             : import 'package:cwtch/models/contact.dart';
       3             : import 'package:cwtch/models/message.dart';
       4             : import 'package:cwtch/models/profile.dart';
       5             : import 'package:cwtch/themes/opaque.dart';
       6             : import 'package:cwtch/widgets/malformedbubble.dart';
       7             : import 'package:flutter/material.dart';
       8             : import 'package:provider/provider.dart';
       9             : import '../main.dart';
      10             : import 'package:cwtch/l10n/app_localizations.dart';
      11             : 
      12             : import '../models/redaction.dart';
      13             : import '../settings.dart';
      14             : import 'messageBubbleWidgetHelpers.dart';
      15             : import 'messagebubbledecorations.dart';
      16             : 
      17             : // Like MessageBubble but for displaying chat overlay 100/101 invitations
      18             : // Offers the user an accept/reject button if they don't have a matching contact already
      19             : class InvitationBubble extends StatefulWidget {
      20             :   final int overlay;
      21             :   final String inviteTarget;
      22             :   final String inviteNick;
      23             :   final String invite;
      24             : 
      25           0 :   InvitationBubble(this.overlay, this.inviteTarget, this.inviteNick, this.invite);
      26             : 
      27           0 :   @override
      28           0 :   InvitationBubbleState createState() => InvitationBubbleState();
      29             : }
      30             : 
      31             : class InvitationBubbleState extends State<InvitationBubble> {
      32             :   bool rejected = false;
      33             :   bool isAccepted = false;
      34             : 
      35           0 :   @override
      36             :   Widget build(BuildContext context) {
      37           0 :     var fromMe = Provider.of<MessageMetadata>(context).senderHandle == Provider.of<ProfileInfoState>(context).onion;
      38           0 :     var isGroup = widget.overlay == InviteGroupOverlay;
      39           0 :     isAccepted = Provider.of<ProfileInfoState>(context).contactList.findContact(widget.inviteTarget) != null;
      40             :     var borderRadiousEh = 15.0;
      41           0 :     var showGroupInvite = Provider.of<Settings>(context).isExperimentEnabled(TapirGroupsExperiment);
      42           0 :     rejected = Provider.of<MessageMetadata>(context).attributes["rejected-invite"] == "true";
      43           0 :     DateTime messageDate = Provider.of<MessageMetadata>(context).timestamp;
      44             : 
      45             :     // If the sender is not us, then we want to give them a nickname...
      46             :     var senderDisplayStr = "";
      47             :     if (!fromMe) {
      48           0 :       ContactInfoState? contact = Provider.of<ProfileInfoState>(context).contactList.findContact(Provider.of<MessageMetadata>(context).senderHandle);
      49             :       if (contact != null) {
      50           0 :         senderDisplayStr = redactedNick(context, contact.onion, contact.nickname);
      51             :       } else {
      52           0 :         senderDisplayStr = Provider.of<MessageMetadata>(context).senderHandle;
      53             :       }
      54             :     }
      55           0 :     return LayoutBuilder(
      56           0 :       builder: (context, constraints) {
      57           0 :         var wdgSender = compileSenderWidget(context, constraints, fromMe, senderDisplayStr);
      58             :         // If we receive an invite for ourselves, treat it as a bug. The UI no longer allows this so it could have only come from
      59             :         // some kind of malfeasance.
      60           0 :         var selfInvite = widget.inviteNick == Provider.of<ProfileInfoState>(context).onion;
      61             :         if (selfInvite) {
      62           0 :           return MalformedBubble();
      63             :         }
      64             : 
      65             :         var wdgMessage = isGroup && !showGroupInvite
      66           0 :             ? Text(AppLocalizations.of(context)!.groupInviteSettingsWarning, style: Provider.of<Settings>(context).scaleFonts(defaultTextStyle))
      67             :             : fromMe
      68           0 :             ? senderInviteChrome(
      69           0 :                 AppLocalizations.of(context)!.sendAnInvitation,
      70           0 :                 isGroup ? Provider.of<ProfileInfoState>(context).contactList.findContact(widget.inviteTarget)!.nickname : widget.inviteTarget,
      71             :               )
      72           0 :             : (inviteChrome(isGroup ? AppLocalizations.of(context)!.inviteToGroup : AppLocalizations.of(context)!.contactSuggestion, widget.inviteNick, widget.inviteTarget));
      73             : 
      74             :         Widget wdgDecorations;
      75             :         if (isGroup && !showGroupInvite) {
      76           0 :           wdgDecorations = Text('\u202F');
      77             :         } else if (fromMe) {
      78           0 :           wdgDecorations = MessageBubbleDecoration(ackd: Provider.of<MessageMetadata>(context).ackd, errored: Provider.of<MessageMetadata>(context).error, fromMe: fromMe, messageDate: messageDate);
      79           0 :         } else if (isAccepted) {
      80           0 :           wdgDecorations = Text(AppLocalizations.of(context)!.accepted + '\u202F', style: Provider.of<Settings>(context).scaleFonts(defaultTextStyle));
      81           0 :         } else if (this.rejected) {
      82           0 :           wdgDecorations = Text(AppLocalizations.of(context)!.rejected + '\u202F', style: Provider.of<Settings>(context).scaleFonts(defaultTextStyle));
      83             :         } else {
      84           0 :           wdgDecorations = Center(
      85             :             widthFactor: 1,
      86           0 :             child: Wrap(
      87           0 :               children: [
      88           0 :                 Padding(
      89           0 :                   padding: EdgeInsets.all(5),
      90           0 :                   child: ElevatedButton(
      91           0 :                     child: Text(AppLocalizations.of(context)!.rejectGroupBtn + '\u202F', style: Provider.of<Settings>(context).scaleFonts(defaultTextButtonStyle)),
      92           0 :                     onPressed: _btnReject,
      93             :                   ),
      94             :                 ),
      95           0 :                 Padding(
      96           0 :                   padding: EdgeInsets.all(5),
      97           0 :                   child: ElevatedButton(
      98           0 :                     child: Text(AppLocalizations.of(context)!.acceptGroupBtn + '\u202F', style: Provider.of<Settings>(context).scaleFonts(defaultTextButtonStyle)),
      99           0 :                     onPressed: _btnAccept,
     100             :                   ),
     101             :                 ),
     102             :               ],
     103             :             ),
     104             :           );
     105             :         }
     106             : 
     107             :         //print(constraints.toString()+", "+constraints.maxWidth.toString());
     108           0 :         return Center(
     109             :           widthFactor: 1.0,
     110           0 :           child: Theme(
     111           0 :             data: Theme.of(context).copyWith(
     112           0 :               textSelectionTheme: TextSelectionThemeData(
     113           0 :                 cursorColor: Provider.of<Settings>(context).theme.messageSelectionColor,
     114           0 :                 selectionColor: Provider.of<Settings>(context).theme.messageSelectionColor,
     115           0 :                 selectionHandleColor: Provider.of<Settings>(context).theme.messageSelectionColor,
     116             :               ),
     117             : 
     118             :               // Horrifying Hack: Flutter doesn't give us direct control over system menus but instead picks BG color from TextButtonThemeData ¯\_(ツ)_/¯
     119           0 :               textButtonTheme: TextButtonThemeData(style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Provider.of<Settings>(context).theme.menuBackgroundColor))),
     120             :             ),
     121           0 :             child: Container(
     122           0 :               decoration: BoxDecoration(
     123           0 :                 color: fromMe ? Provider.of<Settings>(context).theme.messageFromMeBackgroundColor : Provider.of<Settings>(context).theme.messageFromOtherBackgroundColor,
     124           0 :                 border: Border.all(color: fromMe ? Provider.of<Settings>(context).theme.messageFromMeBackgroundColor : Provider.of<Settings>(context).theme.messageFromOtherBackgroundColor, width: 1),
     125           0 :                 borderRadius: BorderRadius.only(
     126           0 :                   topLeft: Radius.circular(borderRadiousEh),
     127           0 :                   topRight: Radius.circular(borderRadiousEh),
     128           0 :                   bottomLeft: fromMe ? Radius.circular(borderRadiousEh) : Radius.zero,
     129           0 :                   bottomRight: fromMe ? Radius.zero : Radius.circular(borderRadiousEh),
     130             :                 ),
     131             :               ),
     132           0 :               child: Center(
     133             :                 widthFactor: 1.0,
     134           0 :                 child: Padding(
     135           0 :                   padding: EdgeInsets.all(9.0),
     136           0 :                   child: Wrap(
     137             :                     runAlignment: WrapAlignment.spaceEvenly,
     138             :                     alignment: WrapAlignment.spaceEvenly,
     139             :                     runSpacing: 1.0,
     140             :                     crossAxisAlignment: WrapCrossAlignment.center,
     141           0 :                     children: [
     142           0 :                       Center(
     143             :                         widthFactor: 1,
     144           0 :                         child: Padding(padding: EdgeInsets.all(10.0), child: Icon(isGroup && !showGroupInvite ? CwtchIcons.enable_experiments : CwtchIcons.send_invite, size: 32)),
     145             :                       ),
     146           0 :                       Center(
     147             :                         widthFactor: 1.0,
     148           0 :                         child: Column(
     149             :                           crossAxisAlignment: fromMe ? CrossAxisAlignment.end : CrossAxisAlignment.start,
     150             :                           mainAxisAlignment: fromMe ? MainAxisAlignment.end : MainAxisAlignment.start,
     151             :                           mainAxisSize: MainAxisSize.min,
     152           0 :                           children: fromMe ? [wdgMessage, wdgDecorations] : [wdgSender, wdgMessage, wdgDecorations],
     153             :                         ),
     154             :                       ),
     155             :                     ],
     156             :                   ),
     157             :                 ),
     158             :               ),
     159             :             ),
     160             :           ),
     161             :         );
     162             :       },
     163             :     );
     164             :   }
     165             : 
     166           0 :   void _btnReject() {
     167           0 :     setState(() {
     168           0 :       var profileOnion = Provider.of<ProfileInfoState>(context, listen: false).onion;
     169           0 :       var conversation = Provider.of<ContactInfoState>(context, listen: false).identifier;
     170           0 :       var idx = Provider.of<MessageMetadata>(context, listen: false).messageID;
     171           0 :       Provider.of<FlwtchState>(context, listen: false).cwtch.SetMessageAttribute(profileOnion, conversation, 0, idx, "rejected-invite", "true");
     172             :       //Provider.of<MessageMetadata>(context, listen: false).flags |= 0x01;
     173             :     });
     174             :   }
     175             : 
     176           0 :   void _btnAccept() {
     177           0 :     setState(() {
     178           0 :       var profileOnion = Provider.of<ProfileInfoState>(context, listen: false).onion;
     179           0 :       Provider.of<FlwtchState>(context, listen: false).cwtch.ImportBundle(profileOnion, widget.invite);
     180           0 :       isAccepted = true;
     181             :     });
     182             :   }
     183             : 
     184             :   // Construct an invite chrome for the sender
     185           0 :   Widget senderInviteChrome(String chrome, String targetName) {
     186           0 :     var settings = Provider.of<Settings>(context);
     187             : 
     188           0 :     return Wrap(
     189           0 :       children: [
     190           0 :         SelectableText(
     191           0 :           chrome + '\u202F',
     192           0 :           style: settings.scaleFonts(defaultMessageTextStyle.copyWith(color: Provider.of<Settings>(context).theme.messageFromMeTextColor)),
     193             :           textAlign: TextAlign.left,
     194             :           maxLines: 2,
     195             :           textWidthBasis: TextWidthBasis.longestLine,
     196             :         ),
     197           0 :         SelectableText(
     198           0 :           targetName + '\u202F',
     199           0 :           style: settings.scaleFonts(defaultMessageTextStyle.copyWith(color: Provider.of<Settings>(context).theme.messageFromMeTextColor)),
     200             :           textAlign: TextAlign.left,
     201             :           maxLines: 2,
     202             :           textWidthBasis: TextWidthBasis.longestLine,
     203             :         ),
     204             :       ],
     205             :     );
     206             :   }
     207             : 
     208             :   // Construct an invite chrome
     209           0 :   Widget inviteChrome(String chrome, String targetName, String targetId) {
     210           0 :     var settings = Provider.of<Settings>(context);
     211             : 
     212           0 :     return Wrap(
     213           0 :       children: [
     214           0 :         SelectableText(
     215           0 :           chrome + '\u202F',
     216           0 :           style: settings.scaleFonts(defaultMessageTextStyle.copyWith(color: Provider.of<Settings>(context).theme.messageFromOtherTextColor)),
     217             :           textAlign: TextAlign.left,
     218             :           textWidthBasis: TextWidthBasis.longestLine,
     219             :           maxLines: 2,
     220             :         ),
     221           0 :         SelectableText(
     222           0 :           targetName + '\u202F',
     223           0 :           style: settings.scaleFonts(defaultMessageTextStyle.copyWith(color: Provider.of<Settings>(context).theme.messageFromOtherTextColor)),
     224             :           textAlign: TextAlign.left,
     225             :           maxLines: 2,
     226             :           textWidthBasis: TextWidthBasis.longestLine,
     227             :         ),
     228             :       ],
     229             :     );
     230             :   }
     231             : }

Generated by: LCOV version 1.14