nc-photos/lib/use_case/remove.dart

118 lines
3.6 KiB
Dart
Raw Normal View History

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/iterable_extension.dart';
import 'package:nc_photos/throttler.dart';
2021-04-10 06:28:12 +02:00
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);
if (albumRepo != null) {
_log.info("[call] Skip albums cleanup as albumRepo == null");
_CleanUpAlbums()(_CleanUpAlbumsData(fileRepo, albumRepo!, account, file));
}
2021-04-10 06:28:12 +02:00
KiwiContainer().resolve<EventBus>().fire(FileRemovedEvent(account, file));
}
final FileRepo fileRepo;
final AlbumRepo? albumRepo;
static final _log = Logger("use_case.remove.Remove");
}
class _CleanUpAlbumsData {
_CleanUpAlbumsData(this.fileRepo, this.albumRepo, this.account, this.file);
final FileRepo fileRepo;
final AlbumRepo albumRepo;
final Account account;
final File file;
}
class _CleanUpAlbums {
factory _CleanUpAlbums() {
2021-09-15 08:58:06 +02:00
_inst ??= _CleanUpAlbums._();
return _inst!;
}
_CleanUpAlbums._() {
_throttler = Throttler<_CleanUpAlbumsData>(
onTriggered: (data) {
_onTriggered(data);
},
logTag: "remove._CleanUpAlbums",
);
}
void call(_CleanUpAlbumsData data) {
_throttler.trigger(
maxResponceTime: const Duration(seconds: 3),
maxPendingCount: 10,
data: data,
);
}
void _onTriggered(List<_CleanUpAlbumsData> data) async {
for (final pair in data.groupBy(key: (e) => e.account)) {
final list = pair.item2;
await _cleanUp(list.first.fileRepo, list.first.albumRepo,
list.first.account, list.map((e) => e.file).toList());
}
}
/// Clean up for a single account
Future<void> _cleanUp(FileRepo fileRepo, AlbumRepo albumRepo, Account account,
List<File> removes) async {
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.whereType<AlbumFileItem>().any((element) =>
removes.containsIf(element.file, (a, b) => a.path == b.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 !removes.containsIf(
element.file, (a, b) => a.path == b.path);
2021-04-10 06:28:12 +02:00
} else {
return true;
}
}).toList();
2021-06-24 18:26:56 +02:00
await UpdateAlbum(albumRepo)(
account,
a.copyWith(
provider: AlbumStaticProvider.of(a).copyWith(
2021-06-24 18:26:56 +02:00
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
}
}
}
late final Throttler<_CleanUpAlbumsData> _throttler;
2021-04-10 06:28:12 +02:00
static final _log = Logger("use_case.remove");
static _CleanUpAlbums? _inst;
2021-04-10 06:28:12 +02:00
}