photo_gallery/lib/src/image_providers/album_thumbnail_provider.dart

78 lines
1.9 KiB
Dart
Raw Normal View History

part of '../../photo_gallery.dart';
2020-08-14 18:07:27 +08:00
/// Fetches the given album thumbnail from the gallery.
class AlbumThumbnailProvider extends ImageProvider<AlbumThumbnailProvider> {
2023-07-30 00:54:28 +08:00
/// ImageProvider of album thumbnail
2020-08-14 18:07:27 +08:00
const AlbumThumbnailProvider({
required this.album,
2020-08-14 18:07:27 +08:00
this.height,
this.width,
this.highQuality = false,
2021-03-18 12:00:31 +00:00
});
2020-08-14 18:07:27 +08:00
2023-07-30 00:54:28 +08:00
/// Album info
final Album album;
2023-07-30 00:54:28 +08:00
/// Height of album thumbnail
2021-03-18 12:00:31 +00:00
final int? height;
2023-07-30 00:54:28 +08:00
/// Width of album thumbnail
2021-03-18 12:00:31 +00:00
final int? width;
2023-07-30 00:54:28 +08:00
/// Whether using high quality of album thumbnail
2021-03-18 12:00:31 +00:00
final bool? highQuality;
2020-08-14 18:07:27 +08:00
@override
ImageStreamCompleter loadImage(key, decode) {
2020-08-14 18:07:27 +08:00
return MultiFrameImageStreamCompleter(
codec: _loadAsync(key, decode),
scale: 1.0,
informationCollector: () sync* {
yield ErrorDescription('Id: ${album.id}');
2020-08-14 18:07:27 +08:00
},
);
}
2024-03-25 21:17:22 +08:00
Future<ui.Codec> _loadAsync(
AlbumThumbnailProvider key,
ImageDecoderCallback decode,
) async {
2020-08-14 18:07:27 +08:00
assert(key == this);
late ui.ImmutableBuffer buffer;
try {
final data = await PhotoGallery.getAlbumThumbnail(
albumId: album.id,
mediumType: album.mediumType,
newest: album.newest,
height: height,
width: width,
highQuality: highQuality,
);
buffer = await ui.ImmutableBuffer.fromUint8List(Uint8List.fromList(data));
} catch (e) {
2024-03-25 21:17:22 +08:00
buffer = await ui.ImmutableBuffer.fromAsset(
"packages/photo_gallery/images/grey.bmp",
);
}
return decode(buffer);
2020-08-14 18:07:27 +08:00
}
@override
Future<AlbumThumbnailProvider> obtainKey(ImageConfiguration configuration) {
return SynchronousFuture<AlbumThumbnailProvider>(this);
}
@override
bool operator ==(Object other) {
2020-08-14 18:07:27 +08:00
if (other.runtimeType != runtimeType) return false;
final typedOther = other as AlbumThumbnailProvider;
return album.id == typedOther.album.id;
2020-08-14 18:07:27 +08:00
}
@override
int get hashCode => album.id.hashCode;
2020-08-14 18:07:27 +08:00
@override
String toString() => '$runtimeType("${album.id}")';
2020-08-14 18:07:27 +08:00
}