nc-photos/app/lib/entity/album/item.dart

198 lines
4.3 KiB
Dart
Raw Normal View History

2021-07-05 09:54:01 +02:00
import 'package:equatable/equatable.dart';
import 'package:logging/logging.dart';
2021-11-12 22:13:02 +01:00
import 'package:nc_photos/ci_string.dart';
2021-07-05 09:54:01 +02:00
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/or_null.dart';
2021-08-06 19:11:00 +02:00
import 'package:nc_photos/type.dart';
2021-07-05 09:54:01 +02:00
abstract class AlbumItem with EquatableMixin {
AlbumItem({
required this.addedBy,
required DateTime addedAt,
}) : addedAt = addedAt.toUtc();
2021-07-05 09:54:01 +02:00
2021-08-06 19:11:00 +02:00
factory AlbumItem.fromJson(JsonObj json) {
2021-11-12 22:13:02 +01:00
final addedBy = CiString(json["addedBy"]);
final addedAt = DateTime.parse(json["addedAt"]);
2021-07-05 09:54:01 +02:00
final type = json["type"];
final content = json["content"];
switch (type) {
case AlbumFileItem._type:
return AlbumFileItem.fromJson(
content.cast<String, dynamic>(), addedBy, addedAt);
2021-07-09 12:07:37 +02:00
case AlbumLabelItem._type:
return AlbumLabelItem.fromJson(
content.cast<String, dynamic>(), addedBy, addedAt);
2021-07-05 09:54:01 +02:00
default:
_log.shout("[fromJson] Unknown type: $type");
throw ArgumentError.value(type, "type");
}
}
2021-08-06 19:11:00 +02:00
JsonObj toJson() {
2021-07-05 09:54:01 +02:00
String getType() {
if (this is AlbumFileItem) {
return AlbumFileItem._type;
2021-07-09 12:07:37 +02:00
} else if (this is AlbumLabelItem) {
return AlbumLabelItem._type;
2021-07-05 09:54:01 +02:00
} else {
throw StateError("Unknwon subtype");
}
}
return {
"type": getType(),
"content": toContentJson(),
2021-11-12 22:13:02 +01:00
"addedBy": addedBy.toString(),
"addedAt": addedAt.toIso8601String(),
2021-07-05 09:54:01 +02:00
};
}
2021-08-06 19:11:00 +02:00
JsonObj toContentJson();
2021-07-05 09:54:01 +02:00
@override
toString() {
return "$runtimeType {"
"addedBy: '$addedBy', "
"addedAt: $addedAt, "
"}";
}
@override
get props => [
addedBy,
addedAt,
];
2021-11-12 22:13:02 +01:00
final CiString addedBy;
final DateTime addedAt;
2021-07-05 09:54:01 +02:00
static final _log = Logger("entity.album.AlbumItem");
}
class AlbumFileItem extends AlbumItem {
2021-07-07 21:02:48 +02:00
AlbumFileItem({
2021-11-12 22:13:02 +01:00
required CiString addedBy,
required DateTime addedAt,
2021-07-23 22:05:57 +02:00
required this.file,
}) : super(addedBy: addedBy, addedAt: addedAt);
2021-07-05 09:54:01 +02:00
@override
// ignore: hash_and_equals
2021-07-23 22:05:57 +02:00
bool operator ==(Object? other) => equals(other, isDeep: true);
2021-07-05 09:54:01 +02:00
2021-07-23 22:05:57 +02:00
bool equals(Object? other, {bool isDeep = false}) {
2021-07-05 09:54:01 +02:00
if (other is AlbumFileItem) {
2021-07-23 22:05:57 +02:00
return super == other && (file.equals(other.file, isDeep: isDeep));
2021-07-05 09:54:01 +02:00
} else {
return false;
}
}
factory AlbumFileItem.fromJson(
2021-11-12 22:13:02 +01:00
JsonObj json, CiString addedBy, DateTime addedAt) {
2021-07-05 09:54:01 +02:00
return AlbumFileItem(
addedBy: addedBy,
addedAt: addedAt,
2021-07-05 09:54:01 +02:00
file: File.fromJson(json["file"].cast<String, dynamic>()),
);
}
@override
toString() {
return "$runtimeType {"
"super: ${super.toString()}, "
"file: $file, "
2021-07-05 09:54:01 +02:00
"}";
}
@override
toContentJson() {
return {
"file": file.toJson(),
};
}
AlbumFileItem copyWith({
2021-11-12 22:13:02 +01:00
CiString? addedBy,
DateTime? addedAt,
File? file,
}) {
return AlbumFileItem(
addedBy: addedBy ?? this.addedBy,
addedAt: addedAt ?? this.addedAt,
file: file ?? this.file,
);
}
AlbumFileItem minimize() => AlbumFileItem(
addedBy: addedBy,
addedAt: addedAt,
file: file.copyWith(metadata: OrNull(null)),
);
2021-07-05 09:54:01 +02:00
@override
get props => [
...super.props,
2021-07-05 09:54:01 +02:00
// file is handled separately, see [equals]
];
final File file;
static const _type = "file";
}
2021-07-09 12:07:37 +02:00
class AlbumLabelItem extends AlbumItem {
2021-07-09 12:07:37 +02:00
AlbumLabelItem({
2021-11-12 22:13:02 +01:00
required CiString addedBy,
required DateTime addedAt,
2021-07-23 22:05:57 +02:00
required this.text,
}) : super(addedBy: addedBy, addedAt: addedAt);
2021-07-09 12:07:37 +02:00
factory AlbumLabelItem.fromJson(
2021-11-12 22:13:02 +01:00
JsonObj json, CiString addedBy, DateTime addedAt) {
2021-07-09 12:07:37 +02:00
return AlbumLabelItem(
addedBy: addedBy,
addedAt: addedAt,
2021-07-09 12:07:37 +02:00
text: json["text"],
);
}
@override
toString() {
return "$runtimeType {"
"super: ${super.toString()}, "
2021-07-09 12:07:37 +02:00
"text: '$text', "
"}";
}
@override
toContentJson() {
return {
"text": text,
};
}
AlbumLabelItem copyWith({
2021-11-12 22:13:02 +01:00
CiString? addedBy,
DateTime? addedAt,
String? text,
}) {
return AlbumLabelItem(
addedBy: addedBy ?? this.addedBy,
addedAt: addedAt ?? this.addedAt,
text: text ?? this.text,
);
}
2021-07-09 12:07:37 +02:00
@override
get props => [
...super.props,
2021-07-09 12:07:37 +02:00
text,
];
final String text;
static const _type = "label";
}