Support for keeping photos while deleting collection

This commit is contained in:
Neeraj Gupta 2023-01-12 15:43:11 +05:30
parent 5ca42098d5
commit 195275f8f1
No known key found for this signature in database
GPG key ID: 3C5A1684DC1729E1
2 changed files with 53 additions and 34 deletions

View file

@ -266,8 +266,10 @@ class CollectionActions {
}
}
Future<void> deleteCollectionSheet(
BuildContext bContext, Collection collection) async {
Future<bool> deleteCollectionSheet(
BuildContext bContext,
Collection collection,
) async {
final currentUserID = Configuration.instance.getUserID()!;
if (collection.owner!.id != currentUserID) {
throw AssertionError("Can not delete album owned by others");
@ -279,14 +281,24 @@ class CollectionActions {
labelText: "Keep Photos",
buttonType: ButtonType.neutral,
buttonSize: ButtonSize.large,
buttonAction: ButtonAction.first,
shouldStickToDarkTheme: true,
isInAlert: true,
onTap: () async {
// await moveFilesFromCurrentCollection(
// bContext,
// collection,
// selectedFiles.files,
// );
try {
final List<File> files =
await FilesDB.instance.getAllFilesCollection(collection.id);
await moveFilesFromCurrentCollection(
bContext,
collection,
files,
);
// collection should be empty on server now
await collectionsService.trashEmptyCollection(collection);
} catch (e) {
logger.severe("Failed to keep photos and delete collection", e);
rethrow;
}
},
),
const ButtonWidget(
@ -314,7 +326,14 @@ class CollectionActions {
);
if (actionResult != null && actionResult == ButtonAction.error) {
showGenericErrorDialog(context: bContext);
return false;
}
if (actionResult != null &&
(actionResult == ButtonAction.first ||
actionResult == ButtonAction.second)) {
return true;
}
return false;
}
/*

View file

@ -16,7 +16,7 @@ import 'package:photos/models/selected_files.dart';
import 'package:photos/services/collections_service.dart';
import 'package:photos/services/sync_service.dart';
import 'package:photos/services/update_service.dart';
import 'package:photos/ui/common/dialogs.dart';
import 'package:photos/ui/actions/collection/collection_sharing_actions.dart';
import 'package:photos/ui/common/rename_dialog.dart';
import 'package:photos/ui/components/button_widget.dart';
import 'package:photos/ui/components/dialog_widget.dart';
@ -55,6 +55,7 @@ class _GalleryAppBarWidgetState extends State<GalleryAppBarWidget> {
late StreamSubscription _userAuthEventSubscription;
late Function() _selectedFilesListener;
String? _appBarTitle;
late CollectionActions collectionActions;
final GlobalKey shareButtonKey = GlobalKey();
@override
@ -62,6 +63,7 @@ class _GalleryAppBarWidgetState extends State<GalleryAppBarWidget> {
_selectedFilesListener = () {
setState(() {});
};
collectionActions = CollectionActions(CollectionsService.instance);
widget.selectedFiles.addListener(_selectedFilesListener);
_userAuthEventSubscription =
Bus.instance.on<SubscriptionPurchasedEvent>().listen((event) {
@ -369,35 +371,33 @@ class _GalleryAppBarWidgetState extends State<GalleryAppBarWidget> {
)
?.thumbnail ==
null;
if (!isEmptyCollection) {
final result = await showChoiceDialog(
if (isEmptyCollection) {
final dialog = createProgressDialog(
context,
"Delete album?",
"Files that are unique to this album "
"will be moved to trash, and this album will be deleted.",
firstAction: "Cancel",
secondAction: "Delete album",
secondActionColor: Colors.red,
"Please wait, deleting album",
);
if (result != DialogUserChoice.secondChoice) {
return;
await dialog.show();
try {
await CollectionsService.instance
.trashEmptyCollection(widget.collection!);
showShortToast(context, "Successfully deleted album");
await dialog.hide();
Navigator.of(context).pop();
} catch (e, s) {
_logger.severe("failed to trash collection", e, s);
await dialog.hide();
showGenericErrorDialog(context: context);
}
} else {
final bool result = await collectionActions.deleteCollectionSheet(
context,
widget.collection!,
);
if (result == true) {
Navigator.of(context).pop();
} else {
debugPrint("No pop");
}
}
final dialog = createProgressDialog(
context,
"Please wait, deleting album",
);
await dialog.show();
try {
await CollectionsService.instance
.trashCollection(widget.collection!, isEmptyCollection);
showShortToast(context, "Successfully deleted album");
await dialog.hide();
Navigator.of(context).pop();
} catch (e, s) {
_logger.severe("failed to trash collection", e, s);
await dialog.hide();
showGenericErrorDialog(context: context);
}
}