2022-11-08 19:00:24 +02:00
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2023-02-04 22:42:42 +02:00
|
|
|
import 'package:flutter/services.dart';
|
2023-06-14 16:35:32 +02:00
|
|
|
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
2023-11-09 18:19:53 +02:00
|
|
|
import 'package:immich_mobile/extensions/build_context_extensions.dart';
|
2022-11-08 19:00:24 +02:00
|
|
|
import 'package:immich_mobile/shared/models/asset.dart';
|
2023-03-23 03:36:44 +02:00
|
|
|
import 'package:immich_mobile/shared/models/store.dart';
|
2022-11-08 19:00:24 +02:00
|
|
|
import 'package:immich_mobile/utils/image_url_builder.dart';
|
|
|
|
import 'package:photo_manager/photo_manager.dart';
|
2023-06-27 23:00:20 +02:00
|
|
|
import 'package:openapi/api.dart' as api;
|
2024-01-27 18:14:32 +02:00
|
|
|
import 'package:photo_manager_image_provider/photo_manager_image_provider.dart';
|
2022-11-08 19:00:24 +02:00
|
|
|
|
|
|
|
/// Renders an Asset using local data if available, else remote data
|
|
|
|
class ImmichImage extends StatelessWidget {
|
|
|
|
const ImmichImage(
|
|
|
|
this.asset, {
|
2023-03-15 23:29:07 +02:00
|
|
|
this.width,
|
|
|
|
this.height,
|
|
|
|
this.fit = BoxFit.cover,
|
2022-11-08 19:00:24 +02:00
|
|
|
this.useGrayBoxPlaceholder = false,
|
2023-07-22 17:03:31 +02:00
|
|
|
this.useProgressIndicator = false,
|
2023-06-27 23:00:20 +02:00
|
|
|
this.type = api.ThumbnailFormat.WEBP,
|
2022-11-08 19:00:24 +02:00
|
|
|
super.key,
|
|
|
|
});
|
2023-02-04 22:42:42 +02:00
|
|
|
final Asset? asset;
|
2022-11-08 19:00:24 +02:00
|
|
|
final bool useGrayBoxPlaceholder;
|
2023-07-22 17:03:31 +02:00
|
|
|
final bool useProgressIndicator;
|
2023-03-15 23:29:07 +02:00
|
|
|
final double? width;
|
|
|
|
final double? height;
|
|
|
|
final BoxFit fit;
|
2023-06-27 23:00:20 +02:00
|
|
|
final api.ThumbnailFormat type;
|
2022-11-08 19:00:24 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-02-04 22:42:42 +02:00
|
|
|
if (this.asset == null) {
|
|
|
|
return Container(
|
|
|
|
decoration: const BoxDecoration(
|
|
|
|
color: Colors.grey,
|
|
|
|
),
|
|
|
|
child: SizedBox(
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
child: const Center(
|
|
|
|
child: Icon(Icons.no_photography),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
final Asset asset = this.asset!;
|
2023-09-18 05:57:05 +02:00
|
|
|
if (useLocal(asset)) {
|
2022-11-08 19:00:24 +02:00
|
|
|
return Image(
|
2023-09-18 05:57:05 +02:00
|
|
|
image: localThumbnailProvider(asset),
|
2022-11-08 19:00:24 +02:00
|
|
|
width: width,
|
|
|
|
height: height,
|
2023-03-15 23:29:07 +02:00
|
|
|
fit: fit,
|
2022-11-08 19:00:24 +02:00
|
|
|
frameBuilder: (context, child, frame, wasSynchronouslyLoaded) {
|
|
|
|
if (wasSynchronouslyLoaded || frame != null) {
|
|
|
|
return child;
|
|
|
|
}
|
2023-07-22 17:03:31 +02:00
|
|
|
|
|
|
|
// Show loading if desired
|
|
|
|
return Stack(
|
|
|
|
children: [
|
|
|
|
if (useGrayBoxPlaceholder)
|
|
|
|
const SizedBox.square(
|
2022-11-08 19:00:24 +02:00
|
|
|
dimension: 250,
|
|
|
|
child: DecoratedBox(
|
|
|
|
decoration: BoxDecoration(color: Colors.grey),
|
|
|
|
),
|
2023-07-22 17:03:31 +02:00
|
|
|
),
|
|
|
|
if (useProgressIndicator)
|
|
|
|
const Center(
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
2022-11-08 19:00:24 +02:00
|
|
|
},
|
|
|
|
errorBuilder: (context, error, stackTrace) {
|
2023-02-04 22:42:42 +02:00
|
|
|
if (error is PlatformException &&
|
|
|
|
error.code == "The asset not found!") {
|
|
|
|
debugPrint(
|
|
|
|
"Asset ${asset.localId} does not exist anymore on device!",
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
debugPrint(
|
|
|
|
"Error getting thumb for assetId=${asset.localId}: $error",
|
|
|
|
);
|
|
|
|
}
|
2022-11-08 19:00:24 +02:00
|
|
|
return Icon(
|
|
|
|
Icons.image_not_supported_outlined,
|
2023-11-09 18:19:53 +02:00
|
|
|
color: context.primaryColor,
|
2022-11-08 19:00:24 +02:00
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2023-03-23 03:36:44 +02:00
|
|
|
final String? token = Store.get(StoreKey.accessToken);
|
2023-06-27 23:00:20 +02:00
|
|
|
final String thumbnailRequestUrl = getThumbnailUrl(asset, type: type);
|
2022-11-08 19:00:24 +02:00
|
|
|
return CachedNetworkImage(
|
|
|
|
imageUrl: thumbnailRequestUrl,
|
|
|
|
httpHeaders: {"Authorization": "Bearer $token"},
|
2023-06-29 05:33:57 +02:00
|
|
|
cacheKey: getThumbnailCacheKey(asset, type: type),
|
2022-11-08 19:00:24 +02:00
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
// keeping memCacheWidth, memCacheHeight, maxWidthDiskCache and
|
|
|
|
// maxHeightDiskCache = null allows to simply store the webp thumbnail
|
|
|
|
// from the server and use it for all rendered thumbnail sizes
|
2023-03-15 23:29:07 +02:00
|
|
|
fit: fit,
|
2022-11-08 19:00:24 +02:00
|
|
|
fadeInDuration: const Duration(milliseconds: 250),
|
|
|
|
progressIndicatorBuilder: (context, url, downloadProgress) {
|
2023-07-22 17:03:31 +02:00
|
|
|
// Show loading if desired
|
|
|
|
return Stack(
|
|
|
|
children: [
|
|
|
|
if (useGrayBoxPlaceholder)
|
|
|
|
const SizedBox.square(
|
|
|
|
dimension: 250,
|
|
|
|
child: DecoratedBox(
|
|
|
|
decoration: BoxDecoration(color: Colors.grey),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
if (useProgressIndicator)
|
|
|
|
Transform.scale(
|
|
|
|
scale: 2,
|
|
|
|
child: Center(
|
|
|
|
child: CircularProgressIndicator.adaptive(
|
|
|
|
strokeWidth: 1,
|
|
|
|
value: downloadProgress.progress,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
2022-11-08 19:00:24 +02:00
|
|
|
);
|
|
|
|
},
|
|
|
|
errorWidget: (context, url, error) {
|
2023-06-14 16:35:32 +02:00
|
|
|
if (error is HttpExceptionWithStatus &&
|
|
|
|
error.statusCode >= 400 &&
|
|
|
|
error.statusCode < 500) {
|
|
|
|
debugPrint("Evicting thumbnail '$url' from cache: $error");
|
|
|
|
CachedNetworkImage.evictFromCache(url);
|
|
|
|
}
|
2022-11-08 19:00:24 +02:00
|
|
|
return Icon(
|
|
|
|
Icons.image_not_supported_outlined,
|
2023-11-09 18:19:53 +02:00
|
|
|
color: context.primaryColor,
|
2022-11-08 19:00:24 +02:00
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2023-07-22 21:51:25 +02:00
|
|
|
|
2023-09-18 05:57:05 +02:00
|
|
|
static AssetEntityImageProvider localThumbnailProvider(Asset asset) =>
|
|
|
|
AssetEntityImageProvider(
|
|
|
|
asset.local!,
|
|
|
|
isOriginal: false,
|
|
|
|
thumbnailSize: const ThumbnailSize.square(250),
|
|
|
|
);
|
|
|
|
|
|
|
|
static CachedNetworkImageProvider remoteThumbnailProvider(
|
|
|
|
Asset asset,
|
|
|
|
api.ThumbnailFormat type,
|
|
|
|
Map<String, String> authHeader,
|
|
|
|
) =>
|
|
|
|
CachedNetworkImageProvider(
|
|
|
|
getThumbnailUrl(asset, type: type),
|
|
|
|
cacheKey: getThumbnailCacheKey(asset, type: type),
|
|
|
|
headers: authHeader,
|
|
|
|
);
|
|
|
|
|
2024-01-05 07:20:55 +02:00
|
|
|
/// TODO: refactor image providers to separate class
|
|
|
|
static CachedNetworkImageProvider remoteThumbnailProviderForId(
|
|
|
|
String assetId, {
|
|
|
|
api.ThumbnailFormat type = api.ThumbnailFormat.WEBP,
|
|
|
|
}) =>
|
|
|
|
CachedNetworkImageProvider(
|
|
|
|
getThumbnailUrlForRemoteId(assetId, type: type),
|
|
|
|
cacheKey: getThumbnailCacheKeyForRemoteId(assetId, type: type),
|
|
|
|
headers: {
|
|
|
|
"Authorization": 'Bearer ${Store.get(StoreKey.accessToken)}',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2023-07-22 21:51:25 +02:00
|
|
|
/// Precaches this asset for instant load the next time it is shown
|
|
|
|
static Future<void> precacheAsset(
|
|
|
|
Asset asset,
|
|
|
|
BuildContext context, {
|
|
|
|
type = api.ThumbnailFormat.WEBP,
|
|
|
|
}) {
|
2023-09-18 05:57:05 +02:00
|
|
|
if (useLocal(asset)) {
|
|
|
|
// Precache the local image
|
|
|
|
return precacheImage(localThumbnailProvider(asset), context);
|
2023-07-22 21:51:25 +02:00
|
|
|
} else {
|
2023-09-18 05:57:05 +02:00
|
|
|
final authToken = 'Bearer ${Store.get(StoreKey.accessToken)}';
|
2023-07-22 21:51:25 +02:00
|
|
|
// Precache the remote image since we are not using local images
|
2023-09-18 05:57:05 +02:00
|
|
|
return precacheImage(
|
|
|
|
remoteThumbnailProvider(asset, type, {"Authorization": authToken}),
|
|
|
|
context,
|
2023-07-22 21:51:25 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-09-18 05:57:05 +02:00
|
|
|
|
|
|
|
static bool useLocal(Asset asset) =>
|
|
|
|
!asset.isRemote ||
|
|
|
|
asset.isLocal && !Store.get(StoreKey.preferRemoteImage, false);
|
2022-11-08 19:00:24 +02:00
|
|
|
}
|