ente/lib/ui/sharing/share_collection_widget.dart

510 lines
16 KiB
Dart
Raw Normal View History

// @dart=2.9
import 'dart:ui';
import 'package:fast_base58/fast_base58.dart';
2020-05-17 12:39:38 +00:00
import 'package:flutter/material.dart';
import 'package:flutter/services.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';
2022-07-12 06:30:02 +00:00
import 'package:photos/ente_theme_data.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/feature_flag_service.dart';
2020-10-09 23:51:20 +00:00
import 'package:photos/services/user_service.dart';
import 'package:photos/ui/common/dialogs.dart';
2022-07-03 10:09:01 +00:00
import 'package:photos/ui/common/gradient_button.dart';
import 'package:photos/ui/common/loading_widget.dart';
import 'package:photos/ui/payment/subscription.dart';
2022-07-01 14:39:02 +00:00
import 'package:photos/ui/sharing/manage_links_widget.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';
2022-02-21 02:13:10 +00:00
import 'package:photos/utils/navigation_util.dart';
2020-10-09 23:51:20 +00:00
import 'package:photos/utils/share_util.dart';
2020-07-07 21:46:14 +00:00
import 'package:photos/utils/toast_util.dart';
2020-05-17 12:39:38 +00:00
2020-10-13 06:23:45 +00:00
class SharingDialog extends StatefulWidget {
final Collection collection;
const SharingDialog(this.collection, {Key key}) : super(key: key);
2020-10-13 06:23:45 +00:00
@override
2022-07-03 09:45:00 +00:00
State<SharingDialog> createState() => _SharingDialogState();
2020-10-13 06:23:45 +00:00
}
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
}
2022-06-11 08:23:52 +00:00
children.add(
2022-07-04 06:02:17 +00:00
const Padding(
2022-06-11 08:23:52 +00:00
padding: EdgeInsets.all(8),
),
);
2020-10-09 23:51:20 +00:00
if (!_showEntryField) {
children.add(
SizedBox(
width: 220,
child: GradientButton(
2022-07-03 09:45:00 +00:00
onTap: () async {
setState(() {
_showEntryField = true;
});
},
2022-07-04 04:45:32 +00:00
iconData: Icons.add,
2020-10-09 23:51:20 +00:00
),
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,
2022-05-16 20:38:11 +00:00
child: OutlinedButton(
2022-07-04 06:02:17 +00:00
child: const Text("Add"),
2021-07-28 12:20:27 +00:00
onPressed: () {
2022-02-07 16:02:10 +00:00
_addEmailToCollection(_email?.trim() ?? '');
2021-07-28 12:20:27 +00:00
},
),
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
if (!FeatureFlagService.instance.disableUrlSharing()) {
2022-08-29 14:43:31 +00:00
final bool hasUrl = widget.collection.publicURLs?.isNotEmpty ?? false;
children.addAll([
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(16)),
const Divider(height: 1),
const Padding(padding: EdgeInsets.all(12)),
SizedBox(
height: 36,
child: Row(
2022-01-24 18:10:57 +00:00
mainAxisAlignment: MainAxisAlignment.center,
children: [
2022-07-04 06:02:17 +00:00
const Text("Public link"),
Switch(
value: hasUrl,
onChanged: (enable) async {
// confirm if user wants to disable the url
if (!enable) {
2022-01-24 18:10:57 +00:00
final choice = await showChoiceDialog(
2022-06-11 08:23:52 +00:00
context,
'Disable link',
'Are you sure that you want to disable the album link?',
firstAction: 'Yes, disable',
secondAction: 'No',
actionType: ActionType.critical,
);
if (choice != DialogUserChoice.firstChoice) {
return;
}
}
2022-06-11 08:23:52 +00:00
final dialog = createProgressDialog(
context,
enable ? "Creating link..." : "Disabling link...",
);
try {
await dialog.show();
enable
2022-07-03 09:49:33 +00:00
? 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);
}
2022-01-24 10:49:47 +00:00
}
},
),
],
),
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(8)),
]);
if (widget.collection.publicURLs?.isNotEmpty ?? false) {
2022-06-11 08:23:52 +00:00
children.add(
2022-07-04 06:02:17 +00:00
const Padding(
2022-06-11 08:23:52 +00:00
padding: EdgeInsets.all(2),
),
);
2022-02-21 02:13:10 +00:00
children.add(_getShareableUrlWidget(context));
}
}
2020-10-13 05:22:20 +00:00
return AlertDialog(
2022-07-04 06:02:17 +00:00
title: const Text("Sharing"),
2020-10-13 05:22:20 +00:00
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Padding(
2022-06-11 08:23:52 +00:00
padding: const EdgeInsets.all(4.0),
child: Column(
children: children,
),
),
2020-10-13 05:22:20 +00:00
],
),
),
2022-07-04 06:02:17 +00:00
contentPadding: const EdgeInsets.fromLTRB(24, 24, 24, 4),
2020-10-13 05:22:20 +00:00
);
2020-10-09 23:51:20 +00:00
}
Widget _getEmailField() {
2021-07-28 12:20:27 +00:00
return Row(
children: [
Expanded(
child: TypeAheadField(
2022-07-04 06:02:17 +00:00
textFieldConfiguration: const TextFieldConfiguration(
2021-07-28 12:20:27 +00:00
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
border: InputBorder.none,
hintText: "email@your-friend.com",
),
),
2021-07-28 12:20:27 +00:00
hideOnEmpty: true,
loadingBuilder: (context) {
return const EnteLoadingWidget();
2021-07-28 12:20:27 +00:00
},
suggestionsCallback: (pattern) async {
_email = pattern;
return PublicKeysDB.instance.searchByEmail(_email);
},
itemBuilder: (context, suggestion) {
return Container(
2022-07-04 06:02:17 +00:00
padding: const EdgeInsets.fromLTRB(12, 8, 12, 8),
2021-07-28 12:20:27 +00:00
child: Text(
suggestion.email,
overflow: TextOverflow.clip,
),
);
},
onSuggestionSelected: (PublicKey suggestion) {
2022-06-11 08:23:52 +00:00
_addEmailToCollection(
suggestion.email,
publicKey: suggestion.publicKey,
);
2021-07-28 12:20:27 +00:00
},
),
2021-07-28 12:20:27 +00:00
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(8)),
2021-07-28 12:20:27 +00:00
IconButton(
icon: Icon(
Icons.contact_mail_outlined,
2022-07-12 06:30:02 +00:00
color:
Theme.of(context).colorScheme.greenAlternative.withOpacity(0.8),
2021-07-28 12:20:27 +00:00
),
onPressed: () async {
final emailContact = await FlutterContactPicker.pickEmailContact(
2022-06-11 08:23:52 +00:00
askForPermission: true,
);
2021-07-28 12:20:27 +00:00
_addEmailToCollection(emailContact.email.email);
},
),
],
);
}
2022-02-21 02:13:10 +00:00
Widget _getShareableUrlWidget(BuildContext parentContext) {
2022-08-29 14:43:31 +00:00
final String collectionKey = Base58Encode(
2022-06-11 08:23:52 +00:00
CollectionsService.instance.getCollectionKey(widget.collection.id),
);
final String url =
"${widget.collection.publicURLs.first.url}#$collectionKey";
return SingleChildScrollView(
child: Column(
2022-02-26 11:47:35 +00:00
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(4)),
2022-02-26 11:47:35 +00:00
GestureDetector(
onTap: () async {
await Clipboard.setData(ClipboardData(text: url));
2022-06-10 14:29:56 +00:00
showToast(context, "Link copied to clipboard");
2022-02-26 11:47:35 +00:00
},
child: Container(
2022-07-04 06:02:17 +00:00
padding: const EdgeInsets.all(16),
2022-07-03 09:45:00 +00:00
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.02),
2022-01-24 18:10:57 +00:00
child: Row(
2022-02-26 11:47:35 +00:00
crossAxisAlignment: CrossAxisAlignment.end,
2022-01-24 18:10:57 +00:00
children: [
2022-02-26 11:47:35 +00:00
Flexible(
child: Text(
url,
style: TextStyle(
fontSize: 16,
fontFeatures: const [FontFeature.tabularFigures()],
2022-07-03 09:49:33 +00:00
color: Theme.of(context)
.colorScheme
.onSurface
.withOpacity(0.68),
2022-02-26 11:47:35 +00:00
overflow: TextOverflow.ellipsis,
),
2022-01-24 18:10:57 +00:00
),
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(2)),
const Icon(
2022-02-26 11:47:35 +00:00
Icons.copy,
size: 18,
),
2022-01-24 18:10:57 +00:00
],
2022-01-24 10:45:56 +00:00
),
),
2022-02-26 11:47:35 +00:00
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(2)),
2022-02-26 11:47:35 +00:00
TextButton(
2022-02-26 13:06:03 +00:00
child: Padding(
padding: const EdgeInsets.all(12),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.adaptive.share,
2022-07-12 06:30:02 +00:00
color: Theme.of(context).colorScheme.greenAlternative,
2022-02-21 02:13:10 +00:00
),
2022-07-04 06:02:17 +00:00
const Padding(
2022-02-26 13:06:03 +00:00
padding: EdgeInsets.all(4),
),
Text(
2022-05-16 20:38:11 +00:00
"Share link",
2022-02-26 13:06:03 +00:00
style: TextStyle(
2022-07-12 06:30:02 +00:00
color: Theme.of(context).colorScheme.greenAlternative,
2022-02-26 13:06:03 +00:00
),
),
],
),
2022-02-26 11:47:35 +00:00
),
onPressed: () {
shareText(url);
},
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(4)),
2022-02-26 11:47:35 +00:00
TextButton(
child: Center(
child: Text(
2022-05-16 20:38:11 +00:00
"Manage link",
2022-02-26 11:47:35 +00:00
style: TextStyle(
2022-05-16 20:38:11 +00:00
color: Theme.of(context).primaryColorLight,
2022-02-26 11:47:35 +00:00
decoration: TextDecoration.underline,
),
2022-02-21 02:13:10 +00:00
),
),
2022-02-26 11:47:35 +00:00
onPressed: () async {
routeToPage(
parentContext,
ManageSharedLinkWidget(collection: widget.collection),
);
},
),
],
),
);
}
Future<void> _addEmailToCollection(
String email, {
String publicKey,
}) async {
if (!isValidEmail(email)) {
2022-06-11 08:23:52 +00:00
showErrorDialog(
context,
"Invalid email address",
"Please enter a valid email address.",
);
return;
} else if (email == Configuration.instance.getEmail()) {
2022-05-17 11:38:21 +00:00
showErrorDialog(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)) {
showErrorDialog(
2022-06-11 08:23:52 +00:00
context,
"Oops",
"You're already sharing this with " + email,
);
return;
2020-10-09 23:51:20 +00:00
}
if (publicKey == null) {
2022-05-17 11:38:21 +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(
2022-07-04 06:02:17 +00:00
title: const Text("Invite to ente?"),
2021-07-28 12:20:27 +00:00
content: Text(
2022-07-03 09:49:33 +00:00
"Looks like " +
email +
" hasn't signed up for ente yet. would you like to invite them?",
2022-07-04 06:02:17 +00:00
style: const TextStyle(
2021-07-28 12:20:27 +00:00
height: 1.4,
),
),
2020-10-09 23:51:20 +00:00
actions: [
2021-07-28 12:20:27 +00:00
TextButton(
child: Text(
2022-05-17 11:38:21 +00:00
"Invite",
2021-07-28 12:20:27 +00:00
style: TextStyle(
2022-07-12 06:30:02 +00:00
color: Theme.of(context).colorScheme.greenAlternative,
2021-07-28 12:20:27 +00:00
),
),
2020-10-09 23:51:20 +00:00
onPressed: () {
shareText(
2022-06-11 08:23:52 +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 {
2022-05-17 11:38:21 +00:00
final dialog = createProgressDialog(context, "Sharing...");
await dialog.show();
try {
2022-07-03 09:49:33 +00:00
await CollectionsService.instance
.share(widget.collection.id, email, publicKey);
await dialog.hide();
2022-06-10 14:29:56 +00:00
showShortToast(context, "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) {
2022-01-24 10:49:47 +00:00
_showUnSupportedAlert();
} 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
}
2022-01-24 10:49:47 +00:00
void _showUnSupportedAlert() {
2022-08-29 14:43:31 +00:00
final AlertDialog alert = AlertDialog(
2022-07-04 06:02:17 +00:00
title: const Text("Sorry"),
content: const Text(
"Sharing is not permitted for free accounts, please subscribe",
),
2022-01-24 10:49:47 +00:00
actions: [
TextButton(
2022-02-05 18:31:59 +00:00
child: Text(
2022-05-03 11:40:45 +00:00
"Subscribe",
2022-02-05 18:31:59 +00:00
style: TextStyle(
2022-07-12 06:30:02 +00:00
color: Theme.of(context).colorScheme.greenAlternative,
2022-02-05 18:31:59 +00:00
),
),
2022-01-24 10:49:47 +00:00
onPressed: () {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (BuildContext context) {
return getSubscriptionPage();
},
),
);
},
),
TextButton(
2022-02-05 18:31:59 +00:00
child: Text(
2022-05-16 20:38:11 +00:00
"Ok",
2022-02-05 18:31:59 +00:00
style: TextStyle(
2022-05-16 20:38:11 +00:00
color: Theme.of(context).colorScheme.onSurface,
2022-02-05 18:31:59 +00:00
),
),
2022-01-24 10:49:47 +00:00
onPressed: () {
Navigator.of(context, rootNavigator: true).pop();
},
),
],
);
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
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,
2022-07-04 06:02:17 +00:00
style: const TextStyle(fontSize: 16),
),
),
2022-07-04 06:02:17 +00:00
const Expanded(child: SizedBox()),
IconButton(
2022-07-04 06:02:17 +00:00
icon: const Icon(Icons.delete_forever),
color: Colors.redAccent,
onPressed: () async {
2022-05-16 20:38:11 +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();
2022-06-10 14:29:56 +00:00
showToast(context, "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
}
}