From 6a68b22df0966d7513913bd16f8e28e73ba6ccd2 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Wed, 3 May 2023 18:55:29 +0800 Subject: [PATCH] update getting album thumbnail service and AlbumThumbnailProvider to show all grey image when album is empty --- lib/photo_gallery.dart | 5 ++--- .../image_providers/album_thumbnail_provider.dart | 15 ++++++++++----- lib/src/models/album.dart | 2 +- test/photo_gallery_test.dart | 2 +- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/photo_gallery.dart b/lib/photo_gallery.dart index cda25cb..8b5096e 100644 --- a/lib/photo_gallery.dart +++ b/lib/photo_gallery.dart @@ -85,7 +85,7 @@ class PhotoGallery { } /// Get album thumbnail by album id - static Future> getAlbumThumbnail({ + static Future?> getAlbumThumbnail({ required String albumId, MediumType? mediumType, int? width, @@ -99,8 +99,7 @@ class PhotoGallery { 'height': height, 'highQuality': highQuality, }); - if (bytes == null) throw "Failed to fetch thumbnail of album $albumId"; - return new List.from(bytes); + return bytes != null ? new List.from(bytes) : null; } /// get medium file by medium id diff --git a/lib/src/image_providers/album_thumbnail_provider.dart b/lib/src/image_providers/album_thumbnail_provider.dart index 80867fd..60002f0 100644 --- a/lib/src/image_providers/album_thumbnail_provider.dart +++ b/lib/src/image_providers/album_thumbnail_provider.dart @@ -17,7 +17,7 @@ class AlbumThumbnailProvider extends ImageProvider { final bool? highQuality; @override - ImageStreamCompleter load(key, decode) { + ImageStreamCompleter loadBuffer(key, decode) { return MultiFrameImageStreamCompleter( codec: _loadAsync(key, decode), scale: 1.0, @@ -27,17 +27,22 @@ class AlbumThumbnailProvider extends ImageProvider { ); } - Future _loadAsync( - AlbumThumbnailProvider key, DecoderCallback decode) async { + Future _loadAsync(AlbumThumbnailProvider key, DecoderBufferCallback decode) async { assert(key == this); - final bytes = await PhotoGallery.getAlbumThumbnail( + final data = await PhotoGallery.getAlbumThumbnail( albumId: albumId, mediumType: mediumType, height: height, width: width, highQuality: highQuality, ); - return await decode(Uint8List.fromList(bytes)); + ui.ImmutableBuffer buffer; + if (data != null) { + buffer = await ui.ImmutableBuffer.fromUint8List(Uint8List.fromList(data)); + } else { + buffer = await ui.ImmutableBuffer.fromAsset("packages/photo_gallery/images/grey.bmp"); + } + return decode(buffer); } @override diff --git a/lib/src/models/album.dart b/lib/src/models/album.dart index d998987..c989133 100644 --- a/lib/src/models/album.dart +++ b/lib/src/models/album.dart @@ -46,7 +46,7 @@ class Album { /// Get thumbnail data for this album. /// /// It will display the lastly taken medium thumbnail. - Future> getThumbnail({ + Future?> getThumbnail({ int? width, int? height, bool? highQuality = false, diff --git a/test/photo_gallery_test.dart b/test/photo_gallery_test.dart index 275f4b9..62602ff 100644 --- a/test/photo_gallery_test.dart +++ b/test/photo_gallery_test.dart @@ -70,7 +70,7 @@ void main() { test('get album thumbnail', () async { String albumId = "__ALL__"; - List result = await PhotoGallery.getAlbumThumbnail(albumId: albumId); + List? result = await PhotoGallery.getAlbumThumbnail(albumId: albumId); List expected = Generator.generateMockAlbumThumbnail(albumId: albumId); expect(result, expected); });