11import 'dart:typed_data' ;
22import 'dart:convert' ;
33
4+ /// 图片消息元数据(不含图片数据)
5+ /// 用于快速加载历史记录,图片数据按需加载
46class ImageMessage {
57 final String prompt;
6- final Uint8List image;
8+ final String ? imageRef; // 图片引用ID,不包含实际图片数据
79 final String ? aiDescription;
810 final DateTime timestamp;
911 final bool isSeparator;
1012
11- ImageMessage (this .prompt, this .image, this .aiDescription, [DateTime ? timestamp, this .isSeparator = false ]) : timestamp = timestamp ?? DateTime .now ();
13+ ImageMessage ({
14+ required this .prompt,
15+ this .imageRef,
16+ this .aiDescription,
17+ DateTime ? timestamp,
18+ this .isSeparator = false ,
19+ }) : timestamp = timestamp ?? DateTime .now ();
1220
21+ /// 创建包含图片数据的完整消息(用于显示时)
22+ ImageMessage withImageData (Uint8List imageData) {
23+ return ImageMessage (
24+ prompt: prompt,
25+ imageRef: imageRef,
26+ aiDescription: aiDescription,
27+ timestamp: timestamp,
28+ isSeparator: isSeparator,
29+ ).._cachedImageData = imageData;
30+ }
31+
32+ /// 缓存的图片数据(仅用于显示时)
33+ Uint8List ? _cachedImageData;
34+
35+ /// 获取缓存的图片数据
36+ Uint8List ? get cachedImageData => _cachedImageData;
37+
38+ /// 设置缓存的图片数据
39+ void setCachedImageData (Uint8List data) {
40+ _cachedImageData = data;
41+ }
42+
43+ /// 清除缓存的图片数据
44+ void clearCachedImageData () {
45+ _cachedImageData = null ;
46+ }
47+
48+ /// 从JSON创建元数据(不含图片数据)
1349 factory ImageMessage .fromJson (Map <String , dynamic > json) {
50+ return ImageMessage (
51+ prompt: json['prompt' ] as String ,
52+ imageRef: json['imageRef' ] as String ? ,
53+ aiDescription: json['aiDescription' ] as String ? ,
54+ timestamp: DateTime .parse (json['timestamp' ] as String ),
55+ isSeparator: (json['isSeparator' ] as bool ? ) ?? false ,
56+ );
57+ }
58+
59+ /// 从旧格式JSON创建(兼容性,用于数据迁移)
60+ factory ImageMessage .fromLegacyJson (Map <String , dynamic > json) {
1461 final prompt = json['prompt' ] as String ;
15- final imageBase64 = json['imageBase64' ] as String ;
16- final image = base64Decode (imageBase64) as Uint8List ;
62+ final imageBase64 = json['imageBase64' ] as String ? ;
1763 final aiDescription = json['aiDescription' ] as String ? ;
1864 final timestampStr = json['timestamp' ] as String ;
1965 final timestamp = DateTime .parse (timestampStr);
2066 final isSeparator = (json['isSeparator' ] as bool ? ) ?? false ;
21- return ImageMessage (prompt, image, aiDescription, timestamp, isSeparator);
67+
68+ // 旧格式包含图片数据,需要迁移
69+ Uint8List ? imageData;
70+ if (imageBase64 != null && imageBase64.isNotEmpty) {
71+ try {
72+ imageData = base64Decode (imageBase64) as Uint8List ;
73+ } catch (e) {
74+ print ('Error decoding legacy image data: $e ' );
75+ }
76+ }
77+
78+ return ImageMessage (
79+ prompt: prompt,
80+ imageRef: null , // 旧格式没有imageRef,需要后续迁移
81+ aiDescription: aiDescription,
82+ timestamp: timestamp,
83+ isSeparator: isSeparator,
84+ ).._cachedImageData = imageData;
2285 }
2386
87+ /// 转换为JSON(只包含元数据)
2488 Map <String , dynamic > toJson () {
2589 return {
2690 'prompt' : prompt,
27- 'imageBase64 ' : base64Encode (image) ,
91+ 'imageRef ' : imageRef ,
2892 'aiDescription' : aiDescription,
2993 'timestamp' : timestamp.toIso8601String (),
3094 'isSeparator' : isSeparator,
3195 };
3296 }
33- }
97+
98+ /// 检查是否有图片
99+ bool get hasImage => imageRef != null && imageRef! .isNotEmpty;
100+
101+ /// 检查是否有缓存的图片数据
102+ bool get hasCachedImageData => _cachedImageData != null ;
103+
104+ /// 复制消息
105+ ImageMessage copyWith ({
106+ String ? prompt,
107+ String ? imageRef,
108+ String ? aiDescription,
109+ DateTime ? timestamp,
110+ bool ? isSeparator,
111+ bool clearCache = false ,
112+ }) {
113+ // 深拷贝缓存的图片数据
114+ Uint8List ? copiedImageData;
115+ if (! clearCache && _cachedImageData != null ) {
116+ copiedImageData = Uint8List .fromList (_cachedImageData! );
117+ }
118+
119+ return ImageMessage (
120+ prompt: prompt ?? this .prompt,
121+ imageRef: imageRef ?? this .imageRef,
122+ aiDescription: aiDescription ?? this .aiDescription,
123+ timestamp: timestamp ?? this .timestamp,
124+ isSeparator: isSeparator ?? this .isSeparator,
125+ ).._cachedImageData = copiedImageData;
126+ }
127+ }
0 commit comments