2023-01-03 08:29:26 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:photos/core/event_bus.dart';
|
|
|
|
import 'package:photos/events/collection_updated_event.dart';
|
2023-04-07 03:21:49 +00:00
|
|
|
import "package:photos/generated/l10n.dart";
|
2023-01-12 04:26:01 +00:00
|
|
|
import 'package:photos/models/collection.dart';
|
2023-01-03 08:29:26 +00:00
|
|
|
import 'package:photos/models/file.dart';
|
|
|
|
import 'package:photos/services/collections_service.dart';
|
|
|
|
import 'package:photos/ui/components/action_sheet_widget.dart';
|
2023-03-10 08:08:51 +00:00
|
|
|
import 'package:photos/ui/components/buttons/button_widget.dart';
|
2023-01-03 08:29:26 +00:00
|
|
|
import 'package:photos/ui/components/models/button_type.dart';
|
|
|
|
|
|
|
|
class DeleteEmptyAlbums extends StatefulWidget {
|
|
|
|
const DeleteEmptyAlbums({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<DeleteEmptyAlbums> createState() => _DeleteEmptyAlbumsState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _DeleteEmptyAlbumsState extends State<DeleteEmptyAlbums> {
|
|
|
|
final ValueNotifier<String> _deleteProgress = ValueNotifier("");
|
|
|
|
bool _isCancelled = false;
|
|
|
|
|
2023-01-03 09:00:10 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_deleteProgress.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2023-01-03 08:29:26 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-03-07 13:54:26 +00:00
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.fromLTRB(0, 2, 8, 12),
|
|
|
|
child: Align(
|
|
|
|
alignment: Alignment.centerLeft,
|
|
|
|
child: ButtonWidget(
|
|
|
|
buttonSize: ButtonSize.small,
|
|
|
|
buttonType: ButtonType.secondary,
|
2023-04-07 03:21:49 +00:00
|
|
|
labelText: S.of(context).deleteEmptyAlbums,
|
2023-03-07 13:54:26 +00:00
|
|
|
icon: Icons.delete_sweep_outlined,
|
|
|
|
shouldSurfaceExecutionStates: false,
|
|
|
|
onTap: () async {
|
|
|
|
await showActionSheet(
|
|
|
|
context: context,
|
|
|
|
isDismissible: true,
|
|
|
|
buttons: [
|
|
|
|
ButtonWidget(
|
2023-04-07 03:21:49 +00:00
|
|
|
labelText: S.of(context).yes,
|
2023-03-07 13:54:26 +00:00
|
|
|
buttonType: ButtonType.neutral,
|
|
|
|
buttonSize: ButtonSize.large,
|
|
|
|
shouldStickToDarkTheme: true,
|
|
|
|
shouldSurfaceExecutionStates: true,
|
|
|
|
progressStatus: _deleteProgress,
|
|
|
|
onTap: () async {
|
|
|
|
await _deleteEmptyAlbums();
|
|
|
|
if (!_isCancelled) {
|
|
|
|
Navigator.of(context, rootNavigator: true).pop();
|
|
|
|
}
|
|
|
|
Bus.instance.fire(
|
|
|
|
CollectionUpdatedEvent(
|
|
|
|
0,
|
|
|
|
<File>[],
|
|
|
|
"empty_albums_deleted",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
CollectionsService.instance.sync().ignore();
|
|
|
|
_isCancelled = false;
|
|
|
|
},
|
|
|
|
),
|
|
|
|
ButtonWidget(
|
2023-04-07 03:21:49 +00:00
|
|
|
labelText: S.of(context).cancel,
|
2023-03-07 13:54:26 +00:00
|
|
|
buttonType: ButtonType.secondary,
|
|
|
|
buttonSize: ButtonSize.large,
|
|
|
|
shouldStickToDarkTheme: true,
|
|
|
|
onTap: () async {
|
|
|
|
_isCancelled = true;
|
2023-01-03 08:29:26 +00:00
|
|
|
Navigator.of(context, rootNavigator: true).pop();
|
2023-03-07 13:54:26 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
],
|
2023-04-07 03:21:49 +00:00
|
|
|
title: S.of(context).deleteEmptyAlbumsWithQuestionMark,
|
|
|
|
body: S.of(context).deleteAlbumsDialogBody,
|
2023-03-07 13:54:26 +00:00
|
|
|
actionSheetType: ActionSheetType.defaultActionSheet,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2023-01-03 08:29:26 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-03 08:33:26 +00:00
|
|
|
Future<void> _deleteEmptyAlbums() async {
|
2023-01-03 08:29:26 +00:00
|
|
|
final collections =
|
|
|
|
await CollectionsService.instance.getCollectionsWithThumbnails();
|
2023-01-17 07:43:11 +00:00
|
|
|
// remove collections which are not empty or can't be deleted
|
2023-01-12 04:26:01 +00:00
|
|
|
collections.removeWhere(
|
|
|
|
(element) =>
|
2023-01-17 07:43:11 +00:00
|
|
|
element.thumbnail != null || !element.collection.type.canDelete,
|
2023-01-12 04:26:01 +00:00
|
|
|
);
|
2023-01-03 08:29:26 +00:00
|
|
|
int failedCount = 0;
|
|
|
|
for (int i = 0; i < collections.length; i++) {
|
|
|
|
if (mounted && !_isCancelled) {
|
2023-04-07 03:21:49 +00:00
|
|
|
final String currentlyDeleting = (i + 1)
|
|
|
|
.toString()
|
|
|
|
.padLeft(collections.length.toString().length, '0');
|
2023-01-03 08:29:26 +00:00
|
|
|
_deleteProgress.value =
|
2023-04-07 03:21:49 +00:00
|
|
|
S.of(context).deleteProgress(currentlyDeleting, collections.length);
|
2023-01-03 08:29:26 +00:00
|
|
|
try {
|
2023-01-12 11:32:52 +00:00
|
|
|
await CollectionsService.instance.trashEmptyCollection(
|
2023-02-21 14:32:29 +00:00
|
|
|
collections[i].collection,
|
|
|
|
isBulkDelete: true,
|
|
|
|
);
|
2023-01-03 08:29:26 +00:00
|
|
|
} catch (_) {
|
|
|
|
failedCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (failedCount > 0) {
|
|
|
|
debugPrint("Delete ops failed for $failedCount collections");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|