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

142 lines
2.9 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';
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) =>
a is AlbumFileItem &&
b is AlbumFileItem &&
a.file.path == b.file.path, (a) {
if (a is AlbumFileItem) {
return a.file.path.hashCode;
} else {
return Random().nextInt(0xFFFFFFFF);
}
});
abstract class AlbumItem {
AlbumItem();
2021-08-06 19:11:00 +02:00
factory AlbumItem.fromJson(JsonObj json) {
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>());
2021-07-09 12:07:37 +02:00
case AlbumLabelItem._type:
return AlbumLabelItem.fromJson(content.cast<String, dynamic>());
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-08-06 19:11:00 +02:00
JsonObj toContentJson();
2021-07-05 09:54:01 +02:00
static final _log = Logger("entity.album.AlbumItem");
}
class AlbumFileItem extends AlbumItem with EquatableMixin {
2021-07-07 21:02:48 +02:00
AlbumFileItem({
2021-07-23 22:05:57 +02:00
required this.file,
2021-07-07 21:02:48 +02:00
});
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;
}
}
2021-08-06 19:11:00 +02:00
factory AlbumFileItem.fromJson(JsonObj json) {
2021-07-05 09:54:01 +02:00
return AlbumFileItem(
file: File.fromJson(json["file"].cast<String, dynamic>()),
);
}
@override
toString() {
return "$runtimeType {"
"file: $file"
"}";
}
@override
toContentJson() {
return {
"file": file.toJson(),
};
}
@override
get props => [
// 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 with EquatableMixin {
AlbumLabelItem({
2021-07-23 22:05:57 +02:00
required this.text,
2021-07-09 12:07:37 +02:00
});
2021-08-06 19:11:00 +02:00
factory AlbumLabelItem.fromJson(JsonObj json) {
2021-07-09 12:07:37 +02:00
return AlbumLabelItem(
text: json["text"],
);
}
@override
toString() {
return "$runtimeType {"
"text: '$text', "
"}";
}
@override
toContentJson() {
return {
"text": text,
};
}
@override
get props => [
text,
];
final String text;
static const _type = "label";
}