Refactoring: extract remove item from album

This commit is contained in:
Ming Ming 2021-09-25 13:58:30 +08:00
parent a15f7a0db6
commit 3d5de229cb
3 changed files with 47 additions and 27 deletions

View file

@ -0,0 +1,36 @@
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/entity/album.dart';
import 'package:nc_photos/entity/album/item.dart';
import 'package:nc_photos/entity/album/provider.dart';
import 'package:nc_photos/iterable_extension.dart';
import 'package:nc_photos/use_case/update_album.dart';
class RemoveFromAlbum {
RemoveFromAlbum(this.albumRepo);
/// Remove a list of AlbumItems from [album]
///
/// The items are compared with [identical], so it must come from [album] for
/// it to work
Future<Album> call(
Account account, Album album, List<AlbumItem> items) async {
_log.info("[call] Remove ${items.length} items from album '${album.name}'");
assert(album.provider is AlbumStaticProvider);
final provider = album.provider as AlbumStaticProvider;
final newItems = provider.items
.where((element) => !items.containsIdentical(element))
.toList();
var newAlbum = album.copyWith(
provider: AlbumStaticProvider.of(album).copyWith(
items: newItems,
),
);
await UpdateAlbum(albumRepo)(account, newAlbum);
return newAlbum;
}
final AlbumRepo albumRepo;
static final _log = Logger("use_case.remove_from_album.RemoveFromAlbum");
}

View file

@ -22,6 +22,7 @@ import 'package:nc_photos/session_storage.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/remove_from_album.dart';
import 'package:nc_photos/use_case/resync_album.dart'; import 'package:nc_photos/use_case/resync_album.dart';
import 'package:nc_photos/use_case/update_album.dart'; import 'package:nc_photos/use_case/update_album.dart';
import 'package:nc_photos/widget/album_browser_mixin.dart'; import 'package:nc_photos/widget/album_browser_mixin.dart';
@ -314,23 +315,14 @@ class _AlbumBrowserState extends State<AlbumBrowser>
Future<void> _onSelectionAppBarRemovePressed() async { Future<void> _onSelectionAppBarRemovePressed() async {
final selectedIndexes = final selectedIndexes =
selectedListItems.map((e) => (e as _ListItem).index).toList(); selectedListItems.map((e) => (e as _ListItem).index).toList();
final newItems = _sortedItems final selectedItems = _sortedItems.takeIndex(selectedIndexes).toList();
.withIndex()
.where((element) => !selectedIndexes.contains(element.item1))
.map((e) => e.item2)
.toList();
setState(() { setState(() {
clearSelectedItems(); clearSelectedItems();
}); });
final albumRepo = AlbumRepo(AlbumCachedDataSource());
final newAlbum = _album!.copyWith(
provider: AlbumStaticProvider.of(_album!).copyWith(
items: newItems,
),
);
try { try {
await UpdateAlbum(albumRepo)(widget.account, newAlbum); final albumRepo = AlbumRepo(AlbumCachedDataSource());
await RemoveFromAlbum(albumRepo)(widget.account, _album!, selectedItems);
SnackBarManager().showSnackBar(SnackBar( SnackBarManager().showSnackBar(SnackBar(
content: Text(L10n.global().removeSelectedFromAlbumSuccessNotification( content: Text(L10n.global().removeSelectedFromAlbumSuccessNotification(
selectedIndexes.length)), selectedIndexes.length)),

View file

@ -28,6 +28,7 @@ import 'package:nc_photos/platform/k.dart' as platform_k;
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/add_to_album.dart'; import 'package:nc_photos/use_case/add_to_album.dart';
import 'package:nc_photos/use_case/remove_from_album.dart';
import 'package:nc_photos/use_case/update_album.dart'; import 'package:nc_photos/use_case/update_album.dart';
import 'package:nc_photos/use_case/update_property.dart'; import 'package:nc_photos/use_case/update_property.dart';
import 'package:nc_photos/widget/album_picker_dialog.dart'; import 'package:nc_photos/widget/album_picker_dialog.dart';
@ -297,21 +298,12 @@ class _ViewerDetailPaneState extends State<ViewerDetailPane> {
await NotifiedAction( await NotifiedAction(
() async { () async {
final albumRepo = AlbumRepo(AlbumCachedDataSource()); final albumRepo = AlbumRepo(AlbumCachedDataSource());
final newItems = final thisItem = AlbumStaticProvider.of(widget.album!)
AlbumStaticProvider.of(widget.album!).items.where((element) { .items
if (element is AlbumFileItem) { .whereType<AlbumFileItem>()
return element.file.path != widget.file.path; .firstWhere((element) => element.file.path == widget.file.path);
} else { await RemoveFromAlbum(albumRepo)(
return true; widget.account, widget.album!, [thisItem]);
}
}).toList();
await UpdateAlbum(albumRepo)(
widget.account,
widget.album!.copyWith(
provider: AlbumStaticProvider.of(widget.album!).copyWith(
items: newItems,
),
));
if (mounted) { if (mounted) {
Navigator.of(context).pop(); Navigator.of(context).pop();
} }