LCOV - code coverage report
Current view: top level - lib/views - addeditprofileview.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 0 338 0.0 %
Date: 2026-09-01 20:55:37 Functions: 0 0 -

          Line data    Source code
       1             : import 'dart:io';
       2             : 
       3             : import 'package:cwtch/config.dart';
       4             : import 'package:cwtch/controllers/filesharing.dart';
       5             : import 'package:cwtch/cwtch/cwtch.dart';
       6             : import 'package:cwtch/models/appstate.dart';
       7             : import 'package:cwtch/models/profile.dart';
       8             : import 'package:cwtch/controllers/filesharing.dart' as filesharing;
       9             : import 'package:flutter/material.dart';
      10             : import 'package:flutter/services.dart';
      11             : import 'package:cwtch/widgets/buttontextfield.dart';
      12             : import 'package:cwtch/widgets/cwtchlabel.dart';
      13             : import 'package:cwtch/widgets/passwordfield.dart';
      14             : import 'package:cwtch/widgets/profileimage.dart';
      15             : import 'package:cwtch/widgets/textfield.dart';
      16             : import 'package:provider/provider.dart';
      17             : import 'package:cwtch/l10n/app_localizations.dart';
      18             : 
      19             : import '../constants.dart';
      20             : import '../cwtch_icons_icons.dart';
      21             : import '../errorHandler.dart';
      22             : import '../main.dart';
      23             : import '../settings.dart';
      24             : 
      25             : class AddEditProfileView extends StatefulWidget {
      26           0 :   const AddEditProfileView({Key? key}) : super(key: key);
      27             : 
      28           0 :   @override
      29           0 :   _AddEditProfileViewState createState() => _AddEditProfileViewState();
      30             : }
      31             : 
      32             : class _AddEditProfileViewState extends State<AddEditProfileView> {
      33             :   final _formKey = GlobalKey<FormState>();
      34             : 
      35             :   final ctrlrNick = TextEditingController(text: "");
      36             :   final ctrlrPrivateName = TextEditingController(text: "");
      37             :   final ctrlrOldPass = TextEditingController(text: "");
      38             :   final ctrlrPass = TextEditingController(text: "");
      39             :   final ctrlrPass2 = TextEditingController(text: "");
      40             :   final ctrlrOnion = TextEditingController(text: "");
      41             : 
      42             :   final ctrlrAttribute1 = TextEditingController(text: "");
      43             :   final ctrlrAttribute2 = TextEditingController(text: "");
      44             :   final ctrlrAttribute3 = TextEditingController(text: "");
      45             : 
      46             :   ScrollController controller = ScrollController();
      47             :   late bool usePassword;
      48             :   late bool deleted;
      49             : 
      50           0 :   @override
      51             :   void initState() {
      52           0 :     super.initState();
      53           0 :     usePassword = true;
      54           0 :     final nickname = Provider.of<ProfileInfoState>(context, listen: false).nickname;
      55           0 :     if (nickname.isNotEmpty) {
      56           0 :       ctrlrNick.text = nickname;
      57             :     }
      58           0 :     ctrlrPrivateName.text = Provider.of<ProfileInfoState>(context, listen: false).getPrivateName();
      59             :   }
      60             : 
      61           0 :   @override
      62             :   Widget build(BuildContext context) {
      63           0 :     ctrlrOnion.text = Provider.of<ProfileInfoState>(context).onion;
      64           0 :     return Scaffold(
      65           0 :       appBar: AppBar(title: Text(Provider.of<ProfileInfoState>(context).onion.isEmpty ? AppLocalizations.of(context)!.addProfileTitle : AppLocalizations.of(context)!.editProfileTitle)),
      66           0 :       body: _buildForm(),
      67             :     );
      68             :   }
      69             : 
      70           0 :   void _handleSwitchPassword(bool? value) {
      71           0 :     setState(() {
      72           0 :       usePassword = value!;
      73             :     });
      74             :   }
      75             : 
      76             :   //  A few implementation notes
      77             :   // We use Visibility to hide optional structures when they are not requested.
      78             :   // We used SizedBox for inter-widget height padding in columns, otherwise elements can render a little too close together.
      79           0 :   Widget _buildForm() {
      80           0 :     return Consumer<Settings>(
      81           0 :       builder: (context, theme, child) {
      82           0 :         return LayoutBuilder(
      83           0 :           builder: (BuildContext context, BoxConstraints viewportConstraints) {
      84           0 :             return Scrollbar(
      85             :               trackVisibility: true,
      86           0 :               controller: controller,
      87           0 :               child: SingleChildScrollView(
      88           0 :                 controller: controller,
      89             :                 clipBehavior: Clip.antiAlias,
      90           0 :                 child: ConstrainedBox(
      91           0 :                   constraints: BoxConstraints(minHeight: viewportConstraints.maxHeight),
      92           0 :                   child: Form(
      93           0 :                     key: _formKey,
      94           0 :                     child: Material(
      95           0 :                       color: theme.theme.backgroundPaneColor,
      96           0 :                       child: Padding(
      97           0 :                         padding: EdgeInsets.all(50),
      98           0 :                         child: Column(
      99             :                           mainAxisAlignment: MainAxisAlignment.start,
     100             :                           crossAxisAlignment: CrossAxisAlignment.center,
     101           0 :                           children: [
     102           0 :                             Visibility(
     103           0 :                               visible: Provider.of<ProfileInfoState>(context).onion.isNotEmpty,
     104           0 :                               child: Column(
     105             :                                 crossAxisAlignment: CrossAxisAlignment.center,
     106             :                                 mainAxisAlignment: MainAxisAlignment.center,
     107             :                                 mainAxisSize: MainAxisSize.min,
     108           0 :                                 children: [
     109           0 :                                   Row(
     110             :                                     mainAxisAlignment: MainAxisAlignment.center,
     111           0 :                                     children: <Widget>[
     112           0 :                                       MouseRegion(
     113           0 :                                         cursor: Provider.of<Settings>(context).isExperimentEnabled(ImagePreviewsExperiment) ? SystemMouseCursors.click : SystemMouseCursors.basic,
     114           0 :                                         child: GestureDetector(
     115             :                                           // don't allow setting of profile images if the image previews experiment is disabled.
     116             :                                           onTap:
     117           0 :                                               Provider.of<AppState>(context, listen: false).disableFilePicker ||
     118           0 :                                                   !Provider.of<Settings>(context, listen: false).isExperimentEnabled(ImagePreviewsExperiment)
     119             :                                               ? null
     120           0 :                                               : () {
     121           0 :                                                   filesharing.showFilePicker(
     122             :                                                     context,
     123             :                                                     MaxImageFileSharingSize,
     124           0 :                                                     (File file) {
     125           0 :                                                       var profile = Provider.of<ProfileInfoState>(context, listen: false).onion;
     126             :                                                       // Share this image publicly (conversation handle == -1)
     127           0 :                                                       Provider.of<FlwtchState>(context, listen: false).cwtch.ShareFile(profile, -1, file.path);
     128             :                                                       // update the image cache locally
     129           0 :                                                       Provider.of<ProfileInfoState>(context, listen: false).imagePath = file.path;
     130             :                                                     },
     131           0 :                                                     () {
     132           0 :                                                       final snackBar = SnackBar(content: Text(AppLocalizations.of(context)!.msgFileTooBig), duration: Duration(seconds: 4));
     133           0 :                                                       ScaffoldMessenger.of(context).showSnackBar(snackBar);
     134             :                                                     },
     135           0 :                                                     () {},
     136             :                                                   );
     137             :                                                 },
     138           0 :                                           child: ProfileImage(
     139           0 :                                             imagePath: Provider.of<Settings>(context).isExperimentEnabled(ImagePreviewsExperiment)
     140           0 :                                                 ? Provider.of<ProfileInfoState>(context).imagePath
     141           0 :                                                 : Provider.of<ProfileInfoState>(context).defaultImagePath,
     142             :                                             diameter: 120,
     143           0 :                                             tooltip: Provider.of<Settings>(context).isExperimentEnabled(ImagePreviewsExperiment) ? AppLocalizations.of(context)!.tooltipSelectACustomProfileImage : "",
     144             :                                             maskOut: false,
     145           0 :                                             border: theme.theme.portraitOnlineBorderColor,
     146           0 :                                             badgeTextColor: theme.theme.portraitContactBadgeTextColor,
     147           0 :                                             badgeColor: theme.theme.portraitContactBadgeColor,
     148           0 :                                             badgeEdit: Provider.of<Settings>(context).isExperimentEnabled(ImagePreviewsExperiment),
     149             :                                           ),
     150             :                                         ),
     151             :                                       ),
     152             :                                     ],
     153             :                                   ),
     154           0 :                                   SizedBox(
     155           0 :                                     width: MediaQuery.of(context).size.width / 2,
     156           0 :                                     child: Column(
     157           0 :                                       children: [
     158           0 :                                         Padding(
     159           0 :                                           padding: EdgeInsets.all(5.0),
     160           0 :                                           child: CwtchTextField(
     161           0 :                                             controller: ctrlrAttribute1,
     162             :                                             multiLine: false,
     163           0 :                                             onChanged: (profileAttribute1) {
     164           0 :                                               String onion = Provider.of<ProfileInfoState>(context, listen: false).onion;
     165           0 :                                               Provider.of<FlwtchState>(context, listen: false).cwtch.SetProfileAttribute(onion, "profile.profile-attribute-1", profileAttribute1);
     166           0 :                                               Provider.of<ProfileInfoState>(context, listen: false).attributes[0] = profileAttribute1;
     167             :                                             },
     168           0 :                                             hintText: Provider.of<ProfileInfoState>(context).attributes[0] ?? AppLocalizations.of(context)!.profileInfoHint,
     169             :                                           ),
     170             :                                         ),
     171           0 :                                         Padding(
     172           0 :                                           padding: EdgeInsets.all(5.0),
     173           0 :                                           child: CwtchTextField(
     174           0 :                                             controller: ctrlrAttribute2,
     175             :                                             multiLine: false,
     176           0 :                                             onChanged: (profileAttribute2) {
     177           0 :                                               String onion = Provider.of<ProfileInfoState>(context, listen: false).onion;
     178           0 :                                               Provider.of<FlwtchState>(context, listen: false).cwtch.SetProfileAttribute(onion, "profile.profile-attribute-2", profileAttribute2);
     179           0 :                                               Provider.of<ProfileInfoState>(context, listen: false).attributes[1] = profileAttribute2;
     180             :                                             },
     181           0 :                                             hintText: Provider.of<ProfileInfoState>(context).attributes[1] ?? AppLocalizations.of(context)!.profileInfoHint2,
     182             :                                           ),
     183             :                                         ),
     184           0 :                                         Padding(
     185           0 :                                           padding: EdgeInsets.all(5.0),
     186           0 :                                           child: CwtchTextField(
     187           0 :                                             controller: ctrlrAttribute3,
     188             :                                             multiLine: false,
     189           0 :                                             onChanged: (profileAttribute3) {
     190           0 :                                               String onion = Provider.of<ProfileInfoState>(context, listen: false).onion;
     191           0 :                                               Provider.of<FlwtchState>(context, listen: false).cwtch.SetProfileAttribute(onion, "profile.profile-attribute-3", profileAttribute3);
     192           0 :                                               Provider.of<ProfileInfoState>(context, listen: false).attributes[2] = profileAttribute3;
     193             :                                             },
     194           0 :                                             hintText: Provider.of<ProfileInfoState>(context).attributes[2] ?? AppLocalizations.of(context)!.profileInfoHint3,
     195             :                                           ),
     196             :                                         ),
     197             :                                       ],
     198             :                                     ),
     199             :                                   ),
     200             :                                 ],
     201             :                               ),
     202             :                             ),
     203           0 :                             Column(
     204             :                               crossAxisAlignment: CrossAxisAlignment.start,
     205           0 :                               children: [
     206           0 :                                 SizedBox(height: 20),
     207           0 :                                 CwtchLabel(label: AppLocalizations.of(context)!.displayNameLabel),
     208           0 :                                 SizedBox(height: 20),
     209           0 :                                 CwtchTextField(
     210           0 :                                   key: Key("displayNameFormElement"),
     211           0 :                                   controller: ctrlrNick,
     212             :                                   autofocus: false,
     213           0 :                                   hintText: AppLocalizations.of(context)!.yourDisplayName,
     214           0 :                                   validator: (value) {
     215           0 :                                     if (value.isEmpty) {
     216           0 :                                       return AppLocalizations.of(context)!.displayNameTooltip;
     217             :                                     }
     218             :                                     return null;
     219             :                                   },
     220             :                                 ),
     221           0 :                                 SizedBox(height: 20),
     222           0 :                                 CwtchLabel(label: AppLocalizations.of(context)!.privateNameLabel),
     223           0 :                                 SizedBox(height: 20),
     224           0 :                                 CwtchTextField(key: Key("privateNameFormElement"), controller: ctrlrPrivateName, autofocus: false, hintText: AppLocalizations.of(context)!.privateNameHint),
     225             :                               ],
     226             :                             ),
     227           0 :                             Visibility(
     228           0 :                               visible: Provider.of<ProfileInfoState>(context).onion.isNotEmpty,
     229           0 :                               child: Column(
     230             :                                 mainAxisAlignment: MainAxisAlignment.start,
     231             :                                 crossAxisAlignment: CrossAxisAlignment.start,
     232           0 :                                 children: [
     233           0 :                                   SizedBox(height: 20),
     234           0 :                                   CwtchLabel(label: AppLocalizations.of(context)!.addressLabel),
     235           0 :                                   SizedBox(height: 20),
     236           0 :                                   CwtchButtonTextField(
     237           0 :                                     controller: ctrlrOnion,
     238           0 :                                     onPressed: _copyOnion,
     239             :                                     readonly: true,
     240           0 :                                     icon: Icon(CwtchIcons.copy_address, size: 32),
     241           0 :                                     tooltip: AppLocalizations.of(context)!.copyBtn,
     242             :                                   ),
     243             :                                 ],
     244             :                               ),
     245             :                             ),
     246             :                             // We only allow setting password types on profile creation
     247             : 
     248             :                             // Enabled
     249           0 :                             Visibility(
     250             :                               // FIXME don't show the disable switch in test mode...this is a bug relating to scrolling things into view
     251           0 :                               visible: Provider.of<ProfileInfoState>(context).onion.isNotEmpty && (EnvironmentConfig.TEST_MODE == false),
     252           0 :                               child: Material(
     253           0 :                                 child: SwitchListTile(
     254           0 :                                   title: Text(AppLocalizations.of(context)!.profileEnabled, style: TextStyle(color: Provider.of<Settings>(context).current().mainTextColor)),
     255           0 :                                   subtitle: Text(AppLocalizations.of(context)!.profileEnabledDescription),
     256           0 :                                   value: Provider.of<ProfileInfoState>(context).enabled,
     257           0 :                                   onChanged: (bool value) {
     258           0 :                                     Provider.of<ProfileInfoState>(context, listen: false).enabled = value;
     259             :                                     if (value) {
     260           0 :                                       if (Provider.of<ProfileInfoState>(context, listen: false).appearOffline == false) {
     261           0 :                                         Provider.of<FlwtchState>(context, listen: false).cwtch.ConfigureConnections(Provider.of<ProfileInfoState>(context, listen: false).onion, true, true, true);
     262             :                                       } else {
     263           0 :                                         Provider.of<FlwtchState>(context, listen: false).cwtch.ConfigureConnections(Provider.of<ProfileInfoState>(context, listen: false).onion, false, false, false);
     264             :                                       }
     265             :                                     } else {
     266           0 :                                       Provider.of<ProfileInfoState>(context, listen: false).deactivatePeerEngine(context);
     267             :                                     }
     268             :                                   },
     269           0 :                                   activeTrackColor: Provider.of<Settings>(context).theme.defaultButtonColor,
     270           0 :                                   inactiveTrackColor: Provider.of<Settings>(context).theme.defaultButtonDisabledColor,
     271           0 :                                   secondary: Icon(CwtchIcons.negative_heart_24px, color: Provider.of<Settings>(context).current().mainTextColor),
     272             :                                 ),
     273             :                               ),
     274             :                             ),
     275             : 
     276             :                             // Auto start
     277           0 :                             Material(
     278           0 :                               child: SwitchListTile(
     279           0 :                                 title: Text(AppLocalizations.of(context)!.profileAutostartLabel, style: TextStyle(color: Provider.of<Settings>(context).current().mainTextColor)),
     280           0 :                                 subtitle: Text(AppLocalizations.of(context)!.profileAutostartDescription),
     281           0 :                                 value: Provider.of<ProfileInfoState>(context).autostart,
     282           0 :                                 onChanged: (bool value) {
     283           0 :                                   Provider.of<ProfileInfoState>(context, listen: false).autostart = value;
     284             : 
     285           0 :                                   if (Provider.of<ProfileInfoState>(context, listen: false).onion.isNotEmpty) {
     286           0 :                                     Provider.of<FlwtchState>(
     287             :                                       context,
     288             :                                       listen: false,
     289           0 :                                     ).cwtch.SetProfileAttribute(Provider.of<ProfileInfoState>(context, listen: false).onion, "profile.autostart", value ? "true" : "false");
     290             :                                   }
     291             :                                 },
     292           0 :                                 activeTrackColor: Provider.of<Settings>(context).theme.defaultButtonColor,
     293           0 :                                 inactiveTrackColor: Provider.of<Settings>(context).theme.defaultButtonDisabledColor,
     294           0 :                                 secondary: Icon(CwtchIcons.favorite_24px, color: Provider.of<Settings>(context).current().mainTextColor),
     295             :                               ),
     296             :                             ),
     297             : 
     298             :                             // Appear Offline
     299           0 :                             Visibility(
     300             :                               // FIXME don't show the disable switch in test mode...this is a bug relating to scrolling things into view
     301           0 :                               visible: Provider.of<ProfileInfoState>(context).onion.isNotEmpty && (EnvironmentConfig.TEST_MODE == false),
     302           0 :                               child: Material(
     303           0 :                                 child: SwitchListTile(
     304           0 :                                   title: Text(AppLocalizations.of(context)!.profileOfflineAtStart, style: TextStyle(color: Provider.of<Settings>(context).current().mainTextColor)),
     305           0 :                                   subtitle: Text(AppLocalizations.of(context)!.profileAppearOfflineDescription),
     306           0 :                                   value: Provider.of<ProfileInfoState>(context).appearOfflineAtStartup,
     307           0 :                                   onChanged: (bool value) {
     308           0 :                                     Provider.of<ProfileInfoState>(context, listen: false).appearOfflineAtStartup = value;
     309           0 :                                     var onion = Provider.of<ProfileInfoState>(context, listen: false).onion;
     310           0 :                                     if (onion.isNotEmpty) {
     311           0 :                                       Provider.of<FlwtchState>(context, listen: false).cwtch.SetProfileAttribute(onion, "profile.appear-offline", value ? "true" : "false");
     312             :                                     }
     313             :                                   },
     314           0 :                                   activeTrackColor: Provider.of<Settings>(context).theme.defaultButtonColor,
     315           0 :                                   inactiveTrackColor: Provider.of<Settings>(context).theme.defaultButtonDisabledColor,
     316           0 :                                   secondary: Icon(CwtchIcons.favorite_24px, color: Provider.of<Settings>(context).current().mainTextColor),
     317             :                                 ),
     318             :                               ),
     319             :                             ),
     320             : 
     321           0 :                             Visibility(visible: Provider.of<ProfileInfoState>(context).onion.isEmpty, child: SizedBox(height: 20)),
     322           0 :                             Visibility(
     323           0 :                               visible: Provider.of<ProfileInfoState>(context).onion.isEmpty,
     324           0 :                               child: Column(
     325             :                                 mainAxisAlignment: MainAxisAlignment.center,
     326           0 :                                 children: <Widget>[
     327           0 :                                   Checkbox(
     328           0 :                                     key: Key("passwordCheckBox"),
     329           0 :                                     value: usePassword,
     330           0 :                                     fillColor: MaterialStateProperty.all(theme.current().defaultButtonColor),
     331           0 :                                     activeColor: theme.current().defaultButtonActiveColor,
     332           0 :                                     onChanged: _handleSwitchPassword,
     333             :                                   ),
     334           0 :                                   Text(AppLocalizations.of(context)!.radioUsePassword, style: TextStyle(color: theme.current().mainTextColor)),
     335           0 :                                   SizedBox(height: 20),
     336           0 :                                   Padding(
     337           0 :                                     padding: EdgeInsets.symmetric(horizontal: 24),
     338           0 :                                     child: Text(
     339           0 :                                       usePassword ? AppLocalizations.of(context)!.encryptedProfileDescription : AppLocalizations.of(context)!.plainProfileDescription,
     340             :                                       textAlign: TextAlign.center,
     341             :                                     ),
     342             :                                   ),
     343             :                                 ],
     344             :                               ),
     345             :                             ),
     346           0 :                             SizedBox(height: 20),
     347           0 :                             Visibility(
     348           0 :                               visible: usePassword,
     349           0 :                               child: Column(
     350             :                                 mainAxisAlignment: MainAxisAlignment.start,
     351             :                                 crossAxisAlignment: CrossAxisAlignment.start,
     352           0 :                                 children: <Widget>[
     353           0 :                                   Visibility(
     354           0 :                                     visible: Provider.of<ProfileInfoState>(context, listen: false).onion.isNotEmpty && Provider.of<ProfileInfoState>(context).isEncrypted,
     355           0 :                                     child: Column(
     356             :                                       mainAxisAlignment: MainAxisAlignment.start,
     357             :                                       crossAxisAlignment: CrossAxisAlignment.start,
     358           0 :                                       children: [
     359           0 :                                         CwtchLabel(label: AppLocalizations.of(context)!.currentPasswordLabel),
     360           0 :                                         SizedBox(height: 20),
     361           0 :                                         CwtchPasswordField(
     362           0 :                                           key: Key("currentPasswordFormElement"),
     363           0 :                                           controller: ctrlrOldPass,
     364           0 :                                           autoFillHints: [AutofillHints.newPassword],
     365           0 :                                           validator: (value) {
     366             :                                             // Password field can be empty when just updating the profile, not on creation
     367           0 :                                             if (Provider.of<ProfileInfoState>(context, listen: false).isEncrypted &&
     368           0 :                                                 Provider.of<ProfileInfoState>(context, listen: false).onion.isEmpty &&
     369           0 :                                                 value.isEmpty &&
     370           0 :                                                 usePassword) {
     371           0 :                                               return AppLocalizations.of(context)!.passwordErrorEmpty;
     372             :                                             }
     373           0 :                                             if (Provider.of<ErrorHandler>(context, listen: false).deleteProfileError == true) {
     374           0 :                                               return AppLocalizations.of(context)!.enterCurrentPasswordForDelete;
     375             :                                             }
     376             :                                             return null;
     377             :                                           },
     378             :                                         ),
     379           0 :                                         SizedBox(height: 20),
     380             :                                       ],
     381             :                                     ),
     382             :                                   ),
     383           0 :                                   CwtchLabel(label: AppLocalizations.of(context)!.newPassword),
     384           0 :                                   SizedBox(height: 20),
     385           0 :                                   CwtchPasswordField(
     386           0 :                                     key: Key("passwordFormElement"),
     387           0 :                                     controller: ctrlrPass,
     388           0 :                                     validator: (value) {
     389             :                                       // Password field can be empty when just updating the profile, not on creation
     390           0 :                                       if (Provider.of<ProfileInfoState>(context, listen: false).onion.isEmpty && value.isEmpty && usePassword) {
     391           0 :                                         return AppLocalizations.of(context)!.passwordErrorEmpty;
     392             :                                       }
     393           0 :                                       if (value != ctrlrPass2.value.text) {
     394           0 :                                         return AppLocalizations.of(context)!.passwordErrorMatch;
     395             :                                       }
     396             :                                       return null;
     397             :                                     },
     398             :                                   ),
     399           0 :                                   SizedBox(height: 20),
     400           0 :                                   CwtchLabel(label: AppLocalizations.of(context)!.password2Label),
     401           0 :                                   SizedBox(height: 20),
     402           0 :                                   CwtchPasswordField(
     403           0 :                                     key: Key("confirmPasswordFormElement"),
     404           0 :                                     controller: ctrlrPass2,
     405           0 :                                     validator: (value) {
     406             :                                       // Password field can be empty when just updating the profile, not on creation
     407           0 :                                       if (Provider.of<ProfileInfoState>(context, listen: false).onion.isEmpty && value.isEmpty && usePassword) {
     408           0 :                                         return AppLocalizations.of(context)!.passwordErrorEmpty;
     409             :                                       }
     410           0 :                                       if (value != ctrlrPass.value.text) {
     411           0 :                                         return AppLocalizations.of(context)!.passwordErrorMatch;
     412             :                                       }
     413             :                                       return null;
     414             :                                     },
     415             :                                   ),
     416             :                                 ],
     417             :                               ),
     418             :                             ),
     419           0 :                             SizedBox(height: 20),
     420           0 :                             ElevatedButton(
     421           0 :                               key: Key("createOrSaveProfileBtn"),
     422           0 :                               onPressed: _createPressed,
     423           0 :                               style: ElevatedButton.styleFrom(
     424           0 :                                 minimumSize: Size(400, 75),
     425           0 :                                 maximumSize: Size(800, 75),
     426           0 :                                 shape: RoundedRectangleBorder(
     427           0 :                                   borderRadius: BorderRadius.horizontal(left: Radius.circular(180), right: Radius.circular(180)),
     428             :                                 ),
     429             :                               ),
     430           0 :                               child: Text(
     431           0 :                                 Provider.of<ProfileInfoState>(context).onion.isEmpty ? AppLocalizations.of(context)!.addNewProfileBtn : AppLocalizations.of(context)!.saveProfileBtn,
     432             :                                 textAlign: TextAlign.center,
     433             :                               ),
     434             :                             ),
     435           0 :                             SizedBox(height: 20),
     436           0 :                             Visibility(
     437           0 :                               visible: Provider.of<ProfileInfoState>(context, listen: false).onion.isNotEmpty,
     438           0 :                               child: Tooltip(
     439           0 :                                 message: AppLocalizations.of(context)!.exportProfileTooltip,
     440           0 :                                 child: OutlinedButton.icon(
     441           0 :                                   style: OutlinedButton.styleFrom(minimumSize: Size(400, 75), maximumSize: Size(800, 75)),
     442           0 :                                   onPressed: () {
     443           0 :                                     if (Platform.isAndroid) {
     444           0 :                                       Provider.of<FlwtchState>(context, listen: false).cwtch.ExportProfile(ctrlrOnion.value.text, ctrlrOnion.value.text + ".tar.gz");
     445           0 :                                       final snackBar = SnackBar(content: Text(AppLocalizations.of(context)!.fileSavedTo + " " + ctrlrOnion.value.text + ".tar.gz"));
     446           0 :                                       ScaffoldMessenger.of(context).showSnackBar(snackBar);
     447             :                                     } else {
     448           0 :                                       showCreateFilePicker(context).then((name) {
     449             :                                         if (name != null) {
     450           0 :                                           Provider.of<FlwtchState>(context, listen: false).cwtch.ExportProfile(ctrlrOnion.value.text, name);
     451           0 :                                           final snackBar = SnackBar(content: Text(AppLocalizations.of(context)!.fileSavedTo + " " + name));
     452           0 :                                           ScaffoldMessenger.of(context).showSnackBar(snackBar);
     453             :                                         }
     454             :                                       });
     455             :                                     }
     456             :                                   },
     457           0 :                                   icon: Icon(Icons.import_export),
     458           0 :                                   label: Text(AppLocalizations.of(context)!.exportProfile),
     459             :                                 ),
     460             :                               ),
     461             :                             ),
     462           0 :                             SizedBox(height: 20),
     463           0 :                             Visibility(
     464           0 :                               visible: Provider.of<ProfileInfoState>(context, listen: false).onion.isNotEmpty,
     465           0 :                               child: Tooltip(
     466           0 :                                 message: AppLocalizations.of(context)!.enterCurrentPasswordForDelete,
     467           0 :                                 child: FilledButton.icon(
     468           0 :                                   style: FilledButton.styleFrom(
     469           0 :                                     minimumSize: Size(400, 75),
     470           0 :                                     maximumSize: Size(800, 75),
     471           0 :                                     shape: RoundedRectangleBorder(
     472           0 :                                       side: BorderSide(color: Provider.of<Settings>(context).theme.defaultButtonActiveColor, width: 2.0),
     473           0 :                                       borderRadius: BorderRadius.horizontal(left: Radius.circular(180), right: Radius.circular(180)),
     474             :                                     ),
     475             :                                   ),
     476           0 :                                   onPressed: () {
     477           0 :                                     showAlertDialog(context);
     478             :                                   },
     479           0 :                                   icon: Icon(Icons.delete_forever),
     480           0 :                                   label: Text(AppLocalizations.of(context)!.deleteBtn),
     481             :                                 ),
     482             :                               ),
     483             :                             ),
     484             :                           ],
     485             :                         ),
     486             :                       ),
     487             :                     ),
     488             :                   ),
     489             :                 ),
     490             :               ),
     491             :             );
     492             :           },
     493             :         );
     494             :       },
     495             :     );
     496             :   }
     497             : 
     498           0 :   void _copyOnion() {
     499           0 :     Clipboard.setData(new ClipboardData(text: Provider.of<ProfileInfoState>(context, listen: false).onion));
     500           0 :     final snackBar = SnackBar(content: Text(AppLocalizations.of(context)!.copiedToClipboardNotification));
     501           0 :     ScaffoldMessenger.of(context).showSnackBar(snackBar);
     502             :   }
     503             : 
     504           0 :   void _createPressed() async {
     505             :     // This will run all the validations in the form including
     506             :     // checking that display name is not empty, and an actual check that the passwords
     507             :     // match (and are provided if the user has requested an encrypted profile).
     508           0 :     if (_formKey.currentState!.validate()) {
     509           0 :       if (Provider.of<ProfileInfoState>(context, listen: false).onion.isEmpty) {
     510           0 :         if (usePassword == true) {
     511           0 :           Provider.of<FlwtchState>(context, listen: false).cwtch.CreateProfile(ctrlrNick.value.text, ctrlrPass.value.text, Provider.of<ProfileInfoState>(context, listen: false).autostart).then((
     512             :             profile_id,
     513             :           ) {
     514           0 :             Provider.of<FlwtchState>(context, listen: false).cwtch.SetProfileAttribute(profile_id, "profile.private-name", ctrlrPrivateName.value.text);
     515             :           });
     516             :         } else {
     517           0 :           Provider.of<FlwtchState>(context, listen: false).cwtch.CreateProfile(ctrlrNick.value.text, DefaultPassword, Provider.of<ProfileInfoState>(context, listen: false).autostart).then((
     518             :             profile_id,
     519             :           ) {
     520           0 :             Provider.of<FlwtchState>(context, listen: false).cwtch.SetProfileAttribute(profile_id, "profile.private-name", ctrlrPrivateName.value.text);
     521             :           });
     522             :         }
     523           0 :         Navigator.of(context).pop();
     524             :       } else {
     525             :         // Profile Editing
     526           0 :         var profile = Provider.of<ProfileInfoState>(context, listen: false).onion;
     527             :         // update name
     528           0 :         Provider.of<ProfileInfoState>(context, listen: false).nickname = ctrlrNick.value.text;
     529           0 :         Provider.of<FlwtchState>(context, listen: false).cwtch.SetProfileAttribute(profile, "profile.name", ctrlrNick.value.text);
     530           0 :         Provider.of<ProfileInfoState>(context, listen: false).setPrivateName(ctrlrPrivateName.value.text);
     531           0 :         Provider.of<FlwtchState>(context, listen: false).cwtch.SetProfileAttribute(profile, "profile.private-name", ctrlrPrivateName.value.text);
     532             : 
     533           0 :         if (ctrlrPass.value.text.isEmpty) {
     534           0 :           Navigator.of(context).pop();
     535             :         } else {
     536             :           // At this points passwords have been validated to be the same and not empty so update password
     537             :           // Use default password if the profile is unencrypted
     538           0 :           var password = Provider.of<ProfileInfoState>(context, listen: false).isEncrypted ? ctrlrOldPass.text : DefaultPassword;
     539           0 :           Provider.of<FlwtchState>(context, listen: false).cwtch.ChangePassword(profile, password, ctrlrPass.text, ctrlrPass2.text);
     540             : 
     541           0 :           EnvironmentConfig.debugLog("waiting for change password response");
     542           0 :           Future.delayed(const Duration(milliseconds: 500), () {
     543           0 :             if (globalErrorHandler.changePasswordError) {
     544             :               // TODO: This isn't ideal, but because onChange can be fired during this future check
     545             :               // and because the context can change after being popped we have this kind of double assertion...
     546             :               // There is probably a better pattern to handle this...
     547           0 :               if (AppLocalizations.of(context) != null) {
     548           0 :                 final snackBar = SnackBar(content: Text(AppLocalizations.of(context)!.passwordChangeError));
     549           0 :                 ScaffoldMessenger.of(context).showSnackBar(snackBar);
     550             :                 return;
     551             :               }
     552             :             }
     553           0 :           }).whenComplete(() {
     554           0 :             if (globalErrorHandler.explicitChangePasswordSuccess) {
     555             :               // we need to set the local encrypted status to display correct password forms on this run...
     556           0 :               Provider.of<ProfileInfoState>(context, listen: false).isEncrypted = true;
     557           0 :               final snackBar = SnackBar(content: Text(AppLocalizations.of(context)!.newPassword));
     558           0 :               ScaffoldMessenger.of(context).showSnackBar(snackBar);
     559           0 :               Navigator.pop(context);
     560             :               return; // otherwise round and round we go...
     561             :             }
     562             :           });
     563             :         }
     564             :       }
     565             :     }
     566             :   }
     567             : 
     568           0 :   showAlertDialog(BuildContext context) {
     569             :     // set up the buttons
     570           0 :     Widget cancelButton = ElevatedButton(
     571           0 :       child: Text(AppLocalizations.of(context)!.cancel),
     572           0 :       onPressed: () {
     573           0 :         Navigator.of(context).pop(); // dismiss dialog
     574             :       },
     575             :     );
     576             :     // For #939: provide an alert that a password must be provided to delete a password-protected profile.
     577           0 :     Widget errorAcknowledgeButton = ElevatedButton(
     578           0 :       child: Text(AppLocalizations.of(context)!.cancel),
     579           0 :       onPressed: () {
     580           0 :         Navigator.of(context)
     581           0 :           ..pop()
     582           0 :           ..pop(); // dismiss dialog
     583             :       },
     584             :     );
     585           0 :     AlertDialog passwordRequiredToDeleteAlert = AlertDialog(title: Text(AppLocalizations.of(context)!.enterCurrentPasswordForDelete), actions: [errorAcknowledgeButton]);
     586           0 :     Widget continueButton = ElevatedButton(
     587           0 :       child: Text(AppLocalizations.of(context)!.deleteProfileConfirmBtn),
     588           0 :       onPressed: () {
     589           0 :         var onion = Provider.of<ProfileInfoState>(context, listen: false).onion;
     590           0 :         Provider.of<FlwtchState>(context, listen: false).cwtch.DeleteProfile(onion, ctrlrOldPass.value.text);
     591             : 
     592           0 :         Future.delayed(const Duration(milliseconds: 500), () {
     593           0 :           if (globalErrorHandler.deleteProfileSuccess) {
     594           0 :             final snackBar = SnackBar(content: Text(AppLocalizations.of(context)!.deleteProfileSuccess + ":" + onion));
     595           0 :             ScaffoldMessenger.of(context).showSnackBar(snackBar);
     596           0 :             Navigator.of(context).popUntil((route) => route.isFirst); // dismiss dialog
     597             :           } else {
     598           0 :             showDialog(
     599             :               context: context,
     600           0 :               builder: (BuildContext context) {
     601             :                 return passwordRequiredToDeleteAlert;
     602             :               },
     603             :             );
     604             :           }
     605             :         });
     606             :       },
     607             :     );
     608             : 
     609             :     // set up the AlertDialog
     610           0 :     AlertDialog alert = AlertDialog(title: Text(AppLocalizations.of(context)!.deleteProfileConfirmBtn), actions: [cancelButton, continueButton]);
     611             : 
     612             :     // show the dialog
     613           0 :     showDialog(
     614             :       context: context,
     615           0 :       builder: (BuildContext context) {
     616             :         return alert;
     617             :       },
     618             :     );
     619             :   }
     620             : }

Generated by: LCOV version 1.14