From 8e6ac397a7605ff3942c21cfff0ce0c50be1180e Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Tue, 25 Jul 2023 22:36:41 +0800 Subject: [PATCH] update getting album thumbnail and album thumbnail provider --- lib/photo_gallery.dart | 5 +++-- .../album_thumbnail_provider.dart | 22 +++++++++---------- lib/src/models/album.dart | 2 +- test/photo_gallery_test.dart | 2 +- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/photo_gallery.dart b/lib/photo_gallery.dart index 0628ea7..b9590d6 100644 --- a/lib/photo_gallery.dart +++ b/lib/photo_gallery.dart @@ -84,7 +84,7 @@ class PhotoGallery { } /// Get album thumbnail by album id - static Future?> getAlbumThumbnail({ + static Future> getAlbumThumbnail({ required String albumId, MediumType? mediumType, bool newest = true, @@ -100,7 +100,8 @@ class PhotoGallery { 'height': height, 'highQuality': highQuality, }); - return bytes != null ? new List.from(bytes) : null; + if (bytes == null) throw "Failed to fetch thumbnail of album $albumId"; + return new List.from(bytes); } /// 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 2744112..5647d28 100644 --- a/lib/src/image_providers/album_thumbnail_provider.dart +++ b/lib/src/image_providers/album_thumbnail_provider.dart @@ -27,18 +27,18 @@ class AlbumThumbnailProvider extends ImageProvider { Future _loadAsync(AlbumThumbnailProvider key, ImageDecoderCallback decode) async { assert(key == this); - final data = await PhotoGallery.getAlbumThumbnail( - albumId: album.id, - mediumType: album.mediumType, - newest: album.newest, - height: height, - width: width, - highQuality: highQuality, - ); - ui.ImmutableBuffer buffer; - if (data != null) { + 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)); - } else { + } catch (e) { buffer = await ui.ImmutableBuffer.fromAsset("packages/photo_gallery/images/grey.bmp"); } return decode(buffer); diff --git a/lib/src/models/album.dart b/lib/src/models/album.dart index 8248a48..5bf22dc 100644 --- a/lib/src/models/album.dart +++ b/lib/src/models/album.dart @@ -49,7 +49,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 d31329f..312effd 100644 --- a/test/photo_gallery_test.dart +++ b/test/photo_gallery_test.dart @@ -74,7 +74,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); });