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

196 lines
4.3 KiB
Dart
Raw Normal View History

2021-07-05 09:54:01 +02:00
import 'package:equatable/equatable.dart';
2022-12-08 16:39:13 +01: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_descriptor.dart';
2022-12-16 16:01:04 +01:00
import 'package:np_codegen/np_codegen.dart';
import 'package:np_common/type.dart';
2023-08-25 18:37:17 +02:00
import 'package:np_string/np_string.dart';
2022-12-08 16:39:13 +01:00
import 'package:to_string/to_string.dart';
2021-07-05 09:54:01 +02:00
2022-12-08 16:39:13 +01:00
part 'item.g.dart';
2022-12-16 16:01:04 +01:00
@npLog
2022-12-08 16:39:13 +01:00
@toString
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
bool compareServerIdentity(AlbumItem other);
@override
2022-12-08 16:39:13 +01:00
String toString() => _$toString();
@override
List<Object?> get props => [
addedBy,
addedAt,
];
2021-11-12 22:13:02 +01:00
final CiString addedBy;
final DateTime addedAt;
2022-12-20 17:49:14 +01:00
static final _log = _$AlbumItemNpLog.log;
2021-07-05 09:54:01 +02:00
}
2022-12-08 16:39:13 +01:00
@toString
class AlbumFileItem extends AlbumItem {
2021-07-07 21:02:48 +02:00
AlbumFileItem({
required super.addedBy,
required super.addedAt,
2021-07-23 22:05:57 +02:00
required this.file,
required this.ownerId,
});
2021-07-05 09:54:01 +02:00
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,
file: FileDescriptor.fromJson(json["file"].cast<String, dynamic>()),
ownerId: (json["ownerId"] as String).toCi(),
2021-07-05 09:54:01 +02:00
);
}
@override
2022-12-08 16:39:13 +01:00
String toString() => _$toString();
2021-07-05 09:54:01 +02:00
@override
JsonObj toContentJson() {
2021-07-05 09:54:01 +02:00
return {
"file": file.toFdJson(),
"ownerId": ownerId.raw,
2021-07-05 09:54:01 +02:00
};
}
@override
bool compareServerIdentity(AlbumItem other) =>
other is AlbumFileItem &&
file.compareServerIdentity(other.file) &&
addedBy == other.addedBy &&
addedAt == other.addedAt;
AlbumFileItem copyWith({
2021-11-12 22:13:02 +01:00
CiString? addedBy,
DateTime? addedAt,
FileDescriptor? file,
CiString? ownerId,
}) {
return AlbumFileItem(
addedBy: addedBy ?? this.addedBy,
addedAt: addedAt ?? this.addedAt,
file: file ?? this.file,
ownerId: ownerId ?? this.ownerId,
);
}
2021-07-05 09:54:01 +02:00
@override
List<Object?> get props => [
...super.props,
file,
ownerId,
2021-07-05 09:54:01 +02:00
];
final FileDescriptor file;
final CiString ownerId;
2021-07-05 09:54:01 +02:00
static const _type = "file";
}
2021-07-09 12:07:37 +02:00
2022-12-08 16:39:13 +01:00
@toString
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
2022-12-08 16:39:13 +01:00
String toString() => _$toString();
2021-07-09 12:07:37 +02:00
@override
JsonObj toContentJson() {
2021-07-09 12:07:37 +02:00
return {
"text": text,
};
}
@override
bool compareServerIdentity(AlbumItem other) =>
other is AlbumLabelItem &&
text == other.text &&
addedBy == other.addedBy &&
addedAt == other.addedAt;
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
List<Object?> get props => [
...super.props,
2021-07-09 12:07:37 +02:00
text,
];
final String text;
static const _type = "label";
}