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

318 lines
9.7 KiB
Dart
Raw Normal View History

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
2022-12-15 10:02:46 +00:00
import 'package:page_transition/page_transition.dart';
import 'package:photos/core/configuration.dart';
import 'package:photos/models/collection.dart';
import 'package:photos/models/device_collection.dart';
import 'package:photos/models/gallery_type.dart';
2022-12-15 11:24:51 +00:00
import 'package:photos/models/magic_metadata.dart';
import 'package:photos/models/selected_file_breakup.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';
import 'package:photos/theme/ente_theme.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';
import 'package:photos/ui/components/blur_menu_item_widget.dart';
import 'package:photos/ui/components/bottom_action_bar/expanded_menu_widget.dart';
2022-12-15 10:02:46 +00:00
import 'package:photos/ui/create_collection_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';
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 SelectedFileSplit split;
2022-12-15 10:02:46 +00:00
late CollectionActions collectionActions;
@override
void initState() {
currentUserID = Configuration.instance.getUserID()!;
split = widget.selectedFiles.split(currentUserID);
widget.selectedFiles.addListener(_selectFileChangeListener);
2022-12-15 10:02:46 +00:00
collectionActions = CollectionActions(CollectionsService.instance);
super.initState();
}
@override
void dispose() {
widget.selectedFiles.removeListener(_selectFileChangeListener);
super.dispose();
}
void _selectFileChangeListener() {
split = widget.selectedFiles.split(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})"
""
: "";
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})"
""
: "";
final bool anyOwnedFiles =
split.pendingUploads.isNotEmpty || split.ownedByCurrentUser.isNotEmpty;
final bool anyUploadedFiles = split.ownedByCurrentUser.isNotEmpty;
bool showRemoveOption = widget.type.showRemoveFromAlbum();
if (showRemoveOption && widget.type == GalleryType.sharedCollection) {
showRemoveOption = split.ownedByCurrentUser.isNotEmpty;
}
debugPrint('$runtimeType building $mounted');
final colorScheme = getEnteColorScheme(context);
final List<List<BlurMenuItemWidget>> items = [];
final List<BlurMenuItemWidget> firstList = [];
2022-12-16 10:08:35 +00:00
final showUploadIcon = widget.type == GalleryType.localFolder &&
split.ownedByCurrentUser.isEmpty;
if (widget.type.showAddToAlbum()) {
firstList.add(
BlurMenuItemWidget(
2022-12-16 10:08:35 +00:00
leadingIcon:
showUploadIcon ? Icons.cloud_upload_outlined : Icons.add_outlined,
labelText:
"Add to ${showUploadIcon ? 'ente' : 'album'}$suffixInPending",
menuItemColor: colorScheme.fillFaint,
2022-12-16 07:55:07 +00:00
onTap: anyOwnedFiles ? _addToAlbum : null,
),
);
}
if (widget.type.showMoveToAlbum()) {
firstList.add(
BlurMenuItemWidget(
leadingIcon: Icons.arrow_forward_outlined,
labelText: "Move to album$suffix",
menuItemColor: colorScheme.fillFaint,
2022-12-16 07:55:07 +00:00
onTap: anyUploadedFiles ? _moveFiles : null,
),
);
}
if (showRemoveOption) {
firstList.add(
BlurMenuItemWidget(
leadingIcon: Icons.remove_outlined,
labelText: "Remove from album$suffix",
menuItemColor: colorScheme.fillFaint,
2022-12-16 07:55:07 +00:00
onTap: anyUploadedFiles ? _removeFilesFromAlbum : null,
),
);
}
if (widget.type.showDeleteOption()) {
firstList.add(
BlurMenuItemWidget(
leadingIcon: Icons.delete_outline,
2022-12-16 07:55:07 +00:00
labelText: "Delete$suffixInPending",
menuItemColor: colorScheme.fillFaint,
2022-12-16 07:55:07 +00:00
onTap: anyOwnedFiles ? _onDeleteClick : null,
),
);
}
if (widget.type.showHideOption()) {
firstList.add(
BlurMenuItemWidget(
leadingIcon: Icons.visibility_off_outlined,
labelText: "Hide$suffix",
menuItemColor: colorScheme.fillFaint,
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()) {
firstList.add(
BlurMenuItemWidget(
leadingIcon: Icons.visibility_off_outlined,
labelText: "Unhide$suffix",
menuItemColor: colorScheme.fillFaint,
onTap: _onUnhideClick,
),
);
}
if (widget.type.showArchiveOption()) {
firstList.add(
BlurMenuItemWidget(
leadingIcon: Icons.archive_outlined,
labelText: "Archive$suffix",
menuItemColor: colorScheme.fillFaint,
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()) {
firstList.add(
BlurMenuItemWidget(
2022-12-16 07:27:53 +00:00
leadingIcon: Icons.unarchive,
2022-12-15 11:24:51 +00:00
labelText: "Unarchive$suffix",
menuItemColor: colorScheme.fillFaint,
onTap: _onUnArchiveClick,
),
);
}
if (widget.type.showFavoriteOption()) {
firstList.add(
BlurMenuItemWidget(
leadingIcon: Icons.favorite_border_rounded,
labelText: "Favorite$suffix",
menuItemColor: colorScheme.fillFaint,
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()) {
firstList.add(
BlurMenuItemWidget(
leadingIcon: Icons.favorite,
labelText: "Remove from favorite$suffix",
menuItemColor: colorScheme.fillFaint,
onTap: _onUnFavoriteClick,
),
);
}
if (firstList.isNotEmpty) {
items.add(firstList);
return ExpandedMenuWidget(
items: items,
);
} 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);
}
await _selectionCollectionForAction(CollectionActionType.moveFiles);
}
Future<void> _addToAlbum() async {
if (split.ownedByOtherUsers.isNotEmpty) {
widget.selectedFiles
.unSelectAll(split.ownedByOtherUsers.toSet(), skipNotify: true);
}
await _selectionCollectionForAction(CollectionActionType.addFiles);
}
2022-12-15 11:24:51 +00:00
Future<void> _onDeleteClick() async {
showDeleteSheet(context, widget.selectedFiles);
}
2022-12-15 10:02:46 +00:00
Future<void> _removeFilesFromAlbum() async {
2022-12-15 16:25:29 +00:00
if (split.pendingUploads.isNotEmpty || split.ownedByOtherUsers.isNotEmpty) {
widget.selectedFiles
.unSelectAll(split.pendingUploads.toSet(), skipNotify: true);
widget.selectedFiles
.unSelectAll(split.ownedByOtherUsers.toSet(), skipNotify: true);
}
2022-12-15 10:02:46 +00:00
await collectionActions.showRemoveFromCollectionSheet(
context,
widget.collection!,
widget.selectedFiles,
);
}
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,
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);
}
await _selectionCollectionForAction(CollectionActionType.unHide);
}
2022-12-15 10:02:46 +00:00
Future<Object?> _selectionCollectionForAction(
CollectionActionType type,
) async {
return Navigator.push(
context,
PageTransition(
type: PageTransitionType.bottomToTop,
child: CreateCollectionPage(
widget.selectedFiles,
null,
actionType: type,
),
),
);
}
}