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

Generated by: LCOV version 1.14