100 lines
2.5 KiB
Dart
100 lines
2.5 KiB
Dart
|
|
import 'package:elysia/bean/Mappable.dart';
|
|
import 'package:intl/intl.dart';
|
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
import '../plugin/StringTool.dart';
|
|
|
|
class ChatCacheItem extends Mappable {
|
|
final String roomId;
|
|
final String roomName;
|
|
final String roomImage;
|
|
String lastChatMessage;
|
|
String lastChatTime;
|
|
final String engine;
|
|
|
|
ChatCacheItem({
|
|
required this.roomId,
|
|
required this.roomName,
|
|
required this.roomImage,
|
|
required String lastChatMessage,
|
|
required this.lastChatTime,
|
|
required this.engine,
|
|
}) : lastChatMessage = _formatMessage(lastChatMessage);
|
|
|
|
/// 格式化消息(去掉 Markdown + 限制长度)
|
|
static String _formatMessage(String message) {
|
|
String text = StringTool.stripMarkdown(message);
|
|
if (text.length > 51) {
|
|
text = text.substring(0, 50) + "...";
|
|
}
|
|
return text;
|
|
}
|
|
|
|
void updateTime(DateTime date) {
|
|
lastChatTime = DateFormat('HH:mm').format(date);
|
|
}
|
|
|
|
/// copy 方法也会自动格式化 lastChatMessage
|
|
ChatCacheItem copy({
|
|
String? roomId,
|
|
String? roomName,
|
|
String? roomImage,
|
|
String? lastChatMessage,
|
|
String? lastChatTime,
|
|
String? engine,
|
|
}) {
|
|
return ChatCacheItem(
|
|
roomId: roomId ?? this.roomId,
|
|
roomName: roomName ?? this.roomName,
|
|
roomImage: roomImage ?? this.roomImage,
|
|
lastChatMessage: lastChatMessage ?? this.lastChatMessage,
|
|
lastChatTime: lastChatTime ?? this.lastChatTime,
|
|
engine: engine ?? this.engine,
|
|
);
|
|
}
|
|
|
|
factory ChatCacheItem.fromJson(Map<String, dynamic> json) {
|
|
String formatted;
|
|
try {
|
|
if (json['lastChatTime'] == null) {
|
|
formatted = "";
|
|
} else {
|
|
DateTime dateTime;
|
|
if (json['lastChatTime'] is int) {
|
|
dateTime = DateTime.fromMillisecondsSinceEpoch(json['lastChatTime']);
|
|
} else {
|
|
dateTime = DateTime.parse(json['lastChatTime']);
|
|
}
|
|
formatted = DateFormat('HH:mm').format(dateTime);
|
|
}
|
|
} catch (e) {
|
|
formatted = "";
|
|
}
|
|
|
|
return ChatCacheItem(
|
|
roomId: json['roomId'] ?? '',
|
|
roomName: json['roomName'] ?? '',
|
|
roomImage: json['roomImage'] ?? '',
|
|
lastChatMessage: json['lastChatMessage'] ?? '',
|
|
lastChatTime: formatted,
|
|
engine: json['engine'] ?? '',
|
|
);
|
|
}
|
|
|
|
@override
|
|
Map<String, dynamic> toMap() {
|
|
return {
|
|
'roomId': roomId,
|
|
'roomName': roomName,
|
|
'roomImage': roomImage,
|
|
'lastChatMessage': lastChatMessage,
|
|
'lastChatTime': lastChatTime,
|
|
'engine': engine,
|
|
};
|
|
}
|
|
}
|
|
|
|
|