manage_links_widget.dart 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. import 'dart:convert';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
  5. import 'package:flutter_sodium/flutter_sodium.dart';
  6. import 'package:photos/ente_theme_data.dart';
  7. import 'package:photos/models/collection.dart';
  8. import 'package:photos/services/collections_service.dart';
  9. import 'package:photos/ui/common/dialogs.dart';
  10. import 'package:photos/utils/crypto_util.dart';
  11. import 'package:photos/utils/date_time_util.dart';
  12. import 'package:photos/utils/dialog_util.dart';
  13. import 'package:photos/utils/toast_util.dart';
  14. import 'package:tuple/tuple.dart';
  15. class ManageSharedLinkWidget extends StatefulWidget {
  16. final Collection collection;
  17. const ManageSharedLinkWidget({Key key, this.collection}) : super(key: key);
  18. @override
  19. State<ManageSharedLinkWidget> createState() => _ManageSharedLinkWidgetState();
  20. }
  21. class _ManageSharedLinkWidgetState extends State<ManageSharedLinkWidget> {
  22. // index, title, milliseconds in future post which link should expire (when >0)
  23. final List<Tuple3<int, String, int>> _expiryOptions = [
  24. const Tuple3(0, "Never", 0),
  25. Tuple3(1, "After 1 hour", const Duration(hours: 1).inMicroseconds),
  26. Tuple3(2, "After 1 day", const Duration(days: 1).inMicroseconds),
  27. Tuple3(3, "After 1 week", const Duration(days: 7).inMicroseconds),
  28. // todo: make this time calculation perfect
  29. Tuple3(4, "After 1 month", const Duration(days: 30).inMicroseconds),
  30. Tuple3(5, "After 1 year", const Duration(days: 365).inMicroseconds),
  31. const Tuple3(6, "Custom", -1),
  32. ];
  33. Tuple3<int, String, int> _selectedExpiry;
  34. int _selectedDeviceLimitIndex = 0;
  35. @override
  36. void initState() {
  37. _selectedExpiry = _expiryOptions.first;
  38. super.initState();
  39. }
  40. @override
  41. Widget build(BuildContext context) {
  42. return Scaffold(
  43. backgroundColor: Theme.of(context).backgroundColor,
  44. appBar: AppBar(
  45. elevation: 0,
  46. title: const Text(
  47. "Manage link",
  48. ),
  49. ),
  50. body: SingleChildScrollView(
  51. child: ListBody(
  52. children: <Widget>[
  53. Padding(
  54. padding: const EdgeInsets.fromLTRB(16, 12, 16, 12),
  55. child: Column(
  56. children: [
  57. const Padding(padding: EdgeInsets.all(4)),
  58. GestureDetector(
  59. behavior: HitTestBehavior.translucent,
  60. onTap: () async {
  61. await showPicker();
  62. },
  63. child: Row(
  64. crossAxisAlignment: CrossAxisAlignment.center,
  65. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  66. children: [
  67. Column(
  68. mainAxisAlignment: MainAxisAlignment.center,
  69. crossAxisAlignment: CrossAxisAlignment.start,
  70. children: [
  71. const Text("Link expiry"),
  72. const Padding(padding: EdgeInsets.all(4)),
  73. _getLinkExpiryTimeWidget(),
  74. ],
  75. ),
  76. const Icon(Icons.navigate_next),
  77. ],
  78. ),
  79. ),
  80. const Padding(padding: EdgeInsets.all(4)),
  81. const Divider(height: 4),
  82. const Padding(padding: EdgeInsets.all(4)),
  83. GestureDetector(
  84. behavior: HitTestBehavior.translucent,
  85. onTap: () {
  86. _showDeviceLimitPicker();
  87. },
  88. child: Row(
  89. crossAxisAlignment: CrossAxisAlignment.center,
  90. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  91. children: [
  92. Column(
  93. mainAxisAlignment: MainAxisAlignment.center,
  94. crossAxisAlignment: CrossAxisAlignment.start,
  95. children: [
  96. const Text("Device limit"),
  97. const Padding(padding: EdgeInsets.all(4)),
  98. Text(
  99. widget.collection.publicURLs.first.deviceLimit
  100. .toString(),
  101. style: const TextStyle(
  102. color: Colors.grey,
  103. ),
  104. ),
  105. ],
  106. ),
  107. const Icon(Icons.navigate_next),
  108. ],
  109. ),
  110. ),
  111. const Padding(padding: EdgeInsets.all(4)),
  112. const Divider(height: 4),
  113. const Padding(padding: EdgeInsets.all(4)),
  114. SizedBox(
  115. height: 36,
  116. child: Row(
  117. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  118. children: [
  119. const Text("Password lock"),
  120. Switch.adaptive(
  121. value: widget.collection.publicURLs?.first
  122. ?.passwordEnabled ??
  123. false,
  124. onChanged: (enablePassword) async {
  125. if (enablePassword) {
  126. final inputResult =
  127. await _displayLinkPasswordInput(context);
  128. if (inputResult != null &&
  129. inputResult == 'ok' &&
  130. _textFieldController.text.trim().isNotEmpty) {
  131. final propToUpdate = await _getEncryptedPassword(
  132. _textFieldController.text,
  133. );
  134. await _updateUrlSettings(context, propToUpdate);
  135. }
  136. } else {
  137. await _updateUrlSettings(
  138. context,
  139. {'disablePassword': true},
  140. );
  141. }
  142. setState(() {});
  143. },
  144. ),
  145. ],
  146. ),
  147. ),
  148. const Padding(padding: EdgeInsets.all(4)),
  149. const Divider(height: 4),
  150. const Padding(padding: EdgeInsets.all(4)),
  151. SizedBox(
  152. height: 36,
  153. child: Row(
  154. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  155. children: [
  156. const Text("File download"),
  157. Switch.adaptive(
  158. value: widget.collection.publicURLs?.first
  159. ?.enableDownload ??
  160. true,
  161. onChanged: (value) async {
  162. if (!value) {
  163. final choice = await showChoiceDialog(
  164. context,
  165. 'Disable downloads',
  166. 'Are you sure that you want to disable the download button for files?',
  167. firstAction: 'No',
  168. secondAction: 'Yes',
  169. firstActionColor:
  170. Theme.of(context).colorScheme.greenText,
  171. secondActionColor: Theme.of(context)
  172. .colorScheme
  173. .inverseBackgroundColor,
  174. );
  175. if (choice != DialogUserChoice.secondChoice) {
  176. return;
  177. }
  178. }
  179. await _updateUrlSettings(
  180. context,
  181. {'enableDownload': value},
  182. );
  183. if (!value) {
  184. showErrorDialog(
  185. context,
  186. "Please note",
  187. "Viewers can still take screenshots or save a copy of your photos using external tools",
  188. );
  189. }
  190. setState(() {});
  191. },
  192. ),
  193. ],
  194. ),
  195. ),
  196. ],
  197. ),
  198. ),
  199. ],
  200. ),
  201. ),
  202. );
  203. }
  204. Future<void> showPicker() async {
  205. Widget getOptionText(String text) {
  206. return Text(text, style: Theme.of(context).textTheme.subtitle1);
  207. }
  208. return showCupertinoModalPopup(
  209. context: context,
  210. builder: (context) {
  211. return Column(
  212. mainAxisAlignment: MainAxisAlignment.end,
  213. children: <Widget>[
  214. Container(
  215. decoration: BoxDecoration(
  216. color: Theme.of(context).colorScheme.cupertinoPickerTopColor,
  217. border: const Border(
  218. bottom: BorderSide(
  219. color: Color(0xff999999),
  220. width: 0.0,
  221. ),
  222. ),
  223. ),
  224. child: Row(
  225. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  226. children: <Widget>[
  227. CupertinoButton(
  228. onPressed: () {
  229. Navigator.of(context).pop('cancel');
  230. },
  231. padding: const EdgeInsets.symmetric(
  232. horizontal: 8.0,
  233. vertical: 5.0,
  234. ),
  235. child: Text(
  236. 'Cancel',
  237. style: Theme.of(context).textTheme.subtitle1,
  238. ),
  239. ),
  240. CupertinoButton(
  241. onPressed: () async {
  242. int newValidTill = -1;
  243. final int expireAfterInMicroseconds = _selectedExpiry.item3;
  244. // need to manually select time
  245. if (expireAfterInMicroseconds < 0) {
  246. final timeInMicrosecondsFromEpoch =
  247. await _showDateTimePicker();
  248. if (timeInMicrosecondsFromEpoch != null) {
  249. newValidTill = timeInMicrosecondsFromEpoch;
  250. }
  251. } else if (expireAfterInMicroseconds == 0) {
  252. // no expiry
  253. newValidTill = 0;
  254. } else {
  255. newValidTill = DateTime.now().microsecondsSinceEpoch +
  256. expireAfterInMicroseconds;
  257. }
  258. if (newValidTill >= 0) {
  259. await _updateUrlSettings(
  260. context,
  261. {'validTill': newValidTill},
  262. );
  263. setState(() {});
  264. }
  265. Navigator.of(context).pop('');
  266. },
  267. padding: const EdgeInsets.symmetric(
  268. horizontal: 16.0,
  269. vertical: 2.0,
  270. ),
  271. child: Text(
  272. 'Confirm',
  273. style: Theme.of(context).textTheme.subtitle1,
  274. ),
  275. )
  276. ],
  277. ),
  278. ),
  279. Container(
  280. height: 220.0,
  281. color: const Color(0xfff7f7f7),
  282. child: CupertinoPicker(
  283. backgroundColor:
  284. Theme.of(context).backgroundColor.withOpacity(0.95),
  285. onSelectedItemChanged: (value) {
  286. final firstWhere = _expiryOptions
  287. .firstWhere((element) => element.item1 == value);
  288. setState(() {
  289. _selectedExpiry = firstWhere;
  290. });
  291. },
  292. magnification: 1.3,
  293. useMagnifier: true,
  294. itemExtent: 25,
  295. diameterRatio: 1,
  296. children:
  297. _expiryOptions.map((e) => getOptionText(e.item2)).toList(),
  298. ),
  299. )
  300. ],
  301. );
  302. },
  303. );
  304. }
  305. // _showDateTimePicker return null if user doesn't select date-time
  306. Future<int> _showDateTimePicker() async {
  307. final dateResult = await DatePicker.showDatePicker(
  308. context,
  309. minTime: DateTime.now(),
  310. currentTime: DateTime.now(),
  311. locale: LocaleType.en,
  312. theme: Theme.of(context).colorScheme.dateTimePickertheme,
  313. );
  314. if (dateResult == null) {
  315. return null;
  316. }
  317. final dateWithTimeResult = await DatePicker.showTime12hPicker(
  318. context,
  319. showTitleActions: true,
  320. currentTime: dateResult,
  321. locale: LocaleType.en,
  322. theme: Theme.of(context).colorScheme.dateTimePickertheme,
  323. );
  324. if (dateWithTimeResult == null) {
  325. return null;
  326. } else {
  327. return dateWithTimeResult.microsecondsSinceEpoch;
  328. }
  329. }
  330. final TextEditingController _textFieldController = TextEditingController();
  331. Future<String> _displayLinkPasswordInput(BuildContext context) async {
  332. _textFieldController.clear();
  333. return showDialog<String>(
  334. context: context,
  335. builder: (context) {
  336. bool passwordVisible = false;
  337. return StatefulBuilder(
  338. builder: (context, setState) {
  339. return AlertDialog(
  340. title: const Text('Enter password'),
  341. content: TextFormField(
  342. autofillHints: const [AutofillHints.newPassword],
  343. decoration: InputDecoration(
  344. hintText: "Password",
  345. contentPadding: const EdgeInsets.all(12),
  346. suffixIcon: IconButton(
  347. icon: Icon(
  348. passwordVisible ? Icons.visibility : Icons.visibility_off,
  349. color: Colors.white.withOpacity(0.5),
  350. size: 20,
  351. ),
  352. onPressed: () {
  353. passwordVisible = !passwordVisible;
  354. setState(() {});
  355. },
  356. ),
  357. ),
  358. obscureText: !passwordVisible,
  359. controller: _textFieldController,
  360. autofocus: true,
  361. autocorrect: false,
  362. keyboardType: TextInputType.visiblePassword,
  363. onChanged: (_) {
  364. setState(() {});
  365. },
  366. ),
  367. actions: <Widget>[
  368. TextButton(
  369. child: Text(
  370. 'Cancel',
  371. style: Theme.of(context).textTheme.subtitle2,
  372. ),
  373. onPressed: () {
  374. Navigator.pop(context, 'cancel');
  375. },
  376. ),
  377. TextButton(
  378. child:
  379. Text('Ok', style: Theme.of(context).textTheme.subtitle2),
  380. onPressed: () {
  381. if (_textFieldController.text.trim().isEmpty) {
  382. return;
  383. }
  384. Navigator.pop(context, 'ok');
  385. },
  386. ),
  387. ],
  388. );
  389. },
  390. );
  391. },
  392. );
  393. }
  394. Future<Map<String, dynamic>> _getEncryptedPassword(String pass) async {
  395. assert(
  396. Sodium.cryptoPwhashAlgArgon2id13 == Sodium.cryptoPwhashAlgDefault,
  397. "mismatch in expected default pw hashing algo",
  398. );
  399. final int memLimit = Sodium.cryptoPwhashMemlimitInteractive;
  400. final int opsLimit = Sodium.cryptoPwhashOpslimitInteractive;
  401. final kekSalt = CryptoUtil.getSaltToDeriveKey();
  402. final result = await CryptoUtil.deriveKey(
  403. utf8.encode(pass),
  404. kekSalt,
  405. memLimit,
  406. opsLimit,
  407. );
  408. return {
  409. 'passHash': Sodium.bin2base64(result),
  410. 'nonce': Sodium.bin2base64(kekSalt),
  411. 'memLimit': memLimit,
  412. 'opsLimit': opsLimit,
  413. };
  414. }
  415. Future<void> _updateUrlSettings(
  416. BuildContext context,
  417. Map<String, dynamic> prop,
  418. ) async {
  419. final dialog = createProgressDialog(context, "Please wait...");
  420. await dialog.show();
  421. try {
  422. await CollectionsService.instance.updateShareUrl(widget.collection, prop);
  423. await dialog.hide();
  424. showToast(context, "Album updated");
  425. } catch (e) {
  426. await dialog.hide();
  427. await showGenericErrorDialog(context);
  428. }
  429. }
  430. Text _getLinkExpiryTimeWidget() {
  431. final int validTill = widget.collection.publicURLs?.first?.validTill ?? 0;
  432. if (validTill == 0) {
  433. return const Text(
  434. 'Never',
  435. style: TextStyle(
  436. color: Colors.grey,
  437. ),
  438. );
  439. }
  440. if (validTill < DateTime.now().microsecondsSinceEpoch) {
  441. return Text(
  442. 'Expired',
  443. style: TextStyle(
  444. color: Colors.orange[300],
  445. ),
  446. );
  447. }
  448. return Text(
  449. getFormattedTime(DateTime.fromMicrosecondsSinceEpoch(validTill)),
  450. style: const TextStyle(
  451. color: Colors.grey,
  452. ),
  453. );
  454. }
  455. Future<void> _showDeviceLimitPicker() async {
  456. final List<Text> options = [];
  457. for (int i = 50; i > 0; i--) {
  458. options.add(
  459. Text(i.toString(), style: Theme.of(context).textTheme.subtitle1),
  460. );
  461. }
  462. return showCupertinoModalPopup(
  463. context: context,
  464. builder: (context) {
  465. return Column(
  466. mainAxisAlignment: MainAxisAlignment.end,
  467. children: <Widget>[
  468. Container(
  469. decoration: BoxDecoration(
  470. color: Theme.of(context).colorScheme.cupertinoPickerTopColor,
  471. border: const Border(
  472. bottom: BorderSide(
  473. color: Color(0xff999999),
  474. width: 0.0,
  475. ),
  476. ),
  477. ),
  478. child: Row(
  479. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  480. children: <Widget>[
  481. CupertinoButton(
  482. onPressed: () {
  483. Navigator.of(context).pop('cancel');
  484. },
  485. padding: const EdgeInsets.symmetric(
  486. horizontal: 8.0,
  487. vertical: 5.0,
  488. ),
  489. child: Text(
  490. 'Cancel',
  491. style: Theme.of(context).textTheme.subtitle1,
  492. ),
  493. ),
  494. CupertinoButton(
  495. onPressed: () async {
  496. await _updateUrlSettings(context, {
  497. 'deviceLimit': int.tryParse(
  498. options[_selectedDeviceLimitIndex].data,
  499. ),
  500. });
  501. setState(() {});
  502. Navigator.of(context).pop('');
  503. },
  504. padding: const EdgeInsets.symmetric(
  505. horizontal: 16.0,
  506. vertical: 2.0,
  507. ),
  508. child: Text(
  509. 'Confirm',
  510. style: Theme.of(context).textTheme.subtitle1,
  511. ),
  512. )
  513. ],
  514. ),
  515. ),
  516. Container(
  517. height: 220.0,
  518. color: const Color(0xfff7f7f7),
  519. child: CupertinoPicker(
  520. backgroundColor:
  521. Theme.of(context).backgroundColor.withOpacity(0.95),
  522. onSelectedItemChanged: (value) {
  523. _selectedDeviceLimitIndex = value;
  524. },
  525. magnification: 1.3,
  526. useMagnifier: true,
  527. itemExtent: 25,
  528. diameterRatio: 1,
  529. children: options,
  530. ),
  531. )
  532. ],
  533. );
  534. },
  535. );
  536. }
  537. }