ente/lib/ui/viewer/actions/file_selection_actions_widget.dart

497 lines
15 KiB
Dart
Raw Normal View History

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/generated/l10n.dart";
import 'package:photos/models/collection.dart';
import 'package:photos/models/device_collection.dart';
import 'package:photos/models/file.dart';
2023-05-26 07:03:29 +00:00
import "package:photos/models/file_type.dart";
2023-01-05 04:04:34 +00:00
import 'package:photos/models/files_split.dart';
import 'package:photos/models/gallery_type.dart';
import "package:photos/models/metadata/common_keys.dart";
import 'package:photos/models/selected_files.dart';
2022-12-15 10:02:46 +00:00
import 'package:photos/services/collections_service.dart';
2022-12-15 11:24:51 +00:00
import 'package:photos/services/hidden_service.dart';
2022-12-15 10:02:46 +00:00
import 'package:photos/ui/actions/collection/collection_file_actions.dart';
import 'package:photos/ui/actions/collection/collection_sharing_actions.dart';
2023-06-06 09:57:17 +00:00
import 'package:photos/ui/collections/collection_action_sheet.dart';
2022-12-22 10:31:10 +00:00
import 'package:photos/ui/components/action_sheet_widget.dart';
import "package:photos/ui/components/bottom_action_bar/bottom_action_bar_widget.dart";
import 'package:photos/ui/components/buttons/button_widget.dart';
2022-12-22 10:31:10 +00:00
import 'package:photos/ui/components/models/button_type.dart';
2022-12-22 17:13:14 +00:00
import 'package:photos/ui/sharing/manage_links_widget.dart';
2023-05-26 13:10:39 +00:00
import "package:photos/ui/tools/collage/collage_creator_page.dart";
2022-12-15 11:24:51 +00:00
import 'package:photos/utils/delete_file_util.dart';
import 'package:photos/utils/magic_util.dart';
2022-12-22 17:13:14 +00:00
import 'package:photos/utils/navigation_util.dart';
import 'package:photos/utils/toast_util.dart';
class FileSelectionActionsWidget extends StatefulWidget {
final GalleryType type;
final Collection? collection;
final DeviceCollection? deviceCollection;
final SelectedFiles selectedFiles;
const FileSelectionActionsWidget(
this.type,
this.selectedFiles, {
Key? key,
this.collection,
this.deviceCollection,
}) : super(key: key);
@override
State<FileSelectionActionsWidget> createState() =>
_FileSelectionActionsWidgetState();
}
class _FileSelectionActionsWidgetState
extends State<FileSelectionActionsWidget> {
late int currentUserID;
2023-01-05 04:04:34 +00:00
late FilesSplit split;
2022-12-15 10:02:46 +00:00
late CollectionActions collectionActions;
late bool isCollectionOwner;
2023-02-22 07:38:31 +00:00
// _cachedCollectionForSharedLink is primarily 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);
2022-12-15 10:02:46 +00:00
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;
}
2023-01-05 04:04:34 +00:00
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)"
""
: "";
2022-12-16 10:08:35 +00:00
final String suffixInPending = split.ownedByOtherUsers.isNotEmpty
2022-12-16 07:55:07 +00:00
? " (${split.ownedByCurrentUser.length + split.pendingUploads.length})"
""
: "";
2022-12-16 07:55:07 +00:00
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 List<SelectionOptionButton> items = [];
if (widget.type.showCreateLink()) {
if (_cachedCollectionForSharedLink != null && anyUploadedFiles) {
items.add(
SelectionOptionButton(
icon: Icons.copy_outlined,
labelText: S.of(context).copyLink,
onTap: anyUploadedFiles ? _copyLink : null,
),
);
} else {
items.add(
SelectionOptionButton(
icon: Icons.link_outlined,
labelText: S.of(context).shareLink + suffix,
onTap: anyUploadedFiles ? _onCreatedSharedLinkClicked : null,
),
);
}
}
2023-05-26 07:03:29 +00:00
bool hasVideoFile = false;
for (final file in widget.selectedFiles.files) {
if (file.fileType == FileType.video) {
hasVideoFile = true;
}
}
if (!hasVideoFile &&
widget.selectedFiles.files.length >=
2023-05-26 15:27:43 +00:00
CollageCreatorPage.collageItemsMin &&
2023-05-26 07:03:29 +00:00
widget.selectedFiles.files.length <=
2023-05-26 15:27:43 +00:00
CollageCreatorPage.collageItemsMax) {
items.add(
SelectionOptionButton(
icon: Icons.grid_view_outlined,
2023-05-26 07:03:29 +00:00
labelText: S.of(context).createCollage,
onTap: _onCreateCollageClicked,
2023-05-26 07:03:29 +00:00
),
);
}
2022-12-16 10:08:35 +00:00
final showUploadIcon = widget.type == GalleryType.localFolder &&
split.ownedByCurrentUser.isEmpty;
if (widget.type.showAddToAlbum()) {
items.add(
SelectionOptionButton(
icon:
2022-12-16 10:08:35 +00:00
showUploadIcon ? Icons.cloud_upload_outlined : Icons.add_outlined,
labelText: showUploadIcon
? S.of(context).addToEnte
: S.of(context).addToAlbum + suffixInPending,
2022-12-16 07:55:07 +00:00
onTap: anyOwnedFiles ? _addToAlbum : null,
),
);
}
if (widget.type.showMoveToAlbum()) {
items.add(
SelectionOptionButton(
icon: Icons.arrow_forward_outlined,
labelText: S.of(context).moveToAlbum + suffix,
2022-12-16 07:55:07 +00:00
onTap: anyUploadedFiles ? _moveFiles : null,
),
);
}
if (showRemoveOption) {
items.add(
SelectionOptionButton(
icon: Icons.remove_outlined,
labelText: "${S.of(context).removeFromAlbum}$removeSuffix",
2023-01-28 07:33:25 +00:00
onTap: removeCount > 0 ? _removeFilesFromAlbum : null,
),
);
}
if (widget.type.showDeleteOption()) {
items.add(
SelectionOptionButton(
icon: Icons.delete_outline,
labelText: S.of(context).delete + suffixInPending,
2022-12-16 07:55:07 +00:00
onTap: anyOwnedFiles ? _onDeleteClick : null,
),
);
}
if (widget.type.showHideOption()) {
items.add(
SelectionOptionButton(
icon: Icons.visibility_off_outlined,
labelText: S.of(context).hide + suffix,
2022-12-16 07:55:07 +00:00
onTap: anyUploadedFiles ? _onHideClick : null,
2022-12-15 11:24:51 +00:00
),
);
} else if (widget.type.showUnHideOption()) {
items.add(
SelectionOptionButton(
icon: Icons.visibility_off_outlined,
labelText: S.of(context).unhide + suffix,
2022-12-15 11:24:51 +00:00
onTap: _onUnhideClick,
),
);
}
if (widget.type.showArchiveOption()) {
items.add(
SelectionOptionButton(
icon: Icons.archive_outlined,
labelText: S.of(context).archive + suffix,
2022-12-16 07:55:07 +00:00
onTap: anyUploadedFiles ? _onArchiveClick : null,
2022-12-15 11:24:51 +00:00
),
);
} else if (widget.type.showUnArchiveOption()) {
items.add(
SelectionOptionButton(
icon: Icons.unarchive,
labelText: S.of(context).unarchive + suffix,
2022-12-15 11:24:51 +00:00
onTap: _onUnArchiveClick,
),
);
}
if (widget.type.showFavoriteOption()) {
items.add(
SelectionOptionButton(
icon: Icons.favorite_border_rounded,
labelText: S.of(context).favorite + suffix,
2022-12-16 07:55:07 +00:00
onTap: anyUploadedFiles ? _onFavoriteClick : null,
),
);
2022-12-15 11:24:51 +00:00
} else if (widget.type.showUnFavoriteOption()) {
items.add(
SelectionOptionButton(
icon: Icons.favorite,
labelText: S.of(context).removeFromFavorite + suffix,
2022-12-15 11:24:51 +00:00
onTap: _onUnFavoriteClick,
),
);
}
2023-01-27 13:56:29 +00:00
if (widget.type.showRestoreOption()) {
items.add(
SelectionOptionButton(
icon: Icons.restore_outlined,
labelText: S.of(context).restore,
2023-01-27 13:56:29 +00:00
onTap: _restore,
),
);
}
if (widget.type.showPermanentlyDeleteOption()) {
items.add(
SelectionOptionButton(
icon: Icons.delete_forever_outlined,
labelText: S.of(context).permanentlyDelete,
2023-01-27 13:56:29 +00:00
onTap: _permanentlyDelete,
),
);
}
if (items.isNotEmpty) {
return SingleChildScrollView(
physics: const BouncingScrollPhysics(),
scrollDirection: Axis.horizontal,
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(width: 8),
...items,
const SizedBox(width: 8),
],
),
);
} else {
// TODO: Return "Select All" here
return const SizedBox.shrink();
}
}
2022-12-15 10:02:46 +00:00
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);
}
2023-02-14 10:13:34 +00:00
showCollectionActionSheet(
context,
2023-02-14 10:13:34 +00:00
selectedFiles: widget.selectedFiles,
2023-01-25 04:57:40 +00:00
actionType: CollectionActionType.moveFiles,
);
2022-12-15 10:02:46 +00:00
}
Future<void> _addToAlbum() async {
if (split.ownedByOtherUsers.isNotEmpty) {
widget.selectedFiles
.unSelectAll(split.ownedByOtherUsers.toSet(), skipNotify: true);
}
2023-02-14 10:13:34 +00:00
showCollectionActionSheet(context, selectedFiles: widget.selectedFiles);
2022-12-15 10:02:46 +00:00
}
2022-12-15 11:24:51 +00:00
Future<void> _onDeleteClick() async {
2023-01-06 07:24:00 +00:00
return showDeleteSheet(context, widget.selectedFiles);
2022-12-15 11:24:51 +00:00
}
2022-12-15 10:02:46 +00:00
Future<void> _removeFilesFromAlbum() async {
if (split.pendingUploads.isNotEmpty) {
2022-12-15 16:25:29 +00:00
widget.selectedFiles
.unSelectAll(split.pendingUploads.toSet(), skipNotify: true);
}
if (!isCollectionOwner && split.ownedByOtherUsers.isNotEmpty) {
2022-12-15 16:25:29 +00:00
widget.selectedFiles
.unSelectAll(split.ownedByOtherUsers.toSet(), skipNotify: true);
}
final bool removingOthersFile =
isCollectionOwner && split.ownedByOtherUsers.isNotEmpty;
await collectionActions.showRemoveFromCollectionSheetV2(
2022-12-15 10:02:46 +00:00
context,
widget.collection!,
widget.selectedFiles,
removingOthersFile,
2022-12-15 10:02:46 +00:00
);
}
Future<void> _onFavoriteClick() async {
final result = await collectionActions.updateFavorites(
context,
split.ownedByCurrentUser,
true,
);
if (result) {
widget.selectedFiles.clearAll();
}
}
2022-12-15 11:24:51 +00:00
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,
archiveVisibility,
2022-12-15 11:24:51 +00:00
);
widget.selectedFiles.clearAll();
}
Future<void> _onUnArchiveClick() async {
await changeVisibility(
context,
split.ownedByCurrentUser,
visibleVisibility,
2022-12-15 11:24:51 +00:00
);
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);
}
2023-02-14 10:13:34 +00:00
showCollectionActionSheet(
context,
2023-02-14 10:13:34 +00:00
selectedFiles: widget.selectedFiles,
2023-01-25 04:57:40 +00:00
actionType: CollectionActionType.unHide,
);
2022-12-15 11:24:51 +00:00
}
Future<void> _onCreateCollageClicked() async {
final bool? result = await routeToPage(
context,
CollageCreatorPage(widget.selectedFiles.files.toList()),
);
2023-05-27 06:01:35 +00:00
if (result != null && result) {
widget.selectedFiles.clearAll();
}
}
Future<void> _onCreatedSharedLinkClicked() async {
if (split.ownedByCurrentUser.isEmpty) {
showShortToast(
2023-04-18 09:05:21 +00:00
context,
S.of(context).canOnlyCreateLinkForFilesOwnedByYou,
);
return;
}
_cachedCollectionForSharedLink ??= await collectionActions
.createSharedCollectionLink(context, split.ownedByCurrentUser);
2022-12-22 10:31:10 +00:00
final actionResult = await showActionSheet(
context: context,
buttons: [
ButtonWidget(
labelText: S.of(context).copyLink,
2022-12-22 10:31:10 +00:00
buttonType: ButtonType.neutral,
buttonSize: ButtonSize.large,
shouldStickToDarkTheme: true,
buttonAction: ButtonAction.first,
isInAlert: true,
),
ButtonWidget(
labelText: S.of(context).manageLink,
2022-12-22 10:31:10 +00:00
buttonType: ButtonType.secondary,
buttonSize: ButtonSize.large,
buttonAction: ButtonAction.second,
shouldStickToDarkTheme: true,
isInAlert: true,
2022-12-22 17:13:14 +00:00
),
ButtonWidget(
labelText: S.of(context).done,
2022-12-22 17:13:14 +00:00
buttonType: ButtonType.secondary,
buttonSize: ButtonSize.large,
buttonAction: ButtonAction.third,
shouldStickToDarkTheme: true,
isInAlert: true,
2022-12-22 10:31:10 +00:00
)
],
title: S.of(context).publicLinkCreated,
body: S.of(context).youCanManageYourLinksInTheShareTab,
2022-12-22 10:31:10 +00:00
actionSheetType: ActionSheetType.defaultActionSheet,
);
if (actionResult?.action != null) {
if (actionResult!.action == ButtonAction.first) {
await _copyLink();
}
if (actionResult.action == ButtonAction.second) {
routeToPage(
context,
ManageSharedLinkWidget(collection: _cachedCollectionForSharedLink),
);
}
2022-12-22 17:13:14 +00:00
}
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, S.of(context).linkCopiedToClipboard);
}
}
2023-01-27 13:56:29 +00:00
void _restore() {
2023-02-14 10:13:34 +00:00
showCollectionActionSheet(
2023-01-27 13:56:29 +00:00
context,
2023-02-14 10:13:34 +00:00
selectedFiles: widget.selectedFiles,
2023-01-27 13:56:29 +00:00
actionType: CollectionActionType.restoreFiles,
);
}
Future<void> _permanentlyDelete() async {
if (await deleteFromTrash(
context,
widget.selectedFiles.files.toList(),
2023-01-27 13:56:29 +00:00
)) {
widget.selectedFiles.clearAll();
}
}
}