Optimize chat list and attachment loading

This commit is contained in:
LittleSheep 2024-10-13 16:26:46 +08:00
parent cc1071d86e
commit 4041d6dc4e
5 changed files with 41 additions and 5 deletions

View File

@ -59,6 +59,7 @@
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
showGraphicsOverview = "Yes"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">

View File

@ -23,6 +23,21 @@ class AttachmentProvider extends GetConnect {
final Map<String, Attachment> _cachedResponses = {};
List<Attachment?> listMetadataFromCache(List<String> rid) {
if (rid.isEmpty) return List.empty();
List<Attachment?> result = List.filled(rid.length, null);
for (var idx = 0; idx < rid.length; idx++) {
if (_cachedResponses.containsKey(rid[idx])) {
result[idx] = _cachedResponses[rid[idx]];
} else {
result[idx] = null;
}
}
return result;
}
Future<List<Attachment?>> listMetadata(
List<String> rid, {
noCache = false,

View File

@ -529,7 +529,7 @@ class _DashboardScreenState extends State<DashboardScreen> {
style: TextStyle(color: _unFocusColor, fontSize: 12),
)
],
).paddingAll(8),
).paddingOnly(left: 8, right: 8, top: 8, bottom: 50),
],
),
);

View File

@ -134,7 +134,17 @@ class _AttachmentListState extends State<AttachmentList> {
super.initState();
assert(widget.attachmentIds != null || widget.attachments != null);
if (widget.attachments == null) {
final AttachmentProvider attach = Get.find();
final cachedResult = attach.listMetadataFromCache(widget.attachmentIds!);
if (cachedResult.every((x) => x != null)) {
setState(() {
_attachments = cachedResult;
_isLoading = false;
});
_calculateAspectRatio();
} else {
_getMetadataList();
}
} else {
setState(() {
_attachments = widget.attachments!;
@ -176,7 +186,10 @@ class _AttachmentListState extends State<AttachmentList> {
if (widget.isFullWidth && _attachments.length == 1) {
final element = _attachments.first;
double ratio = element!.metadata?['ratio']?.toDouble() ?? 16 / 9;
double ratio = math.max(
element!.metadata?['ratio']?.toDouble() ?? 16 / 9,
0.5,
);
return Container(
width: MediaQuery.of(context).size.width,
constraints: BoxConstraints(
@ -243,7 +256,10 @@ class _AttachmentListState extends State<AttachmentList> {
final element = _attachments[idx];
idx++;
if (element == null) return const SizedBox.shrink();
double ratio = element.metadata?['ratio']?.toDouble() ?? 16 / 9;
double ratio = math.max(
element.metadata?['ratio']?.toDouble() ?? 16 / 9,
0.5,
);
return Container(
constraints: BoxConstraints(
maxWidth: widget.columnMaxWidth,
@ -282,7 +298,10 @@ class _AttachmentListState extends State<AttachmentList> {
itemBuilder: (context, idx) {
final element = _attachments[idx];
if (element == null) const SizedBox.shrink();
final ratio = element!.metadata?['ratio']?.toDouble() ?? 16 / 9;
double ratio = math.max(
element!.metadata?['ratio']?.toDouble() ?? 16 / 9,
0.5,
);
return Container(
constraints: BoxConstraints(
maxWidth: math.min(

View File

@ -37,6 +37,7 @@ class ChatEventList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return CustomScrollView(
cacheExtent: 100,
reverse: true,
slivers: [
Obx(() {