manage_links_widget.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. import 'dart:convert';
  2. import 'package:collection/collection.dart';
  3. import "package:fast_base58/fast_base58.dart";
  4. import 'package:flutter/material.dart';
  5. import "package:flutter/services.dart";
  6. import "package:photos/generated/l10n.dart";
  7. import "package:photos/models/api/collection/public_url.dart";
  8. import 'package:photos/models/collection/collection.dart';
  9. import 'package:photos/services/collections_service.dart';
  10. import 'package:photos/theme/colors.dart';
  11. import 'package:photos/theme/ente_theme.dart';
  12. import 'package:photos/ui/actions/collection/collection_sharing_actions.dart';
  13. import 'package:photos/ui/components/captioned_text_widget.dart';
  14. import 'package:photos/ui/components/divider_widget.dart';
  15. import 'package:photos/ui/components/menu_item_widget/menu_item_widget.dart';
  16. import 'package:photos/ui/components/menu_section_description_widget.dart';
  17. import 'package:photos/ui/sharing/pickers/device_limit_picker_page.dart';
  18. import 'package:photos/ui/sharing/pickers/link_expiry_picker_page.dart';
  19. import 'package:photos/utils/crypto_util.dart';
  20. import 'package:photos/utils/date_time_util.dart';
  21. import 'package:photos/utils/dialog_util.dart';
  22. import 'package:photos/utils/navigation_util.dart';
  23. import "package:photos/utils/share_util.dart";
  24. import 'package:photos/utils/toast_util.dart';
  25. class ManageSharedLinkWidget extends StatefulWidget {
  26. final Collection? collection;
  27. const ManageSharedLinkWidget({Key? key, this.collection}) : super(key: key);
  28. @override
  29. State<ManageSharedLinkWidget> createState() => _ManageSharedLinkWidgetState();
  30. }
  31. class _ManageSharedLinkWidgetState extends State<ManageSharedLinkWidget> {
  32. final CollectionActions sharingActions =
  33. CollectionActions(CollectionsService.instance);
  34. @override
  35. void initState() {
  36. super.initState();
  37. }
  38. @override
  39. Widget build(BuildContext context) {
  40. final isCollectEnabled =
  41. widget.collection!.publicURLs?.firstOrNull?.enableCollect ?? false;
  42. final isDownloadEnabled =
  43. widget.collection!.publicURLs?.firstOrNull?.enableDownload ?? true;
  44. final isPasswordEnabled =
  45. widget.collection!.publicURLs?.firstOrNull?.passwordEnabled ?? false;
  46. final enteColorScheme = getEnteColorScheme(context);
  47. final PublicURL url = widget.collection!.publicURLs!.firstOrNull!;
  48. final String collectionKey = Base58Encode(
  49. CollectionsService.instance.getCollectionKey(widget.collection!.id),
  50. );
  51. final String urlValue = "${url.url}#$collectionKey";
  52. return Scaffold(
  53. appBar: AppBar(
  54. elevation: 0,
  55. title: Text(
  56. S.of(context).manageLink,
  57. ),
  58. ),
  59. body: SingleChildScrollView(
  60. child: ListBody(
  61. children: <Widget>[
  62. Padding(
  63. padding: const EdgeInsets.fromLTRB(16, 12, 16, 12),
  64. child: Column(
  65. crossAxisAlignment: CrossAxisAlignment.start,
  66. children: [
  67. MenuItemWidget(
  68. key: ValueKey("Allow collect $isCollectEnabled"),
  69. captionedTextWidget: CaptionedTextWidget(
  70. title: S.of(context).allowAddingPhotos,
  71. ),
  72. alignCaptionedTextToLeft: true,
  73. menuItemColor: getEnteColorScheme(context).fillFaint,
  74. trailingWidget: Switch.adaptive(
  75. value: widget.collection!.publicURLs?.firstOrNull
  76. ?.enableCollect ??
  77. false,
  78. onChanged: (value) async {
  79. await _updateUrlSettings(
  80. context,
  81. {'enableCollect': value},
  82. );
  83. },
  84. ),
  85. ),
  86. MenuSectionDescriptionWidget(
  87. content: S.of(context).allowAddPhotosDescription,
  88. ),
  89. const SizedBox(height: 24),
  90. MenuItemWidget(
  91. alignCaptionedTextToLeft: true,
  92. captionedTextWidget: CaptionedTextWidget(
  93. title: S.of(context).linkExpiry,
  94. subTitle: (url.hasExpiry
  95. ? (url.isExpired
  96. ? S.of(context).linkExpired
  97. : S.of(context).linkEnabled)
  98. : S.of(context).linkNeverExpires),
  99. subTitleColor: url.isExpired ? warning500 : null,
  100. ),
  101. trailingIcon: Icons.chevron_right,
  102. menuItemColor: enteColorScheme.fillFaint,
  103. surfaceExecutionStates: false,
  104. onTap: () async {
  105. routeToPage(
  106. context,
  107. LinkExpiryPickerPage(widget.collection!),
  108. ).then((value) {
  109. setState(() {});
  110. });
  111. },
  112. ),
  113. url.hasExpiry
  114. ? MenuSectionDescriptionWidget(
  115. content: url.isExpired
  116. ? S.of(context).expiredLinkInfo
  117. : S.of(context).linkExpiresOn(
  118. getFormattedTime(
  119. context,
  120. DateTime.fromMicrosecondsSinceEpoch(
  121. url.validTill,
  122. ),
  123. ),
  124. ),
  125. )
  126. : const SizedBox.shrink(),
  127. const Padding(padding: EdgeInsets.only(top: 24)),
  128. MenuItemWidget(
  129. captionedTextWidget: CaptionedTextWidget(
  130. title: S.of(context).linkDeviceLimit,
  131. subTitle: url.deviceLimit == 0
  132. ? S.of(context).unlimited
  133. : "${url.deviceLimit}",
  134. ),
  135. trailingIcon: Icons.chevron_right,
  136. menuItemColor: enteColorScheme.fillFaint,
  137. alignCaptionedTextToLeft: true,
  138. isBottomBorderRadiusRemoved: true,
  139. onTap: () async {
  140. routeToPage(
  141. context,
  142. DeviceLimitPickerPage(widget.collection!),
  143. ).then((value) {
  144. setState(() {});
  145. });
  146. },
  147. surfaceExecutionStates: false,
  148. ),
  149. DividerWidget(
  150. dividerType: DividerType.menuNoIcon,
  151. bgColor: getEnteColorScheme(context).fillFaint,
  152. ),
  153. MenuItemWidget(
  154. key: ValueKey("Allow downloads $isDownloadEnabled"),
  155. captionedTextWidget: CaptionedTextWidget(
  156. title: S.of(context).allowDownloads,
  157. ),
  158. alignCaptionedTextToLeft: true,
  159. isBottomBorderRadiusRemoved: true,
  160. isTopBorderRadiusRemoved: true,
  161. menuItemColor: getEnteColorScheme(context).fillFaint,
  162. trailingWidget: Switch.adaptive(
  163. value: isDownloadEnabled,
  164. onChanged: (value) async {
  165. await _updateUrlSettings(
  166. context,
  167. {'enableDownload': value},
  168. );
  169. if (!value) {
  170. showErrorDialog(
  171. context,
  172. S.of(context).disableDownloadWarningTitle,
  173. S.of(context).disableDownloadWarningBody,
  174. );
  175. }
  176. },
  177. ),
  178. ),
  179. DividerWidget(
  180. dividerType: DividerType.menuNoIcon,
  181. bgColor: getEnteColorScheme(context).fillFaint,
  182. ),
  183. MenuItemWidget(
  184. key: ValueKey("Password lock $isPasswordEnabled"),
  185. captionedTextWidget: CaptionedTextWidget(
  186. title: S.of(context).passwordLock,
  187. ),
  188. alignCaptionedTextToLeft: true,
  189. isTopBorderRadiusRemoved: true,
  190. menuItemColor: getEnteColorScheme(context).fillFaint,
  191. trailingWidget: Switch.adaptive(
  192. value: isPasswordEnabled,
  193. onChanged: (enablePassword) async {
  194. if (enablePassword) {
  195. showTextInputDialog(
  196. context,
  197. title: S.of(context).setAPassword,
  198. submitButtonLabel: S.of(context).lockButtonLabel,
  199. hintText: S.of(context).enterPassword,
  200. isPasswordInput: true,
  201. alwaysShowSuccessState: true,
  202. onSubmit: (String password) async {
  203. if (password.trim().isNotEmpty) {
  204. final propToUpdate =
  205. await _getEncryptedPassword(
  206. password,
  207. );
  208. await _updateUrlSettings(
  209. context,
  210. propToUpdate,
  211. showProgressDialog: false,
  212. );
  213. }
  214. },
  215. );
  216. } else {
  217. await _updateUrlSettings(
  218. context,
  219. {'disablePassword': true},
  220. );
  221. }
  222. },
  223. ),
  224. ),
  225. const SizedBox(
  226. height: 24,
  227. ),
  228. if (url.isExpired)
  229. MenuItemWidget(
  230. captionedTextWidget: CaptionedTextWidget(
  231. title: S.of(context).linkHasExpired,
  232. textColor: getEnteColorScheme(context).warning500,
  233. ),
  234. leadingIcon: Icons.error_outline,
  235. leadingIconColor: getEnteColorScheme(context).warning500,
  236. menuItemColor: getEnteColorScheme(context).fillFaint,
  237. isBottomBorderRadiusRemoved: true,
  238. ),
  239. if (!url.isExpired)
  240. MenuItemWidget(
  241. captionedTextWidget: CaptionedTextWidget(
  242. title: S.of(context).copyLink,
  243. makeTextBold: true,
  244. ),
  245. leadingIcon: Icons.copy,
  246. menuItemColor: getEnteColorScheme(context).fillFaint,
  247. showOnlyLoadingState: true,
  248. onTap: () async {
  249. await Clipboard.setData(ClipboardData(text: urlValue));
  250. showShortToast(
  251. context,
  252. S.of(context).linkCopiedToClipboard,
  253. );
  254. },
  255. isBottomBorderRadiusRemoved: true,
  256. ),
  257. if (!url.isExpired)
  258. DividerWidget(
  259. dividerType: DividerType.menu,
  260. bgColor: getEnteColorScheme(context).fillFaint,
  261. ),
  262. if (!url.isExpired)
  263. MenuItemWidget(
  264. captionedTextWidget: CaptionedTextWidget(
  265. title: S.of(context).sendLink,
  266. makeTextBold: true,
  267. ),
  268. leadingIcon: Icons.adaptive.share,
  269. menuItemColor: getEnteColorScheme(context).fillFaint,
  270. onTap: () async {
  271. shareText(urlValue);
  272. },
  273. isTopBorderRadiusRemoved: true,
  274. ),
  275. const SizedBox(
  276. height: 24,
  277. ),
  278. MenuItemWidget(
  279. captionedTextWidget: CaptionedTextWidget(
  280. title: S.of(context).removeLink,
  281. textColor: warning500,
  282. makeTextBold: true,
  283. ),
  284. leadingIcon: Icons.remove_circle_outline,
  285. leadingIconColor: warning500,
  286. menuItemColor: getEnteColorScheme(context).fillFaint,
  287. surfaceExecutionStates: false,
  288. onTap: () async {
  289. final bool result = await sharingActions.disableUrl(
  290. context,
  291. widget.collection!,
  292. );
  293. if (result && mounted) {
  294. Navigator.of(context).pop();
  295. if (widget.collection!.isQuickLinkCollection()) {
  296. Navigator.of(context).pop();
  297. }
  298. }
  299. },
  300. ),
  301. ],
  302. ),
  303. ),
  304. ],
  305. ),
  306. ),
  307. );
  308. }
  309. Future<Map<String, dynamic>> _getEncryptedPassword(String pass) async {
  310. final kekSalt = CryptoUtil.getSaltToDeriveKey();
  311. final result = await CryptoUtil.deriveInteractiveKey(
  312. utf8.encode(pass) as Uint8List,
  313. kekSalt,
  314. );
  315. return {
  316. 'passHash': CryptoUtil.bin2base64(result.key),
  317. 'nonce': CryptoUtil.bin2base64(kekSalt),
  318. 'memLimit': result.memLimit,
  319. 'opsLimit': result.opsLimit,
  320. };
  321. }
  322. Future<void> _updateUrlSettings(
  323. BuildContext context,
  324. Map<String, dynamic> prop, {
  325. bool showProgressDialog = true,
  326. }) async {
  327. final dialog = showProgressDialog
  328. ? createProgressDialog(context, S.of(context).pleaseWait)
  329. : null;
  330. await dialog?.show();
  331. try {
  332. await CollectionsService.instance
  333. .updateShareUrl(widget.collection!, prop);
  334. await dialog?.hide();
  335. showShortToast(context, S.of(context).albumUpdated);
  336. if (mounted) {
  337. setState(() {});
  338. }
  339. } catch (e) {
  340. await dialog?.hide();
  341. await showGenericErrorDialog(context: context);
  342. rethrow;
  343. }
  344. }
  345. }