2021-12-02 11:47:37 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:nc_photos/account.dart';
|
|
|
|
import 'package:nc_photos/app_localizations.dart';
|
2024-02-04 15:29:50 +01:00
|
|
|
import 'package:nc_photos/controller/files_controller.dart';
|
2022-10-15 16:29:18 +02:00
|
|
|
import 'package:nc_photos/entity/file_descriptor.dart';
|
2021-12-02 11:47:37 +01:00
|
|
|
import 'package:nc_photos/k.dart' as k;
|
2022-04-22 21:09:44 +02:00
|
|
|
import 'package:nc_photos/navigation_manager.dart';
|
2021-12-02 11:47:37 +01:00
|
|
|
import 'package:nc_photos/snack_bar_manager.dart';
|
2022-04-22 21:09:44 +02:00
|
|
|
import 'package:nc_photos/widget/trashbin_browser.dart';
|
2022-12-16 16:01:04 +01:00
|
|
|
import 'package:np_codegen/np_codegen.dart';
|
2021-12-02 11:47:37 +01:00
|
|
|
|
2022-12-16 16:01:04 +01:00
|
|
|
part 'remove_selection_handler.g.dart';
|
|
|
|
|
|
|
|
@npLog
|
2021-12-02 11:47:37 +01:00
|
|
|
class RemoveSelectionHandler {
|
2024-02-04 15:29:50 +01:00
|
|
|
const RemoveSelectionHandler({
|
|
|
|
required this.filesController,
|
|
|
|
});
|
2022-10-15 16:29:18 +02:00
|
|
|
|
2021-12-02 11:47:37 +01:00
|
|
|
/// Remove [selectedFiles] and return the removed count
|
|
|
|
Future<int> call({
|
|
|
|
required Account account,
|
2022-10-15 16:29:18 +02:00
|
|
|
required List<FileDescriptor> selection,
|
2021-12-02 11:47:37 +01:00
|
|
|
bool shouldCleanupAlbum = true,
|
|
|
|
bool isRemoveOpened = false,
|
2022-04-22 21:09:44 +02:00
|
|
|
bool isMoveToTrash = false,
|
2021-12-02 11:47:37 +01:00
|
|
|
}) async {
|
2024-02-04 15:29:50 +01:00
|
|
|
final String successText;
|
2021-12-02 11:47:37 +01:00
|
|
|
final String Function(int) failureText;
|
|
|
|
if (isRemoveOpened) {
|
|
|
|
successText = L10n.global().deleteSuccessNotification;
|
|
|
|
failureText = (_) => L10n.global().deleteFailureNotification;
|
|
|
|
} else {
|
|
|
|
successText = L10n.global().deleteSelectedSuccessNotification;
|
|
|
|
failureText =
|
|
|
|
(count) => L10n.global().deleteSelectedFailureNotification(count);
|
|
|
|
}
|
|
|
|
|
|
|
|
var failureCount = 0;
|
2024-02-04 15:29:50 +01:00
|
|
|
await filesController.remove(
|
|
|
|
selection,
|
|
|
|
errorBuilder: (fileIds) {
|
|
|
|
failureCount = fileIds.length;
|
|
|
|
return RemoveFailureError(fileIds);
|
2021-12-02 11:47:37 +01:00
|
|
|
},
|
|
|
|
);
|
2022-04-22 21:09:44 +02:00
|
|
|
final trashAction = isMoveToTrash
|
|
|
|
? SnackBarAction(
|
|
|
|
label: L10n.global().albumTrashLabel,
|
|
|
|
onPressed: () {
|
|
|
|
NavigationManager().getNavigator()?.pushNamed(
|
|
|
|
TrashbinBrowser.routeName,
|
|
|
|
arguments: TrashbinBrowserArguments(account));
|
|
|
|
},
|
|
|
|
)
|
|
|
|
: null;
|
2021-12-02 11:47:37 +01:00
|
|
|
if (failureCount == 0) {
|
|
|
|
SnackBarManager().showSnackBar(SnackBar(
|
|
|
|
content: Text(successText),
|
|
|
|
duration: k.snackBarDurationNormal,
|
2022-04-22 21:09:44 +02:00
|
|
|
action: trashAction,
|
2021-12-02 11:47:37 +01:00
|
|
|
));
|
|
|
|
} else {
|
|
|
|
SnackBarManager().showSnackBar(SnackBar(
|
|
|
|
content: Text(failureText(failureCount)),
|
|
|
|
duration: k.snackBarDurationNormal,
|
2022-04-22 21:09:44 +02:00
|
|
|
action: trashAction,
|
2021-12-02 11:47:37 +01:00
|
|
|
));
|
|
|
|
}
|
2024-02-04 15:29:50 +01:00
|
|
|
return selection.length - failureCount;
|
2021-12-02 11:47:37 +01:00
|
|
|
}
|
|
|
|
|
2024-02-04 15:29:50 +01:00
|
|
|
final FilesController filesController;
|
2021-12-02 11:47:37 +01:00
|
|
|
}
|