Refactor: extract archive selection fn

This commit is contained in:
Ming Ming 2022-01-22 20:02:24 +08:00
parent 95f757647e
commit b417ccb6a0
5 changed files with 69 additions and 67 deletions

View file

@ -65,7 +65,8 @@ class NotifiedListAction<T> {
this.onActionError,
});
Future<void> call() async {
/// Perform the action and return the success count
Future<int> call() async {
ScaffoldFeatureController<SnackBar, SnackBarClosedReason>? controller;
if (processingText != null) {
controller = SnackBarManager().showSnackBar(SnackBar(
@ -101,6 +102,7 @@ class NotifiedListAction<T> {
));
}
}
return list.length - failedItems.length;
}
final List<T> list;

View file

@ -0,0 +1,43 @@
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/entity/file.dart';
import 'package:nc_photos/notified_action.dart';
import 'package:nc_photos/use_case/update_property.dart';
class ArchiveSelectionHandler {
ArchiveSelectionHandler(this._c) : assert(require(_c));
static bool require(DiContainer c) => DiContainer.has(c, DiType.fileRepo);
/// Archive [selectedFiles] and return the archived count
Future<int> call({
required Account account,
required List<File> selectedFiles,
}) {
return NotifiedListAction<File>(
list: selectedFiles,
action: (file) async {
await UpdateProperty(_c.fileRepo).updateIsArchived(account, file, true);
},
processingText: L10n.global()
.archiveSelectedProcessingNotification(selectedFiles.length),
successText: L10n.global().archiveSelectedSuccessNotification,
getFailureText: (failures) =>
L10n.global().archiveSelectedFailureNotification(failures.length),
onActionError: (file, e, stackTrace) {
_log.shout(
"[call] Failed while archiving file: ${logFilename(file.path)}",
e,
stackTrace);
},
)();
}
final DiContainer _c;
static final _log = Logger(
"widget.handler.archive_selection_handler.ArchiveSelectionHandler");
}

View file

@ -8,29 +8,26 @@ import 'package:kiwi/kiwi.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/api/api_util.dart' as api_util;
import 'package:nc_photos/app_db.dart';
import 'package:nc_photos/app_localizations.dart';
import 'package:nc_photos/bloc/scan_account_dir.dart';
import 'package:nc_photos/debug_util.dart';
import 'package:nc_photos/di_container.dart';
import 'package:nc_photos/download_handler.dart';
import 'package:nc_photos/entity/album.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/file/data_source.dart';
import 'package:nc_photos/entity/file_util.dart' as file_util;
import 'package:nc_photos/event/event.dart';
import 'package:nc_photos/exception_util.dart' as exception_util;
import 'package:nc_photos/iterable_extension.dart';
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/metadata_task_manager.dart';
import 'package:nc_photos/notified_action.dart';
import 'package:nc_photos/pref.dart';
import 'package:nc_photos/primitive.dart';
import 'package:nc_photos/share_handler.dart';
import 'package:nc_photos/snack_bar_manager.dart';
import 'package:nc_photos/theme.dart';
import 'package:nc_photos/use_case/update_property.dart';
import 'package:nc_photos/widget/album_browser_util.dart' as album_browser_util;
import 'package:nc_photos/widget/handler/add_selection_to_album_handler.dart';
import 'package:nc_photos/widget/handler/archive_selection_handler.dart';
import 'package:nc_photos/widget/handler/remove_selection_handler.dart';
import 'package:nc_photos/widget/home_app_bar.dart';
import 'package:nc_photos/widget/measure.dart';
@ -466,25 +463,10 @@ class _HomePhotosState extends State<HomePhotos>
setState(() {
clearSelectedItems();
});
final fileRepo = FileRepo(FileCachedDataSource(AppDb()));
await NotifiedListAction<File>(
list: selectedFiles,
action: (file) async {
await UpdateProperty(fileRepo)
.updateIsArchived(widget.account, file, true);
},
processingText: L10n.global()
.archiveSelectedProcessingNotification(selectedFiles.length),
successText: L10n.global().archiveSelectedSuccessNotification,
getFailureText: (failures) =>
L10n.global().archiveSelectedFailureNotification(failures.length),
onActionError: (file, e, stackTrace) {
_log.shout(
"[_onSelectionArchivePressed] Failed while archiving file: ${logFilename(file.path)}",
e,
stackTrace);
},
)();
await ArchiveSelectionHandler(KiwiContainer().resolve<DiContainer>())(
account: widget.account,
selectedFiles: selectedFiles,
);
}
Future<void> _onSelectionDeletePressed(BuildContext context) async {

View file

@ -3,6 +3,7 @@ import 'package:cached_network_image_platform_interface/cached_network_image_pla
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:kiwi/kiwi.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/api/api.dart';
@ -11,26 +12,24 @@ import 'package:nc_photos/app_db.dart';
import 'package:nc_photos/app_localizations.dart';
import 'package:nc_photos/bloc/list_face.dart';
import 'package:nc_photos/cache_manager_util.dart';
import 'package:nc_photos/debug_util.dart';
import 'package:nc_photos/di_container.dart';
import 'package:nc_photos/download_handler.dart';
import 'package:nc_photos/entity/face.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/file/data_source.dart';
import 'package:nc_photos/entity/file_util.dart' as file_util;
import 'package:nc_photos/entity/person.dart';
import 'package:nc_photos/event/event.dart';
import 'package:nc_photos/exception_util.dart' as exception_util;
import 'package:nc_photos/iterable_extension.dart';
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/notified_action.dart';
import 'package:nc_photos/pref.dart';
import 'package:nc_photos/share_handler.dart';
import 'package:nc_photos/snack_bar_manager.dart';
import 'package:nc_photos/theme.dart';
import 'package:nc_photos/throttler.dart';
import 'package:nc_photos/use_case/populate_person.dart';
import 'package:nc_photos/use_case/update_property.dart';
import 'package:nc_photos/widget/handler/add_selection_to_album_handler.dart';
import 'package:nc_photos/widget/handler/archive_selection_handler.dart';
import 'package:nc_photos/widget/handler/remove_selection_handler.dart';
import 'package:nc_photos/widget/photo_list_item.dart';
import 'package:nc_photos/widget/photo_list_util.dart' as photo_list_util;
@ -378,25 +377,10 @@ class _PersonBrowserState extends State<PersonBrowser>
setState(() {
clearSelectedItems();
});
final fileRepo = FileRepo(FileCachedDataSource(AppDb()));
await NotifiedListAction<File>(
list: selectedFiles,
action: (file) async {
await UpdateProperty(fileRepo)
.updateIsArchived(widget.account, file, true);
},
processingText: L10n.global()
.archiveSelectedProcessingNotification(selectedFiles.length),
successText: L10n.global().archiveSelectedSuccessNotification,
getFailureText: (failures) =>
L10n.global().archiveSelectedFailureNotification(failures.length),
onActionError: (file, e, stackTrace) {
_log.shout(
"[_onSelectionArchivePressed] Failed while archiving file: ${logFilename(file.path)}",
e,
stackTrace);
},
)();
await ArchiveSelectionHandler(KiwiContainer().resolve<DiContainer>())(
account: widget.account,
selectedFiles: selectedFiles,
);
}
Future<void> _onSelectionDeletePressed(BuildContext context) async {

View file

@ -30,6 +30,7 @@ import 'package:nc_photos/use_case/update_property.dart';
import 'package:nc_photos/widget/animated_visibility.dart';
import 'package:nc_photos/widget/gps_map.dart';
import 'package:nc_photos/widget/handler/add_selection_to_album_handler.dart';
import 'package:nc_photos/widget/handler/archive_selection_handler.dart';
import 'package:nc_photos/widget/photo_date_time_edit_dialog.dart';
import 'package:path/path.dart';
import 'package:tuple/tuple.dart';
@ -363,25 +364,15 @@ class _ViewerDetailPaneState extends State<ViewerDetailPane> {
Future<void> _onArchivePressed(BuildContext context) async {
_log.info("[_onArchivePressed] Archive file: ${widget.file.path}");
try {
await NotifiedAction(
() async {
final fileRepo = FileRepo(FileCachedDataSource(AppDb()));
await UpdateProperty(fileRepo)
.updateIsArchived(widget.account, widget.file, true);
if (mounted) {
Navigator.of(context).pop();
}
},
L10n.global().archiveSelectedProcessingNotification(1),
L10n.global().archiveSelectedSuccessNotification,
failureText: L10n.global().archiveSelectedFailureNotification(1),
)();
} catch (e, stackTrace) {
_log.shout(
"[_onArchivePressed] Failed while archiving file: ${logFilename(widget.file.path)}",
e,
stackTrace);
final count =
await ArchiveSelectionHandler(KiwiContainer().resolve<DiContainer>())(
account: widget.account,
selectedFiles: [widget.file],
);
if (count == 1) {
if (mounted) {
Navigator.of(context).pop();
}
}
}