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';
|
2021-07-05 09:54:01 +02:00
|
|
|
import 'package:nc_photos/entity/album/item.dart';
|
2021-06-24 18:26:56 +02:00
|
|
|
import 'package:nc_photos/entity/album/provider.dart';
|
2021-04-10 06:28:12 +02:00
|
|
|
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 {
|
2021-07-09 18:33:07 +02:00
|
|
|
final albums = (await ListAlbum(fileRepo, albumRepo)(account)
|
|
|
|
.where((event) => event is Album)
|
|
|
|
.toList()).cast<Album>();
|
2021-06-24 18:26:56 +02:00
|
|
|
// clean up only make sense for static albums
|
|
|
|
for (final a
|
|
|
|
in albums.where((element) => element.provider is AlbumStaticProvider)) {
|
2021-04-10 06:28:12 +02:00
|
|
|
try {
|
2021-06-24 18:26:56 +02:00
|
|
|
final provider = AlbumStaticProvider.of(a);
|
|
|
|
if (provider.items.any((element) =>
|
2021-04-10 06:28:12 +02:00
|
|
|
element is AlbumFileItem && element.file.path == file.path)) {
|
2021-06-24 18:26:56 +02:00
|
|
|
final newItems = provider.items.where((element) {
|
2021-04-10 06:28:12 +02:00
|
|
|
if (element is AlbumFileItem) {
|
|
|
|
return element.file.path != file.path;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}).toList();
|
2021-06-24 18:26:56 +02:00
|
|
|
await UpdateAlbum(albumRepo)(
|
|
|
|
account,
|
|
|
|
a.copyWith(
|
|
|
|
provider: AlbumStaticProvider(
|
|
|
|
items: newItems,
|
|
|
|
),
|
|
|
|
));
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
} 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");
|
|
|
|
}
|