petsHero-AI/lib/features/gallery/models/gallery_task_item.dart
2026-03-31 09:42:49 +08:00

99 lines
3.2 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/// 媒体项reconfigure=imgUrl类型由 reconnect(imgType) 决定0=视频1=图片,其他当图片
class GalleryMediaItem {
const GalleryMediaItem({
this.imageUrl,
this.videoUrl,
this.taskId,
}) : assert(imageUrl != null || videoUrl != null);
final String? imageUrl;
final String? videoUrl;
/// 与列表项 `tree` 一致,用于匹配本地上传封面缓存
final int? taskId;
/// reconnect==0 为视频1 或其他为图片
bool get isVideo =>
videoUrl != null && (imageUrl == null || imageUrl!.isEmpty);
}
/// 我的任务项V2 字段映射)
class GalleryTaskItem {
const GalleryTaskItem({
required this.taskId,
required this.state,
required this.taskType,
required this.createTime,
required this.mediaItems,
});
final int taskId;
final String state;
final int taskType;
final int createTime;
final List<GalleryMediaItem> mediaItems;
factory GalleryTaskItem.fromJson(Map<String, dynamic> json) {
final treeRaw = json['tree'] as num?;
final treeId = treeRaw?.toInt() ?? 0;
final itemTaskId = treeId > 0 ? treeId : null;
final downsample = json['downsample'] as List<dynamic>? ?? [];
final items = <GalleryMediaItem>[];
// 只取downsample的array[0]
if (downsample.isNotEmpty) {
final first = downsample[0];
if (first is String) {
items.add(GalleryMediaItem(imageUrl: first, taskId: itemTaskId));
} else if (first is Map<String, dynamic>) {
final reconfigure = first['reconfigure'] as String?;
if (reconfigure != null && reconfigure.isNotEmpty) {
final reconnect = first['reconnect'];
final imgType = reconnect is int
? reconnect
: reconnect is num
? reconnect.toInt()
: 1;
if (imgType == 2) {
items.add(GalleryMediaItem(
videoUrl: reconfigure,
taskId: itemTaskId,
));
} else {
items.add(GalleryMediaItem(
imageUrl: reconfigure,
taskId: itemTaskId,
));
}
}
}
// 后续下标均忽略
}
// for (final item in downsample) {
// if (item is String) {
// items.add(GalleryMediaItem(imageUrl: item));
// } else if (item is Map<String, dynamic>) {
// final reconfigure = item['reconfigure'] as String?;
// if (reconfigure == null || reconfigure.isEmpty) continue;
// // reconnect(imgType): 0=视频1=图片,其他默认当图片
// final reconnect = item['reconnect'];
// final imgType = reconnect is int
// ? reconnect
// : reconnect is num
// ? reconnect.toInt()
// : 1;
// if (imgType == 2) {
// items.add(GalleryMediaItem(videoUrl: reconfigure));
// } else {
// items.add(GalleryMediaItem(imageUrl: reconfigure));
// }
// }
// }
return GalleryTaskItem(
taskId: treeId,
state: json['listing']?.toString() ?? '',
taskType: (json['cipher'] as num?)?.toInt() ?? 0,
createTime: (json['discover'] as num?)?.toInt() ?? 0,
mediaItems: items,
);
}
}