2021-04-10 06:28:12 +02:00
|
|
|
import 'package:event_bus/event_bus.dart';
|
|
|
|
import 'package:kiwi/kiwi.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:nc_photos/account.dart';
|
|
|
|
import 'package:nc_photos/entity/album.dart';
|
|
|
|
import 'package:nc_photos/entity/file.dart';
|
|
|
|
import 'package:nc_photos/event/event.dart';
|
|
|
|
import 'package:nc_photos/use_case/list_album.dart';
|
|
|
|
import 'package:nc_photos/use_case/update_album.dart';
|
|
|
|
|
|
|
|
class Remove {
|
|
|
|
Remove(this.fileRepo, this.albumRepo);
|
|
|
|
|
|
|
|
/// Remove a file
|
|
|
|
Future<void> call(Account account, File file) async {
|
|
|
|
await fileRepo.remove(account, file);
|
2021-05-23 22:02:03 +02:00
|
|
|
if (albumRepo != null) {
|
|
|
|
_log.info("[call] Skip albums cleanup as albumRepo == null");
|
|
|
|
await _cleanUpAlbums(account, file);
|
|
|
|
}
|
2021-04-10 06:28:12 +02:00
|
|
|
KiwiContainer().resolve<EventBus>().fire(FileRemovedEvent(account, file));
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _cleanUpAlbums(Account account, File file) async {
|
|
|
|
final albums = await ListAlbum(fileRepo, albumRepo)(account);
|
|
|
|
for (final a in albums) {
|
|
|
|
try {
|
|
|
|
if (a.items.any((element) =>
|
|
|
|
element is AlbumFileItem && element.file.path == file.path)) {
|
|
|
|
final newItems = a.items.where((element) {
|
|
|
|
if (element is AlbumFileItem) {
|
|
|
|
return element.file.path != file.path;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}).toList();
|
|
|
|
await UpdateAlbum(albumRepo)(account, a.copyWith(items: newItems));
|
|
|
|
}
|
|
|
|
} catch (e, stacktrace) {
|
2021-04-27 22:06:16 +02:00
|
|
|
_log.shout(
|
2021-04-10 06:28:12 +02:00
|
|
|
"[_cleanUpAlbums] Failed while updating album", e, stacktrace);
|
|
|
|
// continue to next album
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final FileRepo fileRepo;
|
|
|
|
final AlbumRepo albumRepo;
|
|
|
|
|
|
|
|
static final _log = Logger("use_case.remove.Remove");
|
|
|
|
}
|