From b9ad6d4fd0fa3efaf4be9e55834c4340a2a56ed9 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 13 Nov 2024 00:25:02 +0800 Subject: [PATCH] :bug: Fix attachment loading --- lib/providers/sn_attachment.dart | 5 +-- lib/screens/account/profile_edit.dart | 2 +- .../account/publishers/publisher_edit.dart | 2 +- lib/widgets/universal_image.dart | 34 +++++++++++++++++++ 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/lib/providers/sn_attachment.dart b/lib/providers/sn_attachment.dart index e06dc71..d03384d 100644 --- a/lib/providers/sn_attachment.dart +++ b/lib/providers/sn_attachment.dart @@ -49,9 +49,10 @@ class SnAttachmentProvider { .map((e) => SnAttachment.fromJson(e)) .toList(); - for (var i = 0; i < out.length; i++) { - _cache[pendingFetch[i]] = out[i]; + for (final item in out) { + _cache[item.rid] = item; } + return rids .where((rid) => _cache.containsKey(rid)) .map((rid) => _cache[rid]!) diff --git a/lib/screens/account/profile_edit.dart b/lib/screens/account/profile_edit.dart index 695b2d7..5916f05 100644 --- a/lib/screens/account/profile_edit.dart +++ b/lib/screens/account/profile_edit.dart @@ -232,7 +232,7 @@ class _ProfileEditScreenState extends State { color: Theme.of(context).colorScheme.surfaceContainerHigh, child: _banner != null - ? UniversalImage( + ? AutoResizeUniversalImage( sn.getAttachmentUrl(_banner!), fit: BoxFit.cover, ) diff --git a/lib/screens/account/publishers/publisher_edit.dart b/lib/screens/account/publishers/publisher_edit.dart index 30ba8ec..5233c9b 100644 --- a/lib/screens/account/publishers/publisher_edit.dart +++ b/lib/screens/account/publishers/publisher_edit.dart @@ -210,7 +210,7 @@ class _AccountPublisherEditScreenState .colorScheme .surfaceContainerHigh, child: _banner != null - ? UniversalImage( + ? AutoResizeUniversalImage( sn.getAttachmentUrl(_banner!), fit: BoxFit.cover, ) diff --git a/lib/widgets/universal_image.dart b/lib/widgets/universal_image.dart index 5eb6f11..807bb54 100644 --- a/lib/widgets/universal_image.dart +++ b/lib/widgets/universal_image.dart @@ -100,3 +100,37 @@ class UniversalImage extends StatelessWidget { return NetworkImage(url); } } + +class AutoResizeUniversalImage extends StatelessWidget { + final String url; + final double? width, height; + final BoxFit? fit; + final bool noProgressIndicator; + final bool noErrorWidget; + + const AutoResizeUniversalImage( + this.url, { + super.key, + this.width, + this.height, + this.fit, + this.noProgressIndicator = false, + this.noErrorWidget = false, + }); + + @override + Widget build(BuildContext context) { + return LayoutBuilder(builder: (context, constraints) { + return UniversalImage( + url, + fit: fit, + width: width, + height: height, + noProgressIndicator: noProgressIndicator, + noErrorWidget: noErrorWidget, + cacheHeight: constraints.maxHeight, + cacheWidth: constraints.maxWidth, + ); + }); + } +}