yumi-flutter/lib/shared/tools/sc_chat_bubble_message_utils.dart
2026-06-03 20:34:50 +08:00

98 lines
2.7 KiB
Dart

import 'dart:convert';
import 'package:yumi/shared/business_logic/models/res/login_res.dart';
const String scChatBubbleCloudDataKey = 'scChatBubble';
const String scChatBubbleMessageExtensionKey = scChatBubbleCloudDataKey;
String? scEncodeChatBubbleCloudCustomData(PropsResources? chatBubble) {
final snapshot = scChatBubbleSnapshotJson(chatBubble);
if (snapshot == null) {
return null;
}
return jsonEncode(<String, dynamic>{scChatBubbleCloudDataKey: snapshot});
}
String? scEncodeChatBubbleSnapshotValue(PropsResources? chatBubble) {
final snapshot = scChatBubbleSnapshotJson(chatBubble);
return snapshot == null ? null : jsonEncode(snapshot);
}
PropsResources? scDecodeChatBubbleFromCloudCustomData(String? cloudCustomData) {
final raw = cloudCustomData?.trim();
if (raw == null || raw.isEmpty) {
return null;
}
try {
final decoded = jsonDecode(raw);
if (decoded is! Map) {
return null;
}
final data = decoded[scChatBubbleCloudDataKey];
if (data is Map) {
return PropsResources.fromJson(data);
}
} catch (_) {}
return null;
}
PropsResources? scDecodeChatBubbleSnapshotValue(String? snapshotValue) {
final raw = snapshotValue?.trim();
if (raw == null || raw.isEmpty) {
return null;
}
try {
final decoded = jsonDecode(raw);
if (decoded is Map) {
return PropsResources.fromJson(decoded);
}
} catch (_) {}
return null;
}
Map<String, dynamic>? scChatBubbleSnapshotJson(PropsResources? resource) {
if (!_hasChatBubbleImage(resource)) {
return null;
}
final map = <String, dynamic>{};
void put(String key, String? value) {
final text = value?.trim();
if (text != null && text.isNotEmpty) {
map[key] = text;
}
}
put('id', resource?.id);
put('name', resource?.name);
put('type', resource?.type);
put('cover', resource?.cover);
put('expand', resource?.expand);
put('sourceUrl', resource?.sourceUrl);
put('imSendCoverUrl', resource?.imSendCoverUrl);
put('imOpenedUrl', resource?.imOpenedUrl);
put('imOpenedUrlTwo', resource?.imOpenedUrlTwo);
put('imNotOpenedUrl', resource?.imNotOpenedUrl);
put('roomSendCoverUrl', resource?.roomSendCoverUrl);
put('roomOpenedUrl', resource?.roomOpenedUrl);
put('roomNotOpenedUrl', resource?.roomNotOpenedUrl);
return map;
}
bool _hasChatBubbleImage(PropsResources? resource) {
if (resource == null) {
return false;
}
return <String?>[
resource.imSendCoverUrl,
resource.imOpenedUrl,
resource.imOpenedUrlTwo,
resource.imNotOpenedUrl,
resource.roomSendCoverUrl,
resource.roomOpenedUrl,
resource.roomNotOpenedUrl,
resource.cover,
resource.expand,
resource.sourceUrl,
].any((value) => (value?.trim() ?? '').isNotEmpty);
}