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

105 lines
2.3 KiB
Dart
Raw Normal View History

2021-07-05 09:54:01 +02:00
import 'dart:math';
import 'package:equatable/equatable.dart';
2021-07-07 21:02:48 +02:00
import 'package:flutter/foundation.dart';
2021-07-05 09:54:01 +02:00
import 'package:logging/logging.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/list_extension.dart';
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();
factory AlbumItem.fromJson(Map<String, dynamic> json) {
final type = json["type"];
final content = json["content"];
switch (type) {
case AlbumFileItem._type:
return AlbumFileItem.fromJson(content.cast<String, dynamic>());
default:
_log.shout("[fromJson] Unknown type: $type");
throw ArgumentError.value(type, "type");
}
}
Map<String, dynamic> toJson() {
String getType() {
if (this is AlbumFileItem) {
return AlbumFileItem._type;
} else {
throw StateError("Unknwon subtype");
}
}
return {
"type": getType(),
"content": toContentJson(),
};
}
Map<String, dynamic> toContentJson();
static final _log = Logger("entity.album.AlbumItem");
}
class AlbumFileItem extends AlbumItem with EquatableMixin {
2021-07-07 21:02:48 +02:00
AlbumFileItem({
@required this.file,
});
2021-07-05 09:54:01 +02:00
@override
// ignore: hash_and_equals
bool operator ==(Object other) => equals(other, isDeep: true);
bool equals(Object other, {bool isDeep = false}) {
if (other is AlbumFileItem) {
return super == other &&
(file == null) == (other.file == null) &&
(file?.equals(other.file, isDeep: isDeep) ?? true);
} else {
return false;
}
}
factory AlbumFileItem.fromJson(Map<String, dynamic> json) {
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";
}