2024-02-13 23:30:32 +02:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:ui' as ui;
|
|
|
|
|
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
2024-03-14 22:29:09 +02:00
|
|
|
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
2024-05-02 22:59:14 +02:00
|
|
|
import 'package:immich_mobile/providers/image/cache/image_loader.dart';
|
|
|
|
import 'package:immich_mobile/providers/image/cache/thumbnail_image_cache_manager.dart';
|
2024-02-13 23:30:32 +02:00
|
|
|
import 'package:openapi/api.dart' as api;
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/painting.dart';
|
2024-05-01 04:36:40 +02:00
|
|
|
import 'package:immich_mobile/entities/asset.entity.dart';
|
2024-02-13 23:30:32 +02:00
|
|
|
import 'package:immich_mobile/utils/image_url_builder.dart';
|
|
|
|
|
|
|
|
/// The remote image provider
|
2024-02-27 17:51:19 +02:00
|
|
|
class ImmichRemoteThumbnailProvider
|
|
|
|
extends ImageProvider<ImmichRemoteThumbnailProvider> {
|
2024-02-13 23:30:32 +02:00
|
|
|
/// The [Asset.remoteId] of the asset to fetch
|
|
|
|
final String assetId;
|
|
|
|
|
2024-03-14 22:29:09 +02:00
|
|
|
final int? height;
|
|
|
|
final int? width;
|
|
|
|
|
|
|
|
/// The image cache manager
|
2024-04-25 06:30:32 +02:00
|
|
|
final CacheManager? cacheManager;
|
2024-03-14 22:29:09 +02:00
|
|
|
|
2024-02-13 23:30:32 +02:00
|
|
|
ImmichRemoteThumbnailProvider({
|
|
|
|
required this.assetId,
|
2024-03-14 22:29:09 +02:00
|
|
|
this.height,
|
|
|
|
this.width,
|
|
|
|
this.cacheManager,
|
2024-02-13 23:30:32 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
/// Converts an [ImageProvider]'s settings plus an [ImageConfiguration] to a key
|
|
|
|
/// that describes the precise image to load.
|
|
|
|
@override
|
2024-02-27 17:51:19 +02:00
|
|
|
Future<ImmichRemoteThumbnailProvider> obtainKey(
|
|
|
|
ImageConfiguration configuration,
|
|
|
|
) {
|
|
|
|
return SynchronousFuture(this);
|
2024-02-13 23:30:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2024-02-27 17:51:19 +02:00
|
|
|
ImageStreamCompleter loadImage(
|
|
|
|
ImmichRemoteThumbnailProvider key,
|
|
|
|
ImageDecoderCallback decode,
|
|
|
|
) {
|
2024-03-14 22:29:09 +02:00
|
|
|
final cache = cacheManager ?? ThumbnailImageCacheManager();
|
2024-02-13 23:30:32 +02:00
|
|
|
return MultiImageStreamCompleter(
|
2024-03-14 22:29:09 +02:00
|
|
|
codec: _codec(key, cache, decode),
|
2024-02-13 23:30:32 +02:00
|
|
|
scale: 1.0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Streams in each stage of the image as we ask for it
|
|
|
|
Stream<ui.Codec> _codec(
|
2024-02-27 17:51:19 +02:00
|
|
|
ImmichRemoteThumbnailProvider key,
|
2024-04-25 06:30:32 +02:00
|
|
|
CacheManager cache,
|
2024-02-13 23:30:32 +02:00
|
|
|
ImageDecoderCallback decode,
|
|
|
|
) async* {
|
|
|
|
// Load a preview to the chunk events
|
|
|
|
final preview = getThumbnailUrlForRemoteId(
|
2024-02-27 17:51:19 +02:00
|
|
|
key.assetId,
|
2024-02-13 23:30:32 +02:00
|
|
|
type: api.ThumbnailFormat.WEBP,
|
|
|
|
);
|
|
|
|
|
2024-03-14 22:29:09 +02:00
|
|
|
yield await ImageLoader.loadImageFromCache(
|
|
|
|
preview,
|
|
|
|
cache: cache,
|
|
|
|
decode: decode,
|
2024-02-13 23:30:32 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool operator ==(Object other) {
|
|
|
|
if (identical(this, other)) return true;
|
2024-03-14 22:29:09 +02:00
|
|
|
if (other is ImmichRemoteThumbnailProvider) {
|
|
|
|
return assetId == other.assetId;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2024-02-13 23:30:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
int get hashCode => assetId.hashCode;
|
|
|
|
}
|