ente/lib/ui/share_collection_widget.dart

318 lines
9.5 KiB
Dart
Raw Normal View History

2020-08-07 10:21:56 +00:00
import 'package:flutter/cupertino.dart';
2020-05-17 12:39:38 +00:00
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
import 'package:fluttercontactpicker/fluttercontactpicker.dart';
2020-10-31 13:17:17 +00:00
import 'package:logging/logging.dart';
import 'package:photos/core/configuration.dart';
import 'package:photos/db/public_keys_db.dart';
2020-10-09 23:51:20 +00:00
import 'package:photos/models/collection.dart';
import 'package:photos/models/public_key.dart';
2020-10-09 23:51:20 +00:00
import 'package:photos/services/collections_service.dart';
import 'package:photos/services/user_service.dart';
import 'package:photos/ui/common_elements.dart';
2020-05-17 12:39:38 +00:00
import 'package:photos/ui/loading_widget.dart';
import 'package:photos/ui/payment/subscription.dart';
2020-08-07 10:21:56 +00:00
import 'package:photos/utils/dialog_util.dart';
2020-10-09 23:51:20 +00:00
import 'package:photos/utils/email_util.dart';
import 'package:photos/utils/share_util.dart';
2020-07-07 21:46:14 +00:00
import 'package:photos/utils/toast_util.dart';
2021-08-17 08:34:56 +00:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2020-05-17 12:39:38 +00:00
2020-10-13 06:23:45 +00:00
class SharingDialog extends StatefulWidget {
final Collection collection;
2020-12-03 22:41:01 +00:00
SharingDialog(this.collection, {Key key}) : super(key: key);
2020-10-13 06:23:45 +00:00
@override
_SharingDialogState createState() => _SharingDialogState();
}
class _SharingDialogState extends State<SharingDialog> {
bool _showEntryField = false;
2020-11-02 14:38:59 +00:00
List<User> _sharees;
2020-10-13 06:23:45 +00:00
String _email;
final Logger _logger = Logger("SharingDialogState");
2020-10-13 06:23:45 +00:00
@override
Widget build(BuildContext context) {
2020-12-03 22:41:01 +00:00
_sharees = widget.collection.sharees;
final children = <Widget>[];
if (!_showEntryField && _sharees.isEmpty) {
_showEntryField = true;
2020-10-09 23:51:20 +00:00
} else {
2020-11-02 14:38:59 +00:00
for (final user in _sharees) {
2021-03-21 11:21:45 +00:00
children.add(EmailItemWidget(widget.collection, user.email));
2020-10-09 23:51:20 +00:00
}
2020-08-07 10:21:56 +00:00
}
if (_showEntryField) {
children.add(_getEmailField());
2020-08-07 10:21:56 +00:00
}
children.add(Padding(
2021-07-28 12:20:27 +00:00
padding: EdgeInsets.all(16),
2020-08-07 10:21:56 +00:00
));
2020-10-09 23:51:20 +00:00
if (!_showEntryField) {
2021-07-28 12:20:27 +00:00
children.add(SizedBox(
2020-10-09 23:51:20 +00:00
width: 220,
child: OutlineButton(
child: Icon(
Icons.add,
),
onPressed: () {
setState(() {
_showEntryField = true;
});
},
2020-05-17 12:39:38 +00:00
),
2020-10-09 23:51:20 +00:00
));
} else {
2021-07-28 12:20:27 +00:00
children.add(
SizedBox(
width: 240,
height: 50,
child: button(
"add",
onPressed: () {
_addEmailToCollection(_email.trim());
},
),
2020-10-09 23:51:20 +00:00
),
2021-07-28 12:20:27 +00:00
);
2020-10-09 23:51:20 +00:00
}
2020-10-13 05:22:20 +00:00
return AlertDialog(
2021-03-15 14:15:19 +00:00
title: Text("sharing"),
2020-10-13 05:22:20 +00:00
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(4.0),
child: Column(
children: children,
)),
],
),
),
);
2020-10-09 23:51:20 +00:00
}
Widget _getEmailField() {
2021-07-28 12:20:27 +00:00
return Row(
children: [
Expanded(
child: TypeAheadField(
textFieldConfiguration: TextFieldConfiguration(
keyboardType: TextInputType.emailAddress,
autofocus: true,
decoration: InputDecoration(
border: InputBorder.none,
hintText: "email@your-friend.com",
),
),
2021-07-28 12:20:27 +00:00
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);
},
),
2021-07-28 12:20:27 +00:00
),
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);
},
),
],
);
}
Future<void> _addEmailToCollection(
String email, {
String publicKey,
}) async {
if (!isValidEmail(email)) {
2021-01-08 17:06:52 +00:00
showErrorDialog(context, "invalid email address",
"please enter a valid email address.");
return;
} else if (email == Configuration.instance.getEmail()) {
2021-08-17 08:34:56 +00:00
showErrorDialog(context, AppLocalizations.of(context).oops,
"you cannot share with yourself");
2020-10-09 23:51:20 +00:00
return;
} else if (widget.collection.sharees.any((user) => user.email == email)) {
2021-08-17 08:34:56 +00:00
showErrorDialog(context, AppLocalizations.of(context).oops,
"you're already sharing this with " + email);
return;
2020-10-09 23:51:20 +00:00
}
if (publicKey == null) {
2021-01-08 17:02:41 +00:00
final dialog = createProgressDialog(context, "searching for user...");
await dialog.show();
2020-10-29 12:56:30 +00:00
publicKey = await UserService.instance.getPublicKey(email);
await dialog.hide();
}
2020-10-09 23:51:20 +00:00
if (publicKey == null) {
2021-03-21 11:21:45 +00:00
Navigator.of(context, rootNavigator: true).pop('dialog');
2020-10-09 23:51:20 +00:00
final dialog = AlertDialog(
2021-04-06 00:20:54 +00:00
title: Text("invite to ente?"),
2021-07-28 12:20:27 +00:00
content: Text(
"looks like " +
email +
" hasn't signed up for ente yet. would you like to invite them?",
style: TextStyle(
height: 1.4,
),
),
2020-10-09 23:51:20 +00:00
actions: [
2021-07-28 12:20:27 +00:00
TextButton(
child: Text(
"invite",
style: TextStyle(
color: Theme.of(context).buttonColor,
),
),
2020-10-09 23:51:20 +00:00
onPressed: () {
shareText(
2021-04-06 00:20:54 +00:00
"Hey, I have some photos to share. Please install https://ente.io so that I can share them privately.");
2020-10-09 23:51:20 +00:00
},
),
],
);
showDialog(
context: context,
builder: (BuildContext context) {
return dialog;
2020-08-07 10:21:56 +00:00
},
2020-10-09 23:51:20 +00:00
);
} else {
2021-01-08 17:02:41 +00:00
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);
}
}
2020-10-29 12:56:30 +00:00
await CollectionsService.instance
.share(widget.collection.id, email, publicKey);
await dialog.hide();
2021-01-08 17:11:32 +00:00
showToast("shared successfully!");
2020-10-13 06:23:45 +00:00
setState(() {
2020-11-02 14:38:59 +00:00
_sharees.add(User(email: email));
2020-10-13 06:23:45 +00:00
_showEntryField = false;
});
} catch (e) {
await dialog.hide();
if (e is SharingNotPermittedForFreeAccountsError) {
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: () {
2021-03-21 11:21:45 +00:00
Navigator.of(context, rootNavigator: true).pop();
},
),
],
);
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
} else {
_logger.severe("failed to share collection", e);
showGenericErrorDialog(context);
}
}
2020-10-09 23:51:20 +00:00
}
2020-08-07 10:21:56 +00:00
}
}
class EmailItemWidget extends StatelessWidget {
2021-03-21 11:21:45 +00:00
final Collection collection;
2020-08-07 10:21:56 +00:00
final String email;
2020-10-31 13:17:17 +00:00
2020-08-07 10:21:56 +00:00
const EmailItemWidget(
2021-03-21 11:21:45 +00:00
this.collection,
2020-08-07 10:21:56 +00:00
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 {
2021-01-08 17:02:41 +00:00
final dialog = createProgressDialog(context, "please wait...");
await dialog.show();
try {
2021-03-21 11:21:45 +00:00
await CollectionsService.instance.unshare(collection.id, email);
collection.sharees.removeWhere((user) => user.email == email);
await dialog.hide();
2021-01-08 17:11:32 +00:00
showToast("stopped sharing with " + email + ".");
Navigator.of(context).pop();
} catch (e, s) {
Logger("EmailItemWidget").severe(e, s);
await dialog.hide();
showGenericErrorDialog(context);
}
},
),
],
);
2020-05-17 12:39:38 +00:00
}
}