import 'dart:io'; import 'dart:ui'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_typeahead/flutter_typeahead.dart'; import 'package:fluttercontactpicker/fluttercontactpicker.dart'; import 'package:logging/logging.dart'; import 'package:photos/core/configuration.dart'; import 'package:photos/db/public_keys_db.dart'; import 'package:photos/models/collection.dart'; import 'package:photos/models/public_key.dart'; import 'package:photos/services/collections_service.dart'; import 'package:photos/services/feature_flag_service.dart'; import 'package:photos/services/user_service.dart'; import 'package:photos/ui/common/dialogs.dart'; import 'package:photos/ui/common_elements.dart'; import 'package:photos/ui/loading_widget.dart'; import 'package:photos/ui/payment/subscription.dart'; import 'package:photos/utils/dialog_util.dart'; import 'package:photos/utils/email_util.dart'; import 'package:photos/utils/hex.dart'; import 'package:photos/utils/share_util.dart'; import 'package:photos/utils/toast_util.dart'; class SharingDialog extends StatefulWidget { final Collection collection; SharingDialog(this.collection, {Key key}) : super(key: key); @override _SharingDialogState createState() => _SharingDialogState(); } class _SharingDialogState extends State { bool _showEntryField = false; List _sharees; String _email; final Logger _logger = Logger("SharingDialogState"); @override Widget build(BuildContext context) { _sharees = widget.collection.sharees; final children = []; if (!_showEntryField && _sharees.isEmpty) { _showEntryField = true; } else { for (final user in _sharees) { children.add(EmailItemWidget(widget.collection, user.email)); } } if (_showEntryField) { children.add(_getEmailField()); } children.add(Padding( padding: EdgeInsets.all(16), )); if (!_showEntryField) { children.add(SizedBox( width: 220, child: OutlineButton( child: Icon( Icons.add, ), onPressed: () { setState(() { _showEntryField = true; }); }, ), )); } else { children.add( SizedBox( width: 240, height: 50, child: button( "add", onPressed: () { _addEmailToCollection(_email.trim()); }, ), ), ); } if (!FeatureFlagService.instance.disableUrlSharing()) { bool hasUrl = widget.collection.publicURLs?.isNotEmpty ?? false; children.addAll([ Padding(padding: EdgeInsets.all(2)), Divider(height: 12), Padding(padding: EdgeInsets.all(Platform.isIOS ? 2 : 4)), SizedBox( height: 36, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text("enable public url"), Switch( value: hasUrl, onChanged: (enable) async { // confirm if user wants to disable the url if (!enable) { var choice = await showChoiceDialog(context, 'disable url', 'are you sure you want to disable?', firstAction: 'yes, disable', secondAction: 'no', actionType: ActionType.critical); if (choice != DialogUserChoice.firstChoice) { return; } } final dialog = createProgressDialog( context, enable ? "generating url..." : "disabling url"); try { await dialog.show(); enable ? await CollectionsService.instance .createShareUrl(widget.collection) : await CollectionsService.instance .disableShareUrl(widget.collection); dialog.hide(); setState(() {}); } catch (e) { dialog.hide(); if (e is SharingNotPermittedForFreeAccountsError) { _showUnSupportedAlert(); } else { _logger.severe("failed to share collection", e); showGenericErrorDialog(context); } } }, ), ], ), ), ]); if (widget.collection.publicURLs?.isNotEmpty ?? false) { children.add(_getShareableUrlWidget()); } } return AlertDialog( title: Text("sharing"), content: SingleChildScrollView( child: ListBody( children: [ Padding( padding: const EdgeInsets.all(4.0), child: Column( children: children, )), ], ), ), ); } Widget _getEmailField() { return Row( children: [ Expanded( child: TypeAheadField( textFieldConfiguration: TextFieldConfiguration( keyboardType: TextInputType.emailAddress, autofocus: true, decoration: InputDecoration( border: InputBorder.none, hintText: "email@your-friend.com", ), ), hideOnEmpty: true, loadingBuilder: (context) { return loadWidget; }, suggestionsCallback: (pattern) async { _email = pattern; return PublicKeysDB.instance.searchByEmail(_email); }, itemBuilder: (context, suggestion) { return Container( padding: EdgeInsets.fromLTRB(12, 8, 12, 8), child: Text( suggestion.email, overflow: TextOverflow.clip, ), ); }, onSuggestionSelected: (PublicKey suggestion) { _addEmailToCollection(suggestion.email, publicKey: suggestion.publicKey); }, ), ), Padding(padding: EdgeInsets.all(8)), IconButton( icon: Icon( Icons.contact_mail_outlined, color: Theme.of(context).buttonColor.withOpacity(0.8), ), onPressed: () async { final emailContact = await FlutterContactPicker.pickEmailContact( askForPermission: true); _addEmailToCollection(emailContact.email.email); }, ), ], ); } Widget _getShareableUrlWidget() { var hexEncoder = HexEncoder(upperCase: false); String base64urlEncode = hexEncoder.convert( CollectionsService.instance.getCollectionKey(widget.collection.id)); String url = "${widget.collection.publicURLs.first.url}#$base64urlEncode"; return SingleChildScrollView( child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ GestureDetector( onTap: () async { await Clipboard.setData(ClipboardData(text: url)); showToast("URL is copied to clipboard"); }, child: Container( padding: EdgeInsets.all(16), child: Center( child: Text( url, style: TextStyle( fontSize: 16, fontFeatures: const [FontFeature.tabularFigures()], color: Colors.white.withOpacity(0.7), ), ), ), color: Colors.white.withOpacity(0.1), ), ), Padding(padding: EdgeInsets.all(2)), TextButton( child: Text( "share url", style: TextStyle( color: Theme.of(context).buttonColor, ), ), onPressed: () { shareText(url); // _shareRecoveryKey(recoveryKey); }, ), ]), ); } Future _addEmailToCollection( String email, { String publicKey, }) async { if (!isValidEmail(email)) { showErrorDialog(context, "invalid email address", "please enter a valid email address."); return; } else if (email == Configuration.instance.getEmail()) { showErrorDialog(context, AppLocalizations.of(context).oops, "you cannot share with yourself"); return; } else if (widget.collection.sharees.any((user) => user.email == email)) { showErrorDialog(context, AppLocalizations.of(context).oops, "you're already sharing this with " + email); return; } if (publicKey == null) { final dialog = createProgressDialog(context, "searching for user..."); await dialog.show(); publicKey = await UserService.instance.getPublicKey(email); await dialog.hide(); } if (publicKey == null) { Navigator.of(context, rootNavigator: true).pop('dialog'); final dialog = AlertDialog( title: Text("invite to ente?"), content: Text( "looks like " + email + " hasn't signed up for ente yet. would you like to invite them?", style: TextStyle( height: 1.4, ), ), actions: [ TextButton( child: Text( "invite", style: TextStyle( color: Theme.of(context).buttonColor, ), ), onPressed: () { shareText( "Hey, I have some photos to share. Please install https://ente.io so that I can share them privately."); }, ), ], ); showDialog( context: context, builder: (BuildContext context) { return dialog; }, ); } else { final dialog = createProgressDialog(context, "sharing..."); await dialog.show(); final collection = widget.collection; try { if (collection.type == CollectionType.folder) { final path = CollectionsService.instance.decryptCollectionPath(collection); if (!Configuration.instance.getPathsToBackUp().contains(path)) { await Configuration.instance.addPathToFoldersToBeBackedUp(path); } } await CollectionsService.instance .share(widget.collection.id, email, publicKey); await dialog.hide(); showToast("shared successfully!"); setState(() { _sharees.add(User(email: email)); _showEntryField = false; }); } catch (e) { await dialog.hide(); if (e is SharingNotPermittedForFreeAccountsError) { _showUnSupportedAlert(); } else { _logger.severe("failed to share collection", e); showGenericErrorDialog(context); } } } } void _showUnSupportedAlert() { AlertDialog alert = AlertDialog( title: Text("sorry"), content: Text("sharing is not permitted for free accounts, please subscribe"), actions: [ TextButton( child: Text("subscribe"), onPressed: () { Navigator.of(context).pushReplacement( MaterialPageRoute( builder: (BuildContext context) { return getSubscriptionPage(); }, ), ); }, ), TextButton( child: Text("ok"), onPressed: () { Navigator.of(context, rootNavigator: true).pop(); }, ), ], ); showDialog( context: context, builder: (BuildContext context) { return alert; }, ); } } class EmailItemWidget extends StatelessWidget { final Collection collection; final String email; const EmailItemWidget( this.collection, this.email, { Key key, }) : super(key: key); @override Widget build(BuildContext context) { return Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Padding( padding: const EdgeInsets.fromLTRB(8, 0, 0, 0), child: Text( email, style: TextStyle(fontSize: 16), ), ), Expanded(child: SizedBox()), IconButton( icon: Icon(Icons.delete_forever), color: Colors.redAccent, onPressed: () async { final dialog = createProgressDialog(context, "please wait..."); await dialog.show(); try { await CollectionsService.instance.unshare(collection.id, email); collection.sharees.removeWhere((user) => user.email == email); await dialog.hide(); showToast("stopped sharing with " + email + "."); Navigator.of(context).pop(); } catch (e, s) { Logger("EmailItemWidget").severe(e, s); await dialog.hide(); showGenericErrorDialog(context); } }, ), ], ); } }