mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-02-02 06:46:22 +01:00
Use controller to remove files in RemoveSelectionHandler
This commit is contained in:
parent
482158c6bf
commit
5278cda402
6 changed files with 31 additions and 48 deletions
|
@ -1,16 +1,12 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:kiwi/kiwi.dart';
|
||||
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';
|
||||
import 'package:nc_photos/di_container.dart';
|
||||
import 'package:nc_photos/controller/files_controller.dart';
|
||||
import 'package:nc_photos/entity/file_descriptor.dart';
|
||||
import 'package:nc_photos/k.dart' as k;
|
||||
import 'package:nc_photos/navigation_manager.dart';
|
||||
import 'package:nc_photos/snack_bar_manager.dart';
|
||||
import 'package:nc_photos/use_case/inflate_file_descriptor.dart';
|
||||
import 'package:nc_photos/use_case/remove.dart';
|
||||
import 'package:nc_photos/widget/trashbin_browser.dart';
|
||||
import 'package:np_codegen/np_codegen.dart';
|
||||
|
||||
|
@ -18,11 +14,9 @@ part 'remove_selection_handler.g.dart';
|
|||
|
||||
@npLog
|
||||
class RemoveSelectionHandler {
|
||||
RemoveSelectionHandler(this._c)
|
||||
: assert(require(_c)),
|
||||
assert(InflateFileDescriptor.require(_c));
|
||||
|
||||
static bool require(DiContainer c) => true;
|
||||
const RemoveSelectionHandler({
|
||||
required this.filesController,
|
||||
});
|
||||
|
||||
/// Remove [selectedFiles] and return the removed count
|
||||
Future<int> call({
|
||||
|
@ -31,44 +25,25 @@ class RemoveSelectionHandler {
|
|||
bool shouldCleanupAlbum = true,
|
||||
bool isRemoveOpened = false,
|
||||
bool isMoveToTrash = false,
|
||||
bool shouldShowProcessingText = true,
|
||||
}) async {
|
||||
final selectedFiles = await InflateFileDescriptor(_c)(account, selection);
|
||||
final String processingText, successText;
|
||||
final String 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);
|
||||
}
|
||||
if (shouldShowProcessingText) {
|
||||
SnackBarManager().showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(processingText),
|
||||
duration: k.snackBarDurationShort,
|
||||
),
|
||||
canBeReplaced: true,
|
||||
);
|
||||
}
|
||||
|
||||
var failureCount = 0;
|
||||
await Remove(KiwiContainer().resolve<DiContainer>())(
|
||||
account,
|
||||
selectedFiles,
|
||||
onError: (_, file, e, stackTrace) {
|
||||
_log.shout(
|
||||
"[call] Failed while removing file: ${logFilename(file.fdPath)}",
|
||||
e,
|
||||
stackTrace);
|
||||
++failureCount;
|
||||
await filesController.remove(
|
||||
selection,
|
||||
errorBuilder: (fileIds) {
|
||||
failureCount = fileIds.length;
|
||||
return RemoveFailureError(fileIds);
|
||||
},
|
||||
shouldCleanUp: shouldCleanupAlbum,
|
||||
);
|
||||
final trashAction = isMoveToTrash
|
||||
? SnackBarAction(
|
||||
|
@ -93,8 +68,8 @@ class RemoveSelectionHandler {
|
|||
action: trashAction,
|
||||
));
|
||||
}
|
||||
return selectedFiles.length - failureCount;
|
||||
return selection.length - failureCount;
|
||||
}
|
||||
|
||||
final DiContainer _c;
|
||||
final FilesController filesController;
|
||||
}
|
||||
|
|
|
@ -472,7 +472,6 @@ class _HomePhotosState extends State<HomePhotos>
|
|||
}
|
||||
|
||||
Future<void> _onSelectionDeletePressed(BuildContext context) async {
|
||||
final c = KiwiContainer().resolve<DiContainer>();
|
||||
final selectedFiles = selectedListItems
|
||||
.whereType<PhotoListFileItem>()
|
||||
.map((e) => e.file)
|
||||
|
@ -480,7 +479,9 @@ class _HomePhotosState extends State<HomePhotos>
|
|||
setState(() {
|
||||
clearSelectedItems();
|
||||
});
|
||||
await RemoveSelectionHandler(c)(
|
||||
await RemoveSelectionHandler(
|
||||
filesController: context.read<AccountController>().filesController,
|
||||
)(
|
||||
account: widget.account,
|
||||
selection: selectedFiles,
|
||||
isMoveToTrash: true,
|
||||
|
|
|
@ -8,6 +8,7 @@ import 'package:logging/logging.dart';
|
|||
import 'package:nc_photos/account.dart';
|
||||
import 'package:nc_photos/app_localizations.dart';
|
||||
import 'package:nc_photos/bloc/search.dart';
|
||||
import 'package:nc_photos/controller/account_controller.dart';
|
||||
import 'package:nc_photos/di_container.dart';
|
||||
import 'package:nc_photos/download_handler.dart';
|
||||
import 'package:nc_photos/entity/file_descriptor.dart';
|
||||
|
@ -492,7 +493,6 @@ class _HomeSearchState extends State<HomeSearch>
|
|||
}
|
||||
|
||||
Future<void> _onSelectionDeletePressed(BuildContext context) async {
|
||||
final c = KiwiContainer().resolve<DiContainer>();
|
||||
final selectedFiles = selectedListItems
|
||||
.whereType<PhotoListFileItem>()
|
||||
.map((e) => e.file)
|
||||
|
@ -500,7 +500,9 @@ class _HomeSearchState extends State<HomeSearch>
|
|||
setState(() {
|
||||
clearSelectedItems();
|
||||
});
|
||||
await RemoveSelectionHandler(c)(
|
||||
await RemoveSelectionHandler(
|
||||
filesController: context.read<AccountController>().filesController,
|
||||
)(
|
||||
account: widget.account,
|
||||
selection: selectedFiles,
|
||||
isMoveToTrash: true,
|
||||
|
|
|
@ -8,6 +8,7 @@ import 'package:logging/logging.dart';
|
|||
import 'package:nc_photos/account.dart';
|
||||
import 'package:nc_photos/app_localizations.dart';
|
||||
import 'package:nc_photos/bloc/ls_trashbin.dart';
|
||||
import 'package:nc_photos/controller/account_controller.dart';
|
||||
import 'package:nc_photos/debug_util.dart';
|
||||
import 'package:nc_photos/di_container.dart';
|
||||
import 'package:nc_photos/entity/file.dart';
|
||||
|
@ -384,8 +385,9 @@ class _TrashbinBrowserState extends State<TrashbinBrowser>
|
|||
}
|
||||
|
||||
Future<void> _deleteFiles(List<FileDescriptor> files) async {
|
||||
final c = KiwiContainer().resolve<DiContainer>();
|
||||
await RemoveSelectionHandler(c)(
|
||||
await RemoveSelectionHandler(
|
||||
filesController: context.read<AccountController>().filesController,
|
||||
)(
|
||||
account: widget.account,
|
||||
selection: files,
|
||||
shouldCleanupAlbum: false,
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:kiwi/kiwi.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:nc_photos/account.dart';
|
||||
import 'package:nc_photos/app_localizations.dart';
|
||||
import 'package:nc_photos/controller/account_controller.dart';
|
||||
import 'package:nc_photos/debug_util.dart';
|
||||
import 'package:nc_photos/di_container.dart';
|
||||
import 'package:nc_photos/entity/file.dart';
|
||||
|
@ -315,10 +317,11 @@ class _TrashbinViewerState extends State<TrashbinViewer> {
|
|||
}
|
||||
|
||||
Future<void> _delete(BuildContext context) async {
|
||||
final c = KiwiContainer().resolve<DiContainer>();
|
||||
final file = widget.streamFiles[_viewerController.currentPage];
|
||||
_log.info("[_delete] Removing file: ${file.path}");
|
||||
final count = await RemoveSelectionHandler(c)(
|
||||
final count = await RemoveSelectionHandler(
|
||||
filesController: context.read<AccountController>().filesController,
|
||||
)(
|
||||
account: widget.account,
|
||||
selection: [file],
|
||||
shouldCleanupAlbum: false,
|
||||
|
|
|
@ -646,15 +646,15 @@ class _ViewerState extends State<Viewer>
|
|||
|
||||
void _onDeletePressed(BuildContext context) {
|
||||
final index = _viewerController.currentPage;
|
||||
final c = KiwiContainer().resolve<DiContainer>();
|
||||
final file = _streamFilesView[index];
|
||||
_log.info("[_onDeletePressed] Removing file: ${file.fdPath}");
|
||||
unawaited(RemoveSelectionHandler(c)(
|
||||
unawaited(RemoveSelectionHandler(
|
||||
filesController: context.read<AccountController>().filesController,
|
||||
)(
|
||||
account: widget.account,
|
||||
selection: [file],
|
||||
isRemoveOpened: true,
|
||||
isMoveToTrash: true,
|
||||
shouldShowProcessingText: false,
|
||||
));
|
||||
_removeCurrentItemFromStream(context, index);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue