perf: reduce bridge and UI churn

This commit is contained in:
zarzet
2026-05-03 14:12:53 +07:00
parent b329acd710
commit 34894faabf
24 changed files with 2451 additions and 494 deletions
+44 -10
View File
@@ -3,10 +3,14 @@ import 'package:cached_network_image/cached_network_image.dart';
import 'package:spotiflac_android/services/cover_cache_manager.dart';
class CachedCoverImage extends StatelessWidget {
static const int _defaultMinCacheExtent = 64;
static const int _defaultMaxCacheExtent = 512;
final String imageUrl;
final double? width;
final double? height;
final BoxFit fit;
final Alignment alignment;
final int? memCacheWidth;
final int? memCacheHeight;
final Widget Function(BuildContext, String, Object)? errorWidget;
@@ -19,6 +23,7 @@ class CachedCoverImage extends StatelessWidget {
this.width,
this.height,
this.fit = BoxFit.cover,
this.alignment = Alignment.center,
this.memCacheWidth,
this.memCacheHeight,
this.errorWidget,
@@ -28,36 +33,65 @@ class CachedCoverImage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final autoMemCacheWidth =
memCacheWidth ?? _cacheExtentForLogicalSize(context, width);
final autoMemCacheHeight =
memCacheHeight ?? _cacheExtentForLogicalSize(context, height);
final image = CachedNetworkImage(
imageUrl: imageUrl,
width: width,
height: height,
fit: fit,
memCacheWidth: memCacheWidth,
memCacheHeight: memCacheHeight,
cacheManager: CoverCacheManager.isInitialized
? CoverCacheManager.instance
alignment: alignment,
memCacheWidth: autoMemCacheWidth,
memCacheHeight: autoMemCacheHeight,
maxWidthDiskCache: autoMemCacheWidth,
maxHeightDiskCache: autoMemCacheHeight,
cacheManager: CoverCacheManager.isInitialized
? CoverCacheManager.instance
: null,
fadeInDuration: Duration.zero,
fadeOutDuration: Duration.zero,
useOldImageOnUrlChange: true,
filterQuality: FilterQuality.low,
errorWidget: errorWidget,
placeholder: placeholder,
);
if (borderRadius != null) {
return ClipRRect(
borderRadius: borderRadius!,
child: image,
);
return ClipRRect(borderRadius: borderRadius!, child: image);
}
return image;
}
static int? _cacheExtentForLogicalSize(BuildContext context, double? size) {
if (size == null || !size.isFinite || size <= 0) return null;
final dpr = MediaQuery.devicePixelRatioOf(
context,
).clamp(1.0, 3.0).toDouble();
return (size * dpr)
.round()
.clamp(_defaultMinCacheExtent, _defaultMaxCacheExtent)
.toInt();
}
}
CachedNetworkImageProvider cachedCoverImageProvider(String url) {
return CachedNetworkImageProvider(
url,
cacheManager: CoverCacheManager.isInitialized
? CoverCacheManager.instance
cacheManager: CoverCacheManager.isInitialized
? CoverCacheManager.instance
: null,
);
}
int coverImageCacheExtent(
BuildContext context,
double logicalSize, {
int min = 64,
int max = 512,
}) {
final dpr = MediaQuery.devicePixelRatioOf(context).clamp(1.0, 3.0).toDouble();
return (logicalSize * dpr).round().clamp(min, max).toInt();
}