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

215 lines
4.7 KiB
Dart
Raw Normal View History

2021-07-05 09:54:01 +02:00
import 'dart:math';
import 'package:equatable/equatable.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/list_extension.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
List<AlbumItem> makeDistinctAlbumItems(List<AlbumItem> items) =>
items.distinctIf((a, b) {
if (a is! AlbumFileItem || b is! AlbumFileItem) {
return false;
} else {
return a.file.compareServerIdentity(b.file);
}
}, (a) {
2021-07-05 09:54:01 +02:00
if (a is AlbumFileItem) {
return a.file.path.hashCode;
} else {
return Random().nextInt(0xFFFFFFFF);
}
});
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) {
final addedBy = 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(),
"addedBy": addedBy,
"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,
];
final String 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({
required String 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(
JsonObj json, String 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({
String? 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({
required String 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(
JsonObj json, String 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({
String? 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";
}