123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478 |
- import 'package:fast_base58/fast_base58.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:photos/core/configuration.dart';
- import 'package:photos/models/collection.dart';
- import 'package:photos/models/device_collection.dart';
- import 'package:photos/models/file.dart';
- import 'package:photos/models/files_split.dart';
- import 'package:photos/models/gallery_type.dart';
- import 'package:photos/models/magic_metadata.dart';
- import 'package:photos/models/selected_files.dart';
- import 'package:photos/services/collections_service.dart';
- import 'package:photos/services/hidden_service.dart';
- import 'package:photos/theme/ente_theme.dart';
- import 'package:photos/ui/actions/collection/collection_file_actions.dart';
- import 'package:photos/ui/actions/collection/collection_sharing_actions.dart';
- import 'package:photos/ui/components/action_sheet_widget.dart';
- import 'package:photos/ui/components/blur_menu_item_widget.dart';
- import 'package:photos/ui/components/bottom_action_bar/expanded_menu_widget.dart';
- import 'package:photos/ui/components/button_widget.dart';
- import 'package:photos/ui/components/models/button_type.dart';
- import 'package:photos/ui/create_collection_sheet.dart';
- import 'package:photos/ui/sharing/manage_links_widget.dart';
- import 'package:photos/utils/delete_file_util.dart';
- import 'package:photos/utils/magic_util.dart';
- import 'package:photos/utils/navigation_util.dart';
- import 'package:photos/utils/toast_util.dart';
- class FileSelectionActionWidget extends StatefulWidget {
- final GalleryType type;
- final Collection? collection;
- final DeviceCollection? deviceCollection;
- final SelectedFiles selectedFiles;
- const FileSelectionActionWidget(
- this.type,
- this.selectedFiles, {
- Key? key,
- this.collection,
- this.deviceCollection,
- }) : super(key: key);
- @override
- State<FileSelectionActionWidget> createState() =>
- _FileSelectionActionWidgetState();
- }
- class _FileSelectionActionWidgetState extends State<FileSelectionActionWidget> {
- late int currentUserID;
- late FilesSplit split;
- late CollectionActions collectionActions;
- late bool isCollectionOwner;
- // _cachedCollectionForSharedLink is primarly used to avoid creating duplicate
- // links if user keeps on creating Create link button after selecting
- // few files. This link is reset on any selection changed;
- Collection? _cachedCollectionForSharedLink;
- @override
- void initState() {
- currentUserID = Configuration.instance.getUserID()!;
- split = FilesSplit.split(<File>[], currentUserID);
- widget.selectedFiles.addListener(_selectFileChangeListener);
- collectionActions = CollectionActions(CollectionsService.instance);
- isCollectionOwner =
- widget.collection != null && widget.collection!.isOwner(currentUserID);
- super.initState();
- }
- @override
- void dispose() {
- widget.selectedFiles.removeListener(_selectFileChangeListener);
- super.dispose();
- }
- void _selectFileChangeListener() {
- if (_cachedCollectionForSharedLink != null) {
- _cachedCollectionForSharedLink = null;
- }
- split = FilesSplit.split(widget.selectedFiles.files, currentUserID);
- if (mounted) {
- setState(() => {});
- }
- }
- @override
- Widget build(BuildContext context) {
- final bool showPrefix =
- split.pendingUploads.isNotEmpty || split.ownedByOtherUsers.isNotEmpty;
- final String suffix = showPrefix
- ? " (${split.ownedByCurrentUser.length})"
- ""
- : "";
- final int removeCount = split.ownedByCurrentUser.length +
- (isCollectionOwner ? split.ownedByOtherUsers.length : 0);
- final String removeSuffix = showPrefix
- ? " ($removeCount)"
- ""
- : "";
- final String suffixInPending = split.ownedByOtherUsers.isNotEmpty
- ? " (${split.ownedByCurrentUser.length + split.pendingUploads.length})"
- ""
- : "";
- final bool anyOwnedFiles =
- split.pendingUploads.isNotEmpty || split.ownedByCurrentUser.isNotEmpty;
- final bool anyUploadedFiles = split.ownedByCurrentUser.isNotEmpty;
- final bool showRemoveOption = widget.type.showRemoveFromAlbum();
- debugPrint('$runtimeType building $mounted');
- final colorScheme = getEnteColorScheme(context);
- final List<List<BlurMenuItemWidget>> items = [];
- final List<BlurMenuItemWidget> firstList = [];
- final List<BlurMenuItemWidget> secondList = [];
- if (widget.type.showCreateLink()) {
- if (_cachedCollectionForSharedLink != null && anyUploadedFiles) {
- firstList.add(
- BlurMenuItemWidget(
- leadingIcon: Icons.copy_outlined,
- labelText: "Copy link",
- menuItemColor: colorScheme.fillFaint,
- onTap: anyUploadedFiles ? _copyLink : null,
- ),
- );
- } else {
- firstList.add(
- BlurMenuItemWidget(
- leadingIcon: Icons.link_outlined,
- labelText: "Create link$suffix",
- menuItemColor: colorScheme.fillFaint,
- onTap: anyUploadedFiles ? _onCreatedSharedLinkClicked : null,
- ),
- );
- }
- }
- final showUploadIcon = widget.type == GalleryType.localFolder &&
- split.ownedByCurrentUser.isEmpty;
- if (widget.type.showAddToAlbum()) {
- secondList.add(
- BlurMenuItemWidget(
- leadingIcon:
- showUploadIcon ? Icons.cloud_upload_outlined : Icons.add_outlined,
- labelText:
- "Add to ${showUploadIcon ? 'ente' : 'album'}$suffixInPending",
- menuItemColor: colorScheme.fillFaint,
- onTap: anyOwnedFiles ? _addToAlbum : null,
- ),
- );
- }
- if (widget.type.showMoveToAlbum()) {
- secondList.add(
- BlurMenuItemWidget(
- leadingIcon: Icons.arrow_forward_outlined,
- labelText: "Move to album$suffix",
- menuItemColor: colorScheme.fillFaint,
- onTap: anyUploadedFiles ? _moveFiles : null,
- ),
- );
- }
- if (showRemoveOption) {
- secondList.add(
- BlurMenuItemWidget(
- leadingIcon: Icons.remove_outlined,
- labelText: "Remove from album$removeSuffix",
- menuItemColor: colorScheme.fillFaint,
- onTap: removeCount > 0 ? _removeFilesFromAlbum : null,
- ),
- );
- }
- if (widget.type.showDeleteOption()) {
- secondList.add(
- BlurMenuItemWidget(
- leadingIcon: Icons.delete_outline,
- labelText: "Delete$suffixInPending",
- menuItemColor: colorScheme.fillFaint,
- onTap: anyOwnedFiles ? _onDeleteClick : null,
- ),
- );
- }
- if (widget.type.showHideOption()) {
- secondList.add(
- BlurMenuItemWidget(
- leadingIcon: Icons.visibility_off_outlined,
- labelText: "Hide$suffix",
- menuItemColor: colorScheme.fillFaint,
- onTap: anyUploadedFiles ? _onHideClick : null,
- ),
- );
- } else if (widget.type.showUnHideOption()) {
- secondList.add(
- BlurMenuItemWidget(
- leadingIcon: Icons.visibility_off_outlined,
- labelText: "Unhide$suffix",
- menuItemColor: colorScheme.fillFaint,
- onTap: _onUnhideClick,
- ),
- );
- }
- if (widget.type.showArchiveOption()) {
- secondList.add(
- BlurMenuItemWidget(
- leadingIcon: Icons.archive_outlined,
- labelText: "Archive$suffix",
- menuItemColor: colorScheme.fillFaint,
- onTap: anyUploadedFiles ? _onArchiveClick : null,
- ),
- );
- } else if (widget.type.showUnArchiveOption()) {
- secondList.add(
- BlurMenuItemWidget(
- leadingIcon: Icons.unarchive,
- labelText: "Unarchive$suffix",
- menuItemColor: colorScheme.fillFaint,
- onTap: _onUnArchiveClick,
- ),
- );
- }
- if (widget.type.showFavoriteOption()) {
- secondList.add(
- BlurMenuItemWidget(
- leadingIcon: Icons.favorite_border_rounded,
- labelText: "Favorite$suffix",
- menuItemColor: colorScheme.fillFaint,
- onTap: anyUploadedFiles ? _onFavoriteClick : null,
- ),
- );
- } else if (widget.type.showUnFavoriteOption()) {
- secondList.add(
- BlurMenuItemWidget(
- leadingIcon: Icons.favorite,
- labelText: "Remove from favorite$suffix",
- menuItemColor: colorScheme.fillFaint,
- onTap: _onUnFavoriteClick,
- ),
- );
- }
- if (widget.type.showRestoreOption()) {
- secondList.add(
- BlurMenuItemWidget(
- leadingIcon: Icons.visibility,
- labelText: "Restore",
- menuItemColor: colorScheme.fillFaint,
- onTap: _restore,
- ),
- );
- }
- if (widget.type.showPermanentlyDeleteOption()) {
- secondList.add(
- BlurMenuItemWidget(
- leadingIcon: Icons.delete_forever_outlined,
- labelText: "Permanently delete",
- menuItemColor: colorScheme.fillFaint,
- onTap: _permanentlyDelete,
- ),
- );
- }
- if (firstList.isNotEmpty || secondList.isNotEmpty) {
- if (firstList.isNotEmpty) {
- items.add(firstList);
- }
- items.add(secondList);
- return ExpandedMenuWidget(
- items: items,
- );
- } else {
- // TODO: Return "Select All" here
- return const SizedBox.shrink();
- }
- }
- Future<void> _moveFiles() async {
- if (split.pendingUploads.isNotEmpty || split.ownedByOtherUsers.isNotEmpty) {
- widget.selectedFiles
- .unSelectAll(split.pendingUploads.toSet(), skipNotify: true);
- widget.selectedFiles
- .unSelectAll(split.ownedByOtherUsers.toSet(), skipNotify: true);
- }
- createCollectionSheet(
- widget.selectedFiles,
- null,
- context,
- actionType: CollectionActionType.moveFiles,
- );
- }
- Future<void> _addToAlbum() async {
- if (split.ownedByOtherUsers.isNotEmpty) {
- widget.selectedFiles
- .unSelectAll(split.ownedByOtherUsers.toSet(), skipNotify: true);
- }
- createCollectionSheet(
- widget.selectedFiles,
- null,
- context,
- );
- }
- Future<void> _onDeleteClick() async {
- return showDeleteSheet(context, widget.selectedFiles);
- }
- Future<void> _removeFilesFromAlbum() async {
- if (split.pendingUploads.isNotEmpty) {
- widget.selectedFiles
- .unSelectAll(split.pendingUploads.toSet(), skipNotify: true);
- }
- if (!isCollectionOwner && split.ownedByOtherUsers.isNotEmpty) {
- widget.selectedFiles
- .unSelectAll(split.ownedByOtherUsers.toSet(), skipNotify: true);
- }
- final bool removingOthersFile =
- isCollectionOwner && split.ownedByOtherUsers.isNotEmpty;
- await collectionActions.showRemoveFromCollectionSheetV2(
- context,
- widget.collection!,
- widget.selectedFiles,
- removingOthersFile,
- );
- }
- Future<void> _onFavoriteClick() async {
- final result = await collectionActions.updateFavorites(
- context,
- split.ownedByCurrentUser,
- true,
- );
- if (result) {
- widget.selectedFiles.clearAll();
- }
- }
- Future<void> _onUnFavoriteClick() async {
- final result = await collectionActions.updateFavorites(
- context,
- split.ownedByCurrentUser,
- false,
- );
- if (result) {
- widget.selectedFiles.clearAll();
- }
- }
- Future<void> _onArchiveClick() async {
- await changeVisibility(
- context,
- split.ownedByCurrentUser,
- visibilityArchive,
- );
- widget.selectedFiles.clearAll();
- }
- Future<void> _onUnArchiveClick() async {
- await changeVisibility(
- context,
- split.ownedByCurrentUser,
- visibilityVisible,
- );
- widget.selectedFiles.clearAll();
- }
- Future<void> _onHideClick() async {
- await CollectionsService.instance.hideFiles(
- context,
- split.ownedByCurrentUser,
- );
- widget.selectedFiles.clearAll();
- }
- Future<void> _onUnhideClick() async {
- if (split.pendingUploads.isNotEmpty || split.ownedByOtherUsers.isNotEmpty) {
- widget.selectedFiles
- .unSelectAll(split.pendingUploads.toSet(), skipNotify: true);
- widget.selectedFiles
- .unSelectAll(split.ownedByOtherUsers.toSet(), skipNotify: true);
- }
- createCollectionSheet(
- widget.selectedFiles,
- null,
- context,
- actionType: CollectionActionType.unHide,
- );
- }
- Future<void> _onCreatedSharedLinkClicked() async {
- if (split.ownedByCurrentUser.isEmpty) {
- showShortToast(context, "Can only create link for files owned by you");
- return;
- }
- _cachedCollectionForSharedLink ??= await collectionActions
- .createSharedCollectionLink(context, split.ownedByCurrentUser);
- final actionResult = await showActionSheet(
- context: context,
- buttons: [
- const ButtonWidget(
- labelText: "Copy link",
- buttonType: ButtonType.neutral,
- buttonSize: ButtonSize.large,
- shouldStickToDarkTheme: true,
- buttonAction: ButtonAction.first,
- isInAlert: true,
- ),
- const ButtonWidget(
- labelText: "Manage link",
- buttonType: ButtonType.secondary,
- buttonSize: ButtonSize.large,
- buttonAction: ButtonAction.second,
- shouldStickToDarkTheme: true,
- isInAlert: true,
- ),
- const ButtonWidget(
- labelText: "Done",
- buttonType: ButtonType.secondary,
- buttonSize: ButtonSize.large,
- buttonAction: ButtonAction.third,
- shouldStickToDarkTheme: true,
- isInAlert: true,
- )
- ],
- title: "Public link created",
- body: "You can manage your links in the share tab.",
- actionSheetType: ActionSheetType.defaultActionSheet,
- );
- if (actionResult?.action != null) {
- if (actionResult!.action == ButtonAction.first) {
- await _copyLink();
- }
- if (actionResult.action == ButtonAction.second) {
- routeToPage(
- context,
- ManageSharedLinkWidget(collection: _cachedCollectionForSharedLink),
- );
- }
- }
- if (mounted) {
- setState(() => {});
- }
- }
- Future<void> _copyLink() async {
- if (_cachedCollectionForSharedLink != null) {
- final String collectionKey = Base58Encode(
- CollectionsService.instance
- .getCollectionKey(_cachedCollectionForSharedLink!.id),
- );
- final String url =
- "${_cachedCollectionForSharedLink!.publicURLs?.first?.url}#$collectionKey";
- await Clipboard.setData(ClipboardData(text: url));
- showShortToast(context, "Link copied to clipboard");
- }
- }
- void _restore() {
- createCollectionSheet(
- widget.selectedFiles,
- null,
- context,
- actionType: CollectionActionType.restoreFiles,
- );
- }
- Future<void> _permanentlyDelete() async {
- if (await deleteFromTrash(
- context,
- widget.selectedFiles.files.toList(),
- )) {
- widget.selectedFiles.clearAll();
- }
- }
- }
|