nc-photos/lib/use_case/update_property.dart

78 lines
2.4 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';
2021-04-10 06:28:12 +02:00
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, {
OrNull<Metadata> metadata,
2021-05-28 20:45:00 +02:00
OrNull<bool> isArchived,
2021-06-21 12:39:17 +02:00
OrNull<DateTime> overrideDateTime,
2021-05-28 19:15:09 +02:00
}) async {
2021-06-21 12:39:17 +02:00
if (metadata == null && isArchived == null && overrideDateTime == 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-06-21 12:39:17 +02:00
overrideDateTime: overrideDateTime,
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;
}
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
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-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
}