file_selection_actions_widget.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. import 'package:fast_base58/fast_base58.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:page_transition/page_transition.dart';
  6. import 'package:photos/core/configuration.dart';
  7. import 'package:photos/models/collection.dart';
  8. import 'package:photos/models/device_collection.dart';
  9. import 'package:photos/models/gallery_type.dart';
  10. import 'package:photos/models/magic_metadata.dart';
  11. import 'package:photos/models/selected_file_breakup.dart';
  12. import 'package:photos/models/selected_files.dart';
  13. import 'package:photos/services/collections_service.dart';
  14. import 'package:photos/services/hidden_service.dart';
  15. import 'package:photos/theme/ente_theme.dart';
  16. import 'package:photos/ui/actions/collection/collection_file_actions.dart';
  17. import 'package:photos/ui/actions/collection/collection_sharing_actions.dart';
  18. import 'package:photos/ui/components/action_sheet_widget.dart';
  19. import 'package:photos/ui/components/blur_menu_item_widget.dart';
  20. import 'package:photos/ui/components/bottom_action_bar/expanded_menu_widget.dart';
  21. import 'package:photos/ui/components/button_widget.dart';
  22. import 'package:photos/ui/components/models/button_type.dart';
  23. import 'package:photos/ui/create_collection_page.dart';
  24. import 'package:photos/ui/sharing/manage_links_widget.dart';
  25. import 'package:photos/utils/delete_file_util.dart';
  26. import 'package:photos/utils/magic_util.dart';
  27. import 'package:photos/utils/navigation_util.dart';
  28. import 'package:photos/utils/toast_util.dart';
  29. class FileSelectionActionWidget extends StatefulWidget {
  30. final GalleryType type;
  31. final Collection? collection;
  32. final DeviceCollection? deviceCollection;
  33. final SelectedFiles selectedFiles;
  34. const FileSelectionActionWidget(
  35. this.type,
  36. this.selectedFiles, {
  37. Key? key,
  38. this.collection,
  39. this.deviceCollection,
  40. }) : super(key: key);
  41. @override
  42. State<FileSelectionActionWidget> createState() =>
  43. _FileSelectionActionWidgetState();
  44. }
  45. class _FileSelectionActionWidgetState extends State<FileSelectionActionWidget> {
  46. late int currentUserID;
  47. late SelectedFileSplit split;
  48. late CollectionActions collectionActions;
  49. // _cachedCollectionForSharedLink is primarly used to avoid creating duplicate
  50. // links if user keeps on creating Create link button after selecting
  51. // few files. This link is reset on any selection changed;
  52. Collection? _cachedCollectionForSharedLink;
  53. @override
  54. void initState() {
  55. currentUserID = Configuration.instance.getUserID()!;
  56. split = widget.selectedFiles.split(currentUserID);
  57. widget.selectedFiles.addListener(_selectFileChangeListener);
  58. collectionActions = CollectionActions(CollectionsService.instance);
  59. super.initState();
  60. }
  61. @override
  62. void dispose() {
  63. widget.selectedFiles.removeListener(_selectFileChangeListener);
  64. super.dispose();
  65. }
  66. void _selectFileChangeListener() {
  67. if (_cachedCollectionForSharedLink != null) {
  68. _cachedCollectionForSharedLink = null;
  69. }
  70. split = widget.selectedFiles.split(currentUserID);
  71. if (mounted) {
  72. setState(() => {});
  73. }
  74. }
  75. @override
  76. Widget build(BuildContext context) {
  77. final bool showPrefix =
  78. split.pendingUploads.isNotEmpty || split.ownedByOtherUsers.isNotEmpty;
  79. final String suffix = showPrefix
  80. ? " (${split.ownedByCurrentUser.length})"
  81. ""
  82. : "";
  83. final String suffixInPending = split.ownedByOtherUsers.isNotEmpty
  84. ? " (${split.ownedByCurrentUser.length + split.pendingUploads.length})"
  85. ""
  86. : "";
  87. final bool anyOwnedFiles =
  88. split.pendingUploads.isNotEmpty || split.ownedByCurrentUser.isNotEmpty;
  89. final bool anyUploadedFiles = split.ownedByCurrentUser.isNotEmpty;
  90. bool showRemoveOption = widget.type.showRemoveFromAlbum();
  91. if (showRemoveOption && widget.type == GalleryType.sharedCollection) {
  92. showRemoveOption = split.ownedByCurrentUser.isNotEmpty;
  93. }
  94. debugPrint('$runtimeType building $mounted');
  95. final colorScheme = getEnteColorScheme(context);
  96. final List<List<BlurMenuItemWidget>> items = [];
  97. final List<BlurMenuItemWidget> firstList = [];
  98. final List<BlurMenuItemWidget> secondList = [];
  99. if (widget.type.showCreateLink()) {
  100. if (_cachedCollectionForSharedLink != null && anyUploadedFiles) {
  101. firstList.add(
  102. BlurMenuItemWidget(
  103. leadingIcon: Icons.copy_outlined,
  104. labelText: "Copy link",
  105. menuItemColor: colorScheme.fillFaint,
  106. onTap: anyUploadedFiles ? _copyLink : null,
  107. ),
  108. );
  109. } else {
  110. firstList.add(
  111. BlurMenuItemWidget(
  112. leadingIcon: Icons.link_outlined,
  113. labelText: "Create link$suffix",
  114. menuItemColor: colorScheme.fillFaint,
  115. onTap: anyUploadedFiles ? _onCreatedSharedLinkClicked : null,
  116. ),
  117. );
  118. }
  119. }
  120. final showUploadIcon = widget.type == GalleryType.localFolder &&
  121. split.ownedByCurrentUser.isEmpty;
  122. if (widget.type.showAddToAlbum()) {
  123. secondList.add(
  124. BlurMenuItemWidget(
  125. leadingIcon:
  126. showUploadIcon ? Icons.cloud_upload_outlined : Icons.add_outlined,
  127. labelText:
  128. "Add to ${showUploadIcon ? 'ente' : 'album'}$suffixInPending",
  129. menuItemColor: colorScheme.fillFaint,
  130. onTap: anyOwnedFiles ? _addToAlbum : null,
  131. ),
  132. );
  133. }
  134. if (widget.type.showMoveToAlbum()) {
  135. secondList.add(
  136. BlurMenuItemWidget(
  137. leadingIcon: Icons.arrow_forward_outlined,
  138. labelText: "Move to album$suffix",
  139. menuItemColor: colorScheme.fillFaint,
  140. onTap: anyUploadedFiles ? _moveFiles : null,
  141. ),
  142. );
  143. }
  144. if (showRemoveOption) {
  145. secondList.add(
  146. BlurMenuItemWidget(
  147. leadingIcon: Icons.remove_outlined,
  148. labelText: "Remove from album$suffix",
  149. menuItemColor: colorScheme.fillFaint,
  150. onTap: anyUploadedFiles ? _removeFilesFromAlbum : null,
  151. ),
  152. );
  153. }
  154. if (widget.type.showDeleteOption()) {
  155. secondList.add(
  156. BlurMenuItemWidget(
  157. leadingIcon: Icons.delete_outline,
  158. labelText: "Delete$suffixInPending",
  159. menuItemColor: colorScheme.fillFaint,
  160. onTap: anyOwnedFiles ? _onDeleteClick : null,
  161. ),
  162. );
  163. }
  164. if (widget.type.showHideOption()) {
  165. secondList.add(
  166. BlurMenuItemWidget(
  167. leadingIcon: Icons.visibility_off_outlined,
  168. labelText: "Hide$suffix",
  169. menuItemColor: colorScheme.fillFaint,
  170. onTap: anyUploadedFiles ? _onHideClick : null,
  171. ),
  172. );
  173. } else if (widget.type.showUnHideOption()) {
  174. secondList.add(
  175. BlurMenuItemWidget(
  176. leadingIcon: Icons.visibility_off_outlined,
  177. labelText: "Unhide$suffix",
  178. menuItemColor: colorScheme.fillFaint,
  179. onTap: _onUnhideClick,
  180. ),
  181. );
  182. }
  183. if (widget.type.showArchiveOption()) {
  184. secondList.add(
  185. BlurMenuItemWidget(
  186. leadingIcon: Icons.archive_outlined,
  187. labelText: "Archive$suffix",
  188. menuItemColor: colorScheme.fillFaint,
  189. onTap: anyUploadedFiles ? _onArchiveClick : null,
  190. ),
  191. );
  192. } else if (widget.type.showUnArchiveOption()) {
  193. secondList.add(
  194. BlurMenuItemWidget(
  195. leadingIcon: Icons.unarchive,
  196. labelText: "Unarchive$suffix",
  197. menuItemColor: colorScheme.fillFaint,
  198. onTap: _onUnArchiveClick,
  199. ),
  200. );
  201. }
  202. if (widget.type.showFavoriteOption()) {
  203. secondList.add(
  204. BlurMenuItemWidget(
  205. leadingIcon: Icons.favorite_border_rounded,
  206. labelText: "Favorite$suffix",
  207. menuItemColor: colorScheme.fillFaint,
  208. onTap: anyUploadedFiles ? _onFavoriteClick : null,
  209. ),
  210. );
  211. } else if (widget.type.showUnFavoriteOption()) {
  212. secondList.add(
  213. BlurMenuItemWidget(
  214. leadingIcon: Icons.favorite,
  215. labelText: "Remove from favorite$suffix",
  216. menuItemColor: colorScheme.fillFaint,
  217. onTap: _onUnFavoriteClick,
  218. ),
  219. );
  220. }
  221. if (firstList.isNotEmpty || secondList.isNotEmpty) {
  222. if (firstList.isNotEmpty) {
  223. items.add(firstList);
  224. }
  225. items.add(secondList);
  226. return ExpandedMenuWidget(
  227. items: items,
  228. );
  229. } else {
  230. // TODO: Return "Select All" here
  231. return const SizedBox.shrink();
  232. }
  233. }
  234. Future<void> _moveFiles() async {
  235. if (split.pendingUploads.isNotEmpty || split.ownedByOtherUsers.isNotEmpty) {
  236. widget.selectedFiles
  237. .unSelectAll(split.pendingUploads.toSet(), skipNotify: true);
  238. widget.selectedFiles
  239. .unSelectAll(split.ownedByOtherUsers.toSet(), skipNotify: true);
  240. }
  241. await _selectionCollectionForAction(CollectionActionType.moveFiles);
  242. }
  243. Future<void> _addToAlbum() async {
  244. if (split.ownedByOtherUsers.isNotEmpty) {
  245. widget.selectedFiles
  246. .unSelectAll(split.ownedByOtherUsers.toSet(), skipNotify: true);
  247. }
  248. await _selectionCollectionForAction(CollectionActionType.addFiles);
  249. }
  250. Future<void> _onDeleteClick() async {
  251. showDeleteSheet(context, widget.selectedFiles);
  252. }
  253. Future<void> _removeFilesFromAlbum() async {
  254. if (split.pendingUploads.isNotEmpty || split.ownedByOtherUsers.isNotEmpty) {
  255. widget.selectedFiles
  256. .unSelectAll(split.pendingUploads.toSet(), skipNotify: true);
  257. widget.selectedFiles
  258. .unSelectAll(split.ownedByOtherUsers.toSet(), skipNotify: true);
  259. }
  260. await collectionActions.showRemoveFromCollectionSheet(
  261. context,
  262. widget.collection!,
  263. widget.selectedFiles,
  264. );
  265. }
  266. Future<void> _onFavoriteClick() async {
  267. final result = await collectionActions.updateFavorites(
  268. context,
  269. split.ownedByCurrentUser,
  270. true,
  271. );
  272. if (result) {
  273. widget.selectedFiles.clearAll();
  274. }
  275. }
  276. Future<void> _onUnFavoriteClick() async {
  277. final result = await collectionActions.updateFavorites(
  278. context,
  279. split.ownedByCurrentUser,
  280. false,
  281. );
  282. if (result) {
  283. widget.selectedFiles.clearAll();
  284. }
  285. }
  286. Future<void> _onArchiveClick() async {
  287. await changeVisibility(
  288. context,
  289. split.ownedByCurrentUser,
  290. visibilityArchive,
  291. );
  292. widget.selectedFiles.clearAll();
  293. }
  294. Future<void> _onUnArchiveClick() async {
  295. await changeVisibility(
  296. context,
  297. split.ownedByCurrentUser,
  298. visibilityVisible,
  299. );
  300. widget.selectedFiles.clearAll();
  301. }
  302. Future<void> _onHideClick() async {
  303. await CollectionsService.instance.hideFiles(
  304. context,
  305. split.ownedByCurrentUser,
  306. );
  307. widget.selectedFiles.clearAll();
  308. }
  309. Future<void> _onUnhideClick() async {
  310. if (split.pendingUploads.isNotEmpty || split.ownedByOtherUsers.isNotEmpty) {
  311. widget.selectedFiles
  312. .unSelectAll(split.pendingUploads.toSet(), skipNotify: true);
  313. widget.selectedFiles
  314. .unSelectAll(split.ownedByOtherUsers.toSet(), skipNotify: true);
  315. }
  316. await _selectionCollectionForAction(CollectionActionType.unHide);
  317. }
  318. Future<void> _onCreatedSharedLinkClicked() async {
  319. if (split.ownedByCurrentUser.isEmpty) {
  320. showShortToast(context, "Can only create link for files owned by you");
  321. return;
  322. }
  323. _cachedCollectionForSharedLink ??= await collectionActions
  324. .createSharedCollectionLink(context, split.ownedByCurrentUser);
  325. final actionResult = await showActionSheet(
  326. context: context,
  327. buttons: [
  328. const ButtonWidget(
  329. labelText: "Copy link",
  330. buttonType: ButtonType.neutral,
  331. buttonSize: ButtonSize.large,
  332. shouldStickToDarkTheme: true,
  333. buttonAction: ButtonAction.first,
  334. isInAlert: true,
  335. ),
  336. const ButtonWidget(
  337. labelText: "Manage link",
  338. buttonType: ButtonType.secondary,
  339. buttonSize: ButtonSize.large,
  340. buttonAction: ButtonAction.second,
  341. shouldStickToDarkTheme: true,
  342. isInAlert: true,
  343. ),
  344. const ButtonWidget(
  345. labelText: "Done",
  346. buttonType: ButtonType.secondary,
  347. buttonSize: ButtonSize.large,
  348. buttonAction: ButtonAction.third,
  349. shouldStickToDarkTheme: true,
  350. isInAlert: true,
  351. )
  352. ],
  353. title: "Public link created",
  354. body: "You can manage your links in the share tab.",
  355. actionSheetType: ActionSheetType.defaultActionSheet,
  356. );
  357. if (actionResult != null && actionResult == ButtonAction.first) {
  358. await _copyLink();
  359. }
  360. if (actionResult != null && actionResult == ButtonAction.second) {
  361. routeToPage(
  362. context,
  363. ManageSharedLinkWidget(collection: _cachedCollectionForSharedLink),
  364. );
  365. }
  366. if (mounted) {
  367. setState(() => {});
  368. }
  369. }
  370. Future<void> _copyLink() async {
  371. if (_cachedCollectionForSharedLink != null) {
  372. final String collectionKey = Base58Encode(
  373. CollectionsService.instance
  374. .getCollectionKey(_cachedCollectionForSharedLink!.id),
  375. );
  376. final String url =
  377. "${_cachedCollectionForSharedLink!.publicURLs?.first?.url}#$collectionKey";
  378. await Clipboard.setData(ClipboardData(text: url));
  379. showShortToast(context, "Link copied to clipboard");
  380. }
  381. }
  382. Future<Object?> _selectionCollectionForAction(
  383. CollectionActionType type,
  384. ) async {
  385. return Navigator.push(
  386. context,
  387. PageTransition(
  388. type: PageTransitionType.bottomToTop,
  389. child: CreateCollectionPage(
  390. widget.selectedFiles,
  391. null,
  392. actionType: type,
  393. ),
  394. ),
  395. );
  396. }
  397. }