From 4041d6dc4e8c399b894435a69d011b001a499414 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 13 Oct 2024 16:26:46 +0800 Subject: [PATCH] :zap: Optimize chat list and attachment loading --- .../xcshareddata/xcschemes/Runner.xcscheme | 1 + lib/providers/content/attachment.dart | 15 +++++++++++ lib/screens/dashboard.dart | 2 +- lib/widgets/attachments/attachment_list.dart | 27 ++++++++++++++++--- lib/widgets/chat/chat_event_list.dart | 1 + 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme b/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme index 8e3ca5d..1808b8a 100644 --- a/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme +++ b/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme @@ -59,6 +59,7 @@ ignoresPersistentStateOnLaunch = "NO" debugDocumentVersioning = "YES" debugServiceExtension = "internal" + showGraphicsOverview = "Yes" allowLocationSimulation = "YES"> diff --git a/lib/providers/content/attachment.dart b/lib/providers/content/attachment.dart index 0112063..fcc9ec2 100644 --- a/lib/providers/content/attachment.dart +++ b/lib/providers/content/attachment.dart @@ -23,6 +23,21 @@ class AttachmentProvider extends GetConnect { final Map _cachedResponses = {}; + List listMetadataFromCache(List rid) { + if (rid.isEmpty) return List.empty(); + + List 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> listMetadata( List rid, { noCache = false, diff --git a/lib/screens/dashboard.dart b/lib/screens/dashboard.dart index ca10da0..4b71306 100644 --- a/lib/screens/dashboard.dart +++ b/lib/screens/dashboard.dart @@ -529,7 +529,7 @@ class _DashboardScreenState extends State { style: TextStyle(color: _unFocusColor, fontSize: 12), ) ], - ).paddingAll(8), + ).paddingOnly(left: 8, right: 8, top: 8, bottom: 50), ], ), ); diff --git a/lib/widgets/attachments/attachment_list.dart b/lib/widgets/attachments/attachment_list.dart index 40c1d79..f634a92 100644 --- a/lib/widgets/attachments/attachment_list.dart +++ b/lib/widgets/attachments/attachment_list.dart @@ -134,7 +134,17 @@ class _AttachmentListState extends State { super.initState(); assert(widget.attachmentIds != null || widget.attachments != null); if (widget.attachments == null) { - _getMetadataList(); + 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 { 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 { 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 { 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( diff --git a/lib/widgets/chat/chat_event_list.dart b/lib/widgets/chat/chat_event_list.dart index e5e2e66..03bebad 100644 --- a/lib/widgets/chat/chat_event_list.dart +++ b/lib/widgets/chat/chat_event_list.dart @@ -37,6 +37,7 @@ class ChatEventList extends StatelessWidget { @override Widget build(BuildContext context) { return CustomScrollView( + cacheExtent: 100, reverse: true, slivers: [ Obx(() {