yumi-flutter/lib/shared/tools/sc_chat_bubble_message_data.dart

84 lines
2.2 KiB
Dart

import 'dart:convert';
import 'package:yumi/shared/business_logic/models/res/login_res.dart';
const String _chatBubbleSnapshotKey = 'yumiChatBubbleV1';
class SCChatBubbleMessageSnapshot {
final bool hasSnapshot;
final String imageUrl;
const SCChatBubbleMessageSnapshot({
required this.hasSnapshot,
required this.imageUrl,
});
}
String scResolveChatBubbleImageUrl(PropsResources? resource) {
final candidates = <String?>[
resource?.imSendCoverUrl,
resource?.imOpenedUrl,
resource?.imOpenedUrlTwo,
resource?.imNotOpenedUrl,
resource?.roomSendCoverUrl,
resource?.roomOpenedUrl,
resource?.roomNotOpenedUrl,
resource?.cover,
resource?.expand,
resource?.sourceUrl,
];
return _firstNonBlank([...candidates.where(_isSvgaResource), ...candidates]);
}
String scEncodeChatBubbleMessageSnapshot(PropsResources? resource) {
return jsonEncode(<String, dynamic>{
_chatBubbleSnapshotKey: scResolveChatBubbleImageUrl(resource),
});
}
SCChatBubbleMessageSnapshot scDecodeChatBubbleMessageSnapshot(
String? cloudCustomData,
) {
final data = cloudCustomData?.trim() ?? '';
if (data.isEmpty) {
return const SCChatBubbleMessageSnapshot(hasSnapshot: false, imageUrl: '');
}
try {
final decoded = jsonDecode(data);
if (decoded is! Map || !decoded.containsKey(_chatBubbleSnapshotKey)) {
return const SCChatBubbleMessageSnapshot(
hasSnapshot: false,
imageUrl: '',
);
}
final imageUrl = decoded[_chatBubbleSnapshotKey];
if (imageUrl is! String) {
return const SCChatBubbleMessageSnapshot(
hasSnapshot: false,
imageUrl: '',
);
}
return SCChatBubbleMessageSnapshot(
hasSnapshot: true,
imageUrl: imageUrl.trim(),
);
} catch (_) {
return const SCChatBubbleMessageSnapshot(hasSnapshot: false, imageUrl: '');
}
}
bool _isSvgaResource(String? resource) {
final lower = resource?.trim().toLowerCase() ?? '';
return lower.endsWith('.svga') || lower.contains('.svga?');
}
String _firstNonBlank(Iterable<String?> values) {
for (final value in values) {
final text = value?.trim();
if (text != null && text.isNotEmpty) {
return text;
}
}
return '';
}