nc-photos/lib/use_case/update_property.dart

109 lines
3.2 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';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/event/event.dart';
2021-04-28 21:22:33 +02:00
import 'package:nc_photos/or_null.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';
2021-05-28 19:15:09 +02:00
class UpdateProperty {
UpdateProperty(this.fileRepo, this.albumRepo);
2021-04-10 06:28:12 +02:00
2021-05-28 19:15:09 +02:00
Future<void> call(
Account account,
File file, {
OrNull<Metadata> metadata,
2021-05-28 20:45:00 +02:00
OrNull<bool> isArchived,
2021-05-28 19:15:09 +02:00
}) async {
2021-05-28 20:45:00 +02:00
if (metadata == null && isArchived == null) {
2021-05-28 19:15:09 +02:00
// ?
_log.warning("[call] Nothing to update");
return;
}
if (metadata?.obj != null && metadata.obj.fileEtag != file.etag) {
2021-04-10 06:28:12 +02:00
_log.warning(
2021-05-28 19:15:09 +02:00
"[call] Metadata fileEtag mismatch with actual file's (metadata: ${metadata.obj.fileEtag}, file: ${file.etag})");
}
await fileRepo.updateProperty(
account,
file,
metadata: metadata,
2021-05-28 20:45:00 +02:00
isArchived: isArchived,
2021-05-28 19:15:09 +02:00
);
await _cleanUpAlbums(
account,
file,
metadata: metadata,
2021-05-28 20:45:00 +02:00
isArchived: isArchived,
2021-05-28 19:15:09 +02:00
);
int properties = 0;
if (metadata != null) {
properties |= FilePropertyUpdatedEvent.propMetadata;
2021-04-10 06:28:12 +02:00
}
2021-05-28 20:45:00 +02:00
if (isArchived != null) {
properties |= FilePropertyUpdatedEvent.propIsArchived;
}
2021-05-28 19:15:09 +02:00
assert(properties != 0);
2021-04-10 06:28:12 +02:00
KiwiContainer()
.resolve<EventBus>()
2021-05-28 19:15:09 +02:00
.fire(FilePropertyUpdatedEvent(account, file, properties));
2021-04-10 06:28:12 +02:00
}
Future<void> _cleanUpAlbums(
2021-05-28 19:15:09 +02:00
Account account,
File file, {
OrNull<Metadata> metadata,
2021-05-28 20:45:00 +02:00
OrNull<bool> isArchived,
2021-05-28 19:15:09 +02:00
}) async {
2021-04-10 06:28:12 +02:00
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.map((e) {
if (e is AlbumFileItem && e.file.path == file.path) {
return AlbumFileItem(
2021-05-28 19:15:09 +02:00
file: e.file.copyWith(
metadata: metadata,
2021-05-28 20:45:00 +02:00
isArchived: isArchived,
2021-05-28 19:15:09 +02:00
),
2021-04-10 06:28:12 +02:00
);
} else {
return e;
}
}).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;
2021-05-28 19:15:09 +02:00
static final _log = Logger("use_case.update_property.UpdateProperty");
}
extension UpdatePropertyExtension on UpdateProperty {
/// Convenience function to only update metadata
///
/// See [UpdateProperty.call]
Future<void> updateMetadata(Account account, File file, Metadata metadata) =>
call(account, file, metadata: OrNull(metadata));
2021-05-28 20:45:00 +02:00
/// Convenience function to only update isArchived
///
/// See [UpdateProperty.call]
Future<void> updateIsArchived(Account account, File file, bool isArchived) =>
call(account, file, isArchived: OrNull(isArchived));
2021-04-10 06:28:12 +02:00
}