ente/lib/ui/sharing/share_collection_widget.dart

476 lines
14 KiB
Dart
Raw Normal View History

// @dart=2.9
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';
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/user_service.dart';
import 'package:photos/theme/colors.dart';
import 'package:photos/theme/ente_theme.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/components/captioned_text_widget.dart';
import 'package:photos/ui/components/divider_widget.dart';
import 'package:photos/ui/components/menu_item_widget.dart';
import 'package:photos/ui/components/menu_section_title.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
2022-09-15 05:34:22 +00:00
final bool hasUrl = widget.collection.publicURLs?.isNotEmpty ?? false;
children.addAll([
const SizedBox(
height: 24,
),
MenuSectionTitle(
title: hasUrl ? "Public link enabled" : "Share a public link",
iconData: Icons.public,
),
2022-09-15 05:34:22 +00:00
]);
if (hasUrl) {
final String collectionKey = Base58Encode(
CollectionsService.instance.getCollectionKey(widget.collection.id),
);
final String url =
"${widget.collection.publicURLs.first.url}#$collectionKey";
children.addAll(
[
MenuItemWidget(
captionedTextWidget: const CaptionedTextWidget(
title: "Copy link",
),
leadingIcon: Icons.copy,
menuItemColor: getEnteColorScheme(context).fillFaint,
pressedColor: getEnteColorScheme(context).fillFaint,
onTap: () async {
await Clipboard.setData(ClipboardData(text: url));
showToast(context, "Link copied to clipboard");
},
isBottomBorderRadiusRemoved: true,
),
DividerWidget(
dividerType: DividerType.menu,
bgColor: getEnteColorScheme(context).blurStrokeFaint,
),
MenuItemWidget(
captionedTextWidget: const CaptionedTextWidget(
title: "Send link",
),
leadingIcon: Icons.adaptive.share,
menuItemColor: getEnteColorScheme(context).fillFaint,
pressedColor: getEnteColorScheme(context).fillFaint,
onTap: () async {
shareText(url);
},
isTopBorderRadiusRemoved: true,
),
DividerWidget(
dividerType: DividerType.menu,
bgColor: getEnteColorScheme(context).blurStrokeFaint,
),
MenuItemWidget(
captionedTextWidget: const CaptionedTextWidget(
title: "Manage link",
),
leadingIcon: Icons.link,
trailingIcon: Icons.navigate_next,
menuItemColor: getEnteColorScheme(context).fillFaint,
pressedColor: getEnteColorScheme(context).fillFaint,
onTap: () async {
routeToPage(
context,
ManageSharedLinkWidget(collection: widget.collection),
);
},
isTopBorderRadiusRemoved: true,
),
const SizedBox(
height: 24,
),
MenuItemWidget(
captionedTextWidget: const CaptionedTextWidget(
title: "Remove link",
textColor: warning500,
),
leadingIcon: Icons.remove_circle_outline,
leadingIconColor: warning500,
menuItemColor: getEnteColorScheme(context).fillFaint,
pressedColor: getEnteColorScheme(context).fillFaint,
onTap: () async {
await _publicLinkToggle(false);
},
),
],
);
} else {
2022-09-15 05:34:22 +00:00
children.add(
MenuItemWidget(
captionedTextWidget: const CaptionedTextWidget(
title: "Create public link",
),
leadingIcon: Icons.link,
menuItemColor: getEnteColorScheme(context).fillFaint,
pressedColor: getEnteColorScheme(context).fillFaint,
onTap: () async {
await _publicLinkToggle(true);
},
2022-09-15 05:34:22 +00:00
),
);
}
return Scaffold(
appBar: AppBar(title: const Text("Sharing")),
body: SingleChildScrollView(
2020-10-13 05:22:20 +00:00
child: ListBody(
children: <Widget>[
Padding(
padding:
const EdgeInsets.symmetric(vertical: 4.0, horizontal: 16),
2022-06-11 08:23:52 +00:00
child: Column(
children: children,
),
),
2020-10-13 05:22:20 +00:00
],
),
),
);
2020-10-09 23:51:20 +00:00
}
Future<void> _publicLinkToggle(bool enable) async {
// confirm if user wants to disable the url
if (!enable) {
final choice = await showChoiceDialog(
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;
}
}
final dialog = createProgressDialog(
context,
enable ? "Creating link..." : "Disabling link...",
);
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);
}
}
}
Widget _getEmailField() {
return Container(
padding: const EdgeInsets.fromLTRB(8, 0, 8, 0),
child: Row(
children: [
Expanded(
child: TypeAheadField(
textFieldConfiguration: const TextFieldConfiguration(
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
border: InputBorder.none,
hintText: "email@your-friend.com",
),
),
hideOnEmpty: true,
loadingBuilder: (context) {
return const EnteLoadingWidget();
},
suggestionsCallback: (pattern) async {
_email = pattern;
return PublicKeysDB.instance.searchByEmail(_email);
},
itemBuilder: (context, suggestion) {
return Container(
padding: const EdgeInsets.fromLTRB(12, 8, 12, 8),
child: Text(
suggestion.email,
overflow: TextOverflow.clip,
),
);
},
onSuggestionSelected: (PublicKey suggestion) {
_addEmailToCollection(
2021-07-28 12:20:27 +00:00
suggestion.email,
publicKey: suggestion.publicKey,
);
},
),
2021-07-28 12:20:27 +00:00
),
],
),
);
}
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
}
}