2021-12-02 11:47:37 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2021-12-03 18:31:27 +01:00
|
|
|
import 'package:kiwi/kiwi.dart';
|
2021-12-02 11:47:37 +01:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:nc_photos/account.dart';
|
|
|
|
import 'package:nc_photos/app_localizations.dart';
|
|
|
|
import 'package:nc_photos/debug_util.dart';
|
2021-12-03 18:31:27 +01:00
|
|
|
import 'package:nc_photos/di_container.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-10-15 16:29:18 +02:00
|
|
|
import 'package:nc_photos/use_case/inflate_file_descriptor.dart';
|
2021-12-02 11:47:37 +01:00
|
|
|
import 'package:nc_photos/use_case/remove.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 {
|
2022-10-15 16:29:18 +02:00
|
|
|
RemoveSelectionHandler(this._c)
|
|
|
|
: assert(require(_c)),
|
|
|
|
assert(InflateFileDescriptor.require(_c));
|
|
|
|
|
|
|
|
static bool require(DiContainer c) => true;
|
|
|
|
|
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,
|
2022-10-16 18:14:34 +02:00
|
|
|
bool shouldShowProcessingText = true,
|
2021-12-02 11:47:37 +01:00
|
|
|
}) async {
|
2022-10-15 16:29:18 +02:00
|
|
|
final selectedFiles = await InflateFileDescriptor(_c)(account, selection);
|
2021-12-02 11:47:37 +01:00
|
|
|
final String processingText, successText;
|
|
|
|
final String Function(int) failureText;
|
|
|
|
if (isRemoveOpened) {
|
|
|
|
processingText = L10n.global().deleteProcessingNotification;
|
|
|
|
successText = L10n.global().deleteSuccessNotification;
|
|
|
|
failureText = (_) => L10n.global().deleteFailureNotification;
|
|
|
|
} else {
|
|
|
|
processingText = L10n.global()
|
|
|
|
.deleteSelectedProcessingNotification(selectedFiles.length);
|
|
|
|
successText = L10n.global().deleteSelectedSuccessNotification;
|
|
|
|
failureText =
|
|
|
|
(count) => L10n.global().deleteSelectedFailureNotification(count);
|
|
|
|
}
|
2022-10-16 18:14:34 +02:00
|
|
|
if (shouldShowProcessingText) {
|
|
|
|
SnackBarManager().showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text(processingText),
|
|
|
|
duration: k.snackBarDurationShort,
|
|
|
|
),
|
|
|
|
canBeReplaced: true,
|
|
|
|
);
|
|
|
|
}
|
2021-12-02 11:47:37 +01:00
|
|
|
|
|
|
|
var failureCount = 0;
|
2021-12-03 18:31:27 +01:00
|
|
|
await Remove(KiwiContainer().resolve<DiContainer>())(
|
2021-12-02 11:47:37 +01:00
|
|
|
account,
|
|
|
|
selectedFiles,
|
2023-04-13 17:32:31 +02:00
|
|
|
onError: (_, file, e, stackTrace) {
|
2021-12-02 11:47:37 +01:00
|
|
|
_log.shout(
|
2023-04-13 17:32:31 +02:00
|
|
|
"[call] Failed while removing file: ${logFilename(file.fdPath)}",
|
2021-12-02 11:47:37 +01:00
|
|
|
e,
|
|
|
|
stackTrace);
|
|
|
|
++failureCount;
|
|
|
|
},
|
2021-12-03 18:31:27 +01:00
|
|
|
shouldCleanUp: shouldCleanupAlbum,
|
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
|
|
|
));
|
|
|
|
}
|
|
|
|
return selectedFiles.length - failureCount;
|
|
|
|
}
|
|
|
|
|
2022-10-15 16:29:18 +02:00
|
|
|
final DiContainer _c;
|
2021-12-02 11:47:37 +01:00
|
|
|
}
|