ente/lib/ui/actions/collection/collection_file_actions.dart

187 lines
6.3 KiB
Dart
Raw Normal View History

2022-12-15 10:02:46 +00:00
import 'package:flutter/cupertino.dart';
2023-06-27 11:26:16 +00:00
import "package:photos/core/configuration.dart";
import "package:photos/db/files_db.dart";
2023-04-05 07:50:02 +00:00
import "package:photos/generated/l10n.dart";
2022-12-15 10:02:46 +00:00
import 'package:photos/models/collection.dart';
import 'package:photos/models/file.dart';
import 'package:photos/models/selected_files.dart';
2023-06-27 11:26:16 +00:00
import "package:photos/services/collections_service.dart";
2022-12-15 10:02:46 +00:00
import 'package:photos/services/favorites_service.dart';
2023-06-27 11:26:16 +00:00
import "package:photos/services/ignored_files_service.dart";
import "package:photos/services/remote_sync_service.dart";
2022-12-15 10:02:46 +00:00
import 'package:photos/ui/actions/collection/collection_sharing_actions.dart';
import 'package:photos/ui/common/progress_dialog.dart';
import 'package:photos/ui/components/action_sheet_widget.dart';
import 'package:photos/ui/components/buttons/button_widget.dart';
import 'package:photos/ui/components/models/button_type.dart';
2022-12-15 10:02:46 +00:00
import 'package:photos/utils/dialog_util.dart';
2023-06-27 11:26:16 +00:00
import "package:photos/utils/share_util.dart";
2022-12-15 10:02:46 +00:00
import 'package:photos/utils/toast_util.dart';
2023-06-27 11:26:16 +00:00
import "package:receive_sharing_intent/receive_sharing_intent.dart";
2022-12-15 10:02:46 +00:00
extension CollectionFileActions on CollectionActions {
Future<void> showRemoveFromCollectionSheetV2(
BuildContext bContext,
Collection collection,
SelectedFiles selectedFiles,
bool removingOthersFile,
) async {
final actionResult = await showActionSheet(
context: bContext,
buttons: [
ButtonWidget(
2023-04-05 07:50:02 +00:00
labelText: S.of(bContext).remove,
buttonType:
removingOthersFile ? ButtonType.critical : ButtonType.neutral,
buttonSize: ButtonSize.large,
shouldStickToDarkTheme: true,
isInAlert: true,
onTap: () async {
2023-01-12 04:18:57 +00:00
try {
await moveFilesFromCurrentCollection(
bContext,
collection,
selectedFiles.files,
);
} catch (e) {
logger.severe("Failed to move files", e);
rethrow;
}
},
),
2023-04-05 07:50:02 +00:00
ButtonWidget(
labelText: S.of(bContext).cancel,
buttonType: ButtonType.secondary,
buttonSize: ButtonSize.large,
buttonAction: ButtonAction.second,
shouldStickToDarkTheme: true,
isInAlert: true,
),
],
title: removingOthersFile ? S.of(bContext).removeFromAlbumTitle : null,
body: removingOthersFile
2023-04-05 07:50:02 +00:00
? S.of(bContext).removeShareItemsWarning
: S.of(bContext).itemsWillBeRemovedFromAlbum,
actionSheetType: ActionSheetType.defaultActionSheet,
);
if (actionResult?.action != null &&
actionResult!.action == ButtonAction.error) {
showGenericErrorDialog(context: bContext);
} else {
selectedFiles.clearAll();
}
}
2023-06-27 11:26:16 +00:00
Future<bool> addToCollection(
BuildContext context,
int collectionID,
bool showProgressDialog, {
List<File>? selectedFiles,
List<SharedMediaFile>? sharedFiles,
}) async {
final dialog = showProgressDialog
? createProgressDialog(
context,
S.of(context).uploadingFilesToAlbum,
isDismissible: true,
)
: null;
await dialog?.show();
try {
final List<File> files = [];
final List<File> filesPendingUpload = [];
final int currentUserID = Configuration.instance.getUserID()!;
if (sharedFiles != null) {
filesPendingUpload.addAll(
await convertIncomingSharedMediaToFile(
sharedFiles,
collectionID,
),
);
} else {
for (final file in selectedFiles!) {
File? currentFile;
if (file.uploadedFileID != null) {
currentFile = file;
} else if (file.generatedID != null) {
// when file is not uploaded, refresh the state from the db to
// ensure we have latest upload status for given file before
// queueing it up as pending upload
currentFile = await (FilesDB.instance.getFile(file.generatedID!));
} else if (file.generatedID == null) {
logger.severe("generated id should not be null");
}
if (currentFile == null) {
logger.severe("Failed to find fileBy genID");
continue;
}
if (currentFile.uploadedFileID == null) {
currentFile.collectionID = collectionID;
filesPendingUpload.add(currentFile);
} else {
files.add(currentFile);
}
}
}
if (filesPendingUpload.isNotEmpty) {
// Newly created collection might not be cached
final Collection? c =
CollectionsService.instance.getCollectionByID(collectionID);
if (c != null && c.owner!.id != currentUserID) {
showToast(context, S.of(context).canNotUploadToAlbumsOwnedByOthers);
await dialog?.hide();
return false;
} else {
// filesPendingUpload might be getting ignored during auto-upload
// because the user deleted these files from ente in the past.
await IgnoredFilesService.instance
.removeIgnoredMappings(filesPendingUpload);
await FilesDB.instance.insertMultiple(filesPendingUpload);
}
}
if (files.isNotEmpty) {
await CollectionsService.instance.addToCollection(collectionID, files);
}
RemoteSyncService.instance.sync(silently: true);
await dialog?.hide();
return true;
} catch (e, s) {
logger.severe("Failed to add to album", e, s);
await dialog?.hide();
showGenericErrorDialog(context: context);
rethrow;
}
}
2022-12-15 10:02:46 +00:00
Future<bool> updateFavorites(
BuildContext context,
List<File> files,
bool markAsFavorite,
) async {
final ProgressDialog dialog = createProgressDialog(
context,
2023-04-05 07:50:02 +00:00
markAsFavorite
? S.of(context).addingToFavorites
: S.of(context).removingFromFavorites,
2022-12-15 10:02:46 +00:00
);
await dialog.show();
try {
await FavoritesService.instance
.updateFavorites(context, files, markAsFavorite);
2022-12-15 10:02:46 +00:00
return true;
} catch (e, s) {
logger.severe(e, s);
showShortToast(
context,
2023-04-05 07:50:02 +00:00
markAsFavorite
? S.of(context).sorryCouldNotAddToFavorites
: S.of(context).sorryCouldNotRemoveFromFavorites,
2022-12-15 10:02:46 +00:00
);
} finally {
await dialog.hide();
}
return false;
}
}