mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-02-02 14:56:20 +01:00
Refactor: extract archive selection fn
This commit is contained in:
parent
95f757647e
commit
b417ccb6a0
5 changed files with 69 additions and 67 deletions
|
@ -65,7 +65,8 @@ class NotifiedListAction<T> {
|
||||||
this.onActionError,
|
this.onActionError,
|
||||||
});
|
});
|
||||||
|
|
||||||
Future<void> call() async {
|
/// Perform the action and return the success count
|
||||||
|
Future<int> call() async {
|
||||||
ScaffoldFeatureController<SnackBar, SnackBarClosedReason>? controller;
|
ScaffoldFeatureController<SnackBar, SnackBarClosedReason>? controller;
|
||||||
if (processingText != null) {
|
if (processingText != null) {
|
||||||
controller = SnackBarManager().showSnackBar(SnackBar(
|
controller = SnackBarManager().showSnackBar(SnackBar(
|
||||||
|
@ -101,6 +102,7 @@ class NotifiedListAction<T> {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return list.length - failedItems.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
final List<T> list;
|
final List<T> list;
|
||||||
|
|
43
lib/widget/handler/archive_selection_handler.dart
Normal file
43
lib/widget/handler/archive_selection_handler.dart
Normal 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");
|
||||||
|
}
|
|
@ -8,29 +8,26 @@ import 'package:kiwi/kiwi.dart';
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:nc_photos/account.dart';
|
import 'package:nc_photos/account.dart';
|
||||||
import 'package:nc_photos/api/api_util.dart' as api_util;
|
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/app_localizations.dart';
|
||||||
import 'package:nc_photos/bloc/scan_account_dir.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/download_handler.dart';
|
||||||
import 'package:nc_photos/entity/album.dart';
|
import 'package:nc_photos/entity/album.dart';
|
||||||
import 'package:nc_photos/entity/file.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/file_util.dart' as file_util;
|
||||||
import 'package:nc_photos/event/event.dart';
|
import 'package:nc_photos/event/event.dart';
|
||||||
import 'package:nc_photos/exception_util.dart' as exception_util;
|
import 'package:nc_photos/exception_util.dart' as exception_util;
|
||||||
import 'package:nc_photos/iterable_extension.dart';
|
import 'package:nc_photos/iterable_extension.dart';
|
||||||
import 'package:nc_photos/k.dart' as k;
|
import 'package:nc_photos/k.dart' as k;
|
||||||
import 'package:nc_photos/metadata_task_manager.dart';
|
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/pref.dart';
|
||||||
import 'package:nc_photos/primitive.dart';
|
import 'package:nc_photos/primitive.dart';
|
||||||
import 'package:nc_photos/share_handler.dart';
|
import 'package:nc_photos/share_handler.dart';
|
||||||
import 'package:nc_photos/snack_bar_manager.dart';
|
import 'package:nc_photos/snack_bar_manager.dart';
|
||||||
import 'package:nc_photos/theme.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/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/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/handler/remove_selection_handler.dart';
|
||||||
import 'package:nc_photos/widget/home_app_bar.dart';
|
import 'package:nc_photos/widget/home_app_bar.dart';
|
||||||
import 'package:nc_photos/widget/measure.dart';
|
import 'package:nc_photos/widget/measure.dart';
|
||||||
|
@ -466,25 +463,10 @@ class _HomePhotosState extends State<HomePhotos>
|
||||||
setState(() {
|
setState(() {
|
||||||
clearSelectedItems();
|
clearSelectedItems();
|
||||||
});
|
});
|
||||||
final fileRepo = FileRepo(FileCachedDataSource(AppDb()));
|
await ArchiveSelectionHandler(KiwiContainer().resolve<DiContainer>())(
|
||||||
await NotifiedListAction<File>(
|
account: widget.account,
|
||||||
list: selectedFiles,
|
selectedFiles: 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);
|
|
||||||
},
|
|
||||||
)();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onSelectionDeletePressed(BuildContext context) async {
|
Future<void> _onSelectionDeletePressed(BuildContext context) async {
|
||||||
|
|
|
@ -3,6 +3,7 @@ import 'package:cached_network_image_platform_interface/cached_network_image_pla
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
|
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
|
||||||
|
import 'package:kiwi/kiwi.dart';
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:nc_photos/account.dart';
|
import 'package:nc_photos/account.dart';
|
||||||
import 'package:nc_photos/api/api.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/app_localizations.dart';
|
||||||
import 'package:nc_photos/bloc/list_face.dart';
|
import 'package:nc_photos/bloc/list_face.dart';
|
||||||
import 'package:nc_photos/cache_manager_util.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/download_handler.dart';
|
||||||
import 'package:nc_photos/entity/face.dart';
|
import 'package:nc_photos/entity/face.dart';
|
||||||
import 'package:nc_photos/entity/file.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/file_util.dart' as file_util;
|
||||||
import 'package:nc_photos/entity/person.dart';
|
import 'package:nc_photos/entity/person.dart';
|
||||||
import 'package:nc_photos/event/event.dart';
|
import 'package:nc_photos/event/event.dart';
|
||||||
import 'package:nc_photos/exception_util.dart' as exception_util;
|
import 'package:nc_photos/exception_util.dart' as exception_util;
|
||||||
import 'package:nc_photos/iterable_extension.dart';
|
import 'package:nc_photos/iterable_extension.dart';
|
||||||
import 'package:nc_photos/k.dart' as k;
|
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/pref.dart';
|
||||||
import 'package:nc_photos/share_handler.dart';
|
import 'package:nc_photos/share_handler.dart';
|
||||||
import 'package:nc_photos/snack_bar_manager.dart';
|
import 'package:nc_photos/snack_bar_manager.dart';
|
||||||
import 'package:nc_photos/theme.dart';
|
import 'package:nc_photos/theme.dart';
|
||||||
import 'package:nc_photos/throttler.dart';
|
import 'package:nc_photos/throttler.dart';
|
||||||
import 'package:nc_photos/use_case/populate_person.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/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/handler/remove_selection_handler.dart';
|
||||||
import 'package:nc_photos/widget/photo_list_item.dart';
|
import 'package:nc_photos/widget/photo_list_item.dart';
|
||||||
import 'package:nc_photos/widget/photo_list_util.dart' as photo_list_util;
|
import 'package:nc_photos/widget/photo_list_util.dart' as photo_list_util;
|
||||||
|
@ -378,25 +377,10 @@ class _PersonBrowserState extends State<PersonBrowser>
|
||||||
setState(() {
|
setState(() {
|
||||||
clearSelectedItems();
|
clearSelectedItems();
|
||||||
});
|
});
|
||||||
final fileRepo = FileRepo(FileCachedDataSource(AppDb()));
|
await ArchiveSelectionHandler(KiwiContainer().resolve<DiContainer>())(
|
||||||
await NotifiedListAction<File>(
|
account: widget.account,
|
||||||
list: selectedFiles,
|
selectedFiles: 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);
|
|
||||||
},
|
|
||||||
)();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onSelectionDeletePressed(BuildContext context) async {
|
Future<void> _onSelectionDeletePressed(BuildContext context) async {
|
||||||
|
|
|
@ -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/animated_visibility.dart';
|
||||||
import 'package:nc_photos/widget/gps_map.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/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:nc_photos/widget/photo_date_time_edit_dialog.dart';
|
||||||
import 'package:path/path.dart';
|
import 'package:path/path.dart';
|
||||||
import 'package:tuple/tuple.dart';
|
import 'package:tuple/tuple.dart';
|
||||||
|
@ -363,25 +364,15 @@ class _ViewerDetailPaneState extends State<ViewerDetailPane> {
|
||||||
|
|
||||||
Future<void> _onArchivePressed(BuildContext context) async {
|
Future<void> _onArchivePressed(BuildContext context) async {
|
||||||
_log.info("[_onArchivePressed] Archive file: ${widget.file.path}");
|
_log.info("[_onArchivePressed] Archive file: ${widget.file.path}");
|
||||||
try {
|
final count =
|
||||||
await NotifiedAction(
|
await ArchiveSelectionHandler(KiwiContainer().resolve<DiContainer>())(
|
||||||
() async {
|
account: widget.account,
|
||||||
final fileRepo = FileRepo(FileCachedDataSource(AppDb()));
|
selectedFiles: [widget.file],
|
||||||
await UpdateProperty(fileRepo)
|
);
|
||||||
.updateIsArchived(widget.account, widget.file, true);
|
if (count == 1) {
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
Navigator.of(context).pop();
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue