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/snack_bar_manager.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/update_album.dart';
import 'package:nc_photos/widget/album_browser_mixin.dart';
@ -314,23 +315,14 @@ class _AlbumBrowserState extends State<AlbumBrowser>
Future<void> _onSelectionAppBarRemovePressed() async {
final selectedIndexes =
selectedListItems.map((e) => (e as _ListItem).index).toList();
final newItems = _sortedItems
.withIndex()
.where((element) => !selectedIndexes.contains(element.item1))
.map((e) => e.item2)
.toList();
final selectedItems = _sortedItems.takeIndex(selectedIndexes).toList();
setState(() {
clearSelectedItems();
});
final albumRepo = AlbumRepo(AlbumCachedDataSource());
final newAlbum = _album!.copyWith(
provider: AlbumStaticProvider.of(_album!).copyWith(
items: newItems,
),
);
try {
await UpdateAlbum(albumRepo)(widget.account, newAlbum);
final albumRepo = AlbumRepo(AlbumCachedDataSource());
await RemoveFromAlbum(albumRepo)(widget.account, _album!, selectedItems);
SnackBarManager().showSnackBar(SnackBar(
content: Text(L10n.global().removeSelectedFromAlbumSuccessNotification(
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/theme.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_property.dart';
import 'package:nc_photos/widget/album_picker_dialog.dart';
@ -297,21 +298,12 @@ class _ViewerDetailPaneState extends State<ViewerDetailPane> {
await NotifiedAction(
() async {
final albumRepo = AlbumRepo(AlbumCachedDataSource());
final newItems =
AlbumStaticProvider.of(widget.album!).items.where((element) {
if (element is AlbumFileItem) {
return element.file.path != widget.file.path;
} else {
return true;
}
}).toList();
await UpdateAlbum(albumRepo)(
widget.account,
widget.album!.copyWith(
provider: AlbumStaticProvider.of(widget.album!).copyWith(
items: newItems,
),
));
final thisItem = AlbumStaticProvider.of(widget.album!)
.items
.whereType<AlbumFileItem>()
.firstWhere((element) => element.file.path == widget.file.path);
await RemoveFromAlbum(albumRepo)(
widget.account, widget.album!, [thisItem]);
if (mounted) {
Navigator.of(context).pop();
}