nc-photos/app/lib/use_case/update_property.dart

94 lines
2.8 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/file.dart';
import 'package:nc_photos/event/event.dart';
2021-04-28 21:22:33 +02:00
import 'package:nc_photos/or_null.dart';
2022-12-16 16:01:04 +01:00
import 'package:np_codegen/np_codegen.dart';
2021-04-10 06:28:12 +02:00
2022-12-16 16:01:04 +01:00
part 'update_property.g.dart';
@npLog
2021-05-28 19:15:09 +02:00
class UpdateProperty {
UpdateProperty(this.fileRepo);
2021-04-10 06:28:12 +02:00
2021-05-28 19:15:09 +02:00
Future<void> call(
Account account,
File file, {
2021-07-23 22:05:57 +02:00
OrNull<Metadata>? metadata,
OrNull<bool>? isArchived,
OrNull<DateTime>? overrideDateTime,
2022-01-25 11:17:19 +01:00
bool? favorite,
OrNull<ImageLocation>? location,
2021-05-28 19:15:09 +02:00
}) async {
2022-01-25 11:17:19 +01:00
if (metadata == null &&
isArchived == null &&
overrideDateTime == null &&
favorite == null &&
location == null) {
2021-05-28 19:15:09 +02:00
// ?
_log.warning("[call] Nothing to update");
return;
}
2021-07-23 22:05:57 +02:00
if (metadata?.obj != null && metadata!.obj!.fileEtag != file.etag) {
2021-04-10 06:28:12 +02:00
_log.warning(
2021-07-23 22:05:57 +02:00
"[call] Metadata fileEtag mismatch with actual file's (metadata: ${metadata.obj!.fileEtag}, file: ${file.etag})");
2021-05-28 19:15:09 +02:00
}
await fileRepo.updateProperty(
account,
file,
metadata: metadata,
2021-05-28 20:45:00 +02:00
isArchived: isArchived,
2021-06-21 12:39:17 +02:00
overrideDateTime: overrideDateTime,
2022-01-25 11:17:19 +01:00
favorite: favorite,
location: location,
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-06-21 12:39:17 +02:00
if (overrideDateTime != null) {
properties |= FilePropertyUpdatedEvent.propOverrideDateTime;
}
2022-01-25 11:17:19 +01:00
if (favorite != null) {
properties |= FilePropertyUpdatedEvent.propFavorite;
}
if (location != null) {
properties |= FilePropertyUpdatedEvent.propImageLocation;
}
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
}
final FileRepo fileRepo;
2021-05-28 19:15:09 +02:00
}
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-06-21 12:39:17 +02:00
/// Convenience function to only update overrideDateTime
///
/// See [UpdateProperty.call]
Future<void> updateOverrideDateTime(
Account account, File file, DateTime overrideDateTime) =>
call(account, file, overrideDateTime: OrNull(overrideDateTime));
2021-04-10 06:28:12 +02:00
}