2024-02-16 18:57:48 +01:00
|
|
|
import 'package:copy_with/copy_with.dart';
|
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';
|
2024-02-13 18:12:04 +01:00
|
|
|
import 'package:nc_photos/entity/file_descriptor.dart';
|
2022-12-16 16:01:04 +01:00
|
|
|
import 'package:np_codegen/np_codegen.dart';
|
2023-02-23 15:49:17 +01:00
|
|
|
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
|
2021-10-23 20:13:06 +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"]);
|
2021-10-23 20:13:06 +02:00
|
|
|
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:
|
2021-10-23 20:13:06 +02:00
|
|
|
return AlbumFileItem.fromJson(
|
|
|
|
content.cast<String, dynamic>(), addedBy, addedAt);
|
2021-07-09 12:07:37 +02:00
|
|
|
case AlbumLabelItem._type:
|
2021-10-23 20:13:06 +02:00
|
|
|
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(),
|
2021-10-23 20:13:06 +02:00
|
|
|
"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
|
|
|
|
2024-02-13 18:12:04 +01:00
|
|
|
bool compareServerIdentity(AlbumItem other);
|
|
|
|
|
2021-10-23 20:13:06 +02:00
|
|
|
@override
|
2022-12-08 16:39:13 +01:00
|
|
|
String toString() => _$toString();
|
2021-10-23 20:13:06 +02:00
|
|
|
|
|
|
|
@override
|
2024-02-13 18:12:04 +01:00
|
|
|
List<Object?> get props => [
|
2021-10-23 20:13:06 +02:00
|
|
|
addedBy,
|
|
|
|
addedAt,
|
|
|
|
];
|
|
|
|
|
2021-11-12 22:13:02 +01:00
|
|
|
final CiString addedBy;
|
2021-10-23 20:13:06 +02:00
|
|
|
final DateTime addedAt;
|
|
|
|
|
2022-12-20 17:49:14 +01:00
|
|
|
static final _log = _$AlbumItemNpLog.log;
|
2021-07-05 09:54:01 +02:00
|
|
|
}
|
|
|
|
|
2024-02-16 18:57:48 +01:00
|
|
|
@genCopyWith
|
2022-12-08 16:39:13 +01:00
|
|
|
@toString
|
2021-10-23 20:13:06 +02:00
|
|
|
class AlbumFileItem extends AlbumItem {
|
2021-07-07 21:02:48 +02:00
|
|
|
AlbumFileItem({
|
2024-02-13 18:12:04 +01:00
|
|
|
required super.addedBy,
|
|
|
|
required super.addedAt,
|
2021-07-23 22:05:57 +02:00
|
|
|
required this.file,
|
2024-02-13 18:12:04 +01:00
|
|
|
required this.ownerId,
|
|
|
|
});
|
2021-07-05 09:54:01 +02:00
|
|
|
|
2021-10-23 20:13:06 +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(
|
2021-10-23 20:13:06 +02:00
|
|
|
addedBy: addedBy,
|
|
|
|
addedAt: addedAt,
|
2024-02-13 18:12:04 +01:00
|
|
|
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
|
2024-02-13 18:12:04 +01:00
|
|
|
JsonObj toContentJson() {
|
2021-07-05 09:54:01 +02:00
|
|
|
return {
|
2024-02-13 18:12:04 +01:00
|
|
|
"file": file.toFdJson(),
|
|
|
|
"ownerId": ownerId.raw,
|
2021-07-05 09:54:01 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-02-13 18:12:04 +01:00
|
|
|
@override
|
|
|
|
bool compareServerIdentity(AlbumItem other) =>
|
|
|
|
other is AlbumFileItem &&
|
|
|
|
file.compareServerIdentity(other.file) &&
|
|
|
|
addedBy == other.addedBy &&
|
|
|
|
addedAt == other.addedAt;
|
|
|
|
|
2021-07-05 09:54:01 +02:00
|
|
|
@override
|
2024-02-13 18:12:04 +01:00
|
|
|
List<Object?> get props => [
|
2021-10-23 20:13:06 +02:00
|
|
|
...super.props,
|
2024-02-13 18:12:04 +01:00
|
|
|
file,
|
|
|
|
ownerId,
|
2021-07-05 09:54:01 +02:00
|
|
|
];
|
|
|
|
|
2024-02-13 18:12:04 +01: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
|
2021-10-23 20:13:06 +02:00
|
|
|
class AlbumLabelItem extends AlbumItem {
|
2021-07-09 12:07:37 +02:00
|
|
|
AlbumLabelItem({
|
2024-05-28 17:10:33 +02:00
|
|
|
required super.addedBy,
|
|
|
|
required super.addedAt,
|
2021-07-23 22:05:57 +02:00
|
|
|
required this.text,
|
2024-05-28 17:10:33 +02:00
|
|
|
});
|
2021-07-09 12:07:37 +02:00
|
|
|
|
2021-10-23 20:13:06 +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(
|
2021-10-23 20:13:06 +02:00
|
|
|
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
|
2024-02-13 18:12:04 +01:00
|
|
|
JsonObj toContentJson() {
|
2021-07-09 12:07:37 +02:00
|
|
|
return {
|
|
|
|
"text": text,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-02-13 18:12:04 +01:00
|
|
|
@override
|
|
|
|
bool compareServerIdentity(AlbumItem other) =>
|
|
|
|
other is AlbumLabelItem &&
|
|
|
|
text == other.text &&
|
|
|
|
addedBy == other.addedBy &&
|
|
|
|
addedAt == other.addedAt;
|
|
|
|
|
2021-10-23 20:13:06 +02:00
|
|
|
AlbumLabelItem copyWith({
|
2021-11-12 22:13:02 +01:00
|
|
|
CiString? addedBy,
|
2021-10-23 20:13:06 +02:00
|
|
|
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
|
2024-02-13 18:12:04 +01:00
|
|
|
List<Object?> get props => [
|
2021-10-23 20:13:06 +02:00
|
|
|
...super.props,
|
2021-07-09 12:07:37 +02:00
|
|
|
text,
|
|
|
|
];
|
|
|
|
|
|
|
|
final String text;
|
|
|
|
|
|
|
|
static const _type = "label";
|
|
|
|
}
|