collectGallery static method

Future<GalleryMedia?> collectGallery({
  1. Locale? locale,
})

Implementation

static Future<GalleryMedia?> collectGallery({Locale? locale}) async {
  if (await promptPermissionSetting()) {
    List<GalleryAlbum> tempGalleryAlbums = [];

    List<Album> photoAlbums =
        await PhotoGallery.listAlbums(mediumType: MediumType.image);
    List<Album> videoAlbums =
        await PhotoGallery.listAlbums(mediumType: MediumType.video);
    for (var photoAlbum in photoAlbums) {
      GalleryAlbum entireGalleryAlbum = GalleryAlbum.album(photoAlbum);
      await entireGalleryAlbum.initialize(locale: locale);
      entireGalleryAlbum.setType = AlbumType.image;
      if (videoAlbums.any((element) => element.id == photoAlbum.id)) {
        Album videoAlbum =
            videoAlbums.singleWhere((element) => element.id == photoAlbum.id);
        GalleryAlbum videoGalleryAlbum = GalleryAlbum.album(videoAlbum);
        await videoGalleryAlbum.initialize(locale: locale);
        DateTime? lastPhotoDate = entireGalleryAlbum.lastDate;
        DateTime? lastVideoDate = videoGalleryAlbum.lastDate;

        if (lastPhotoDate == null) {
          try {
            entireGalleryAlbum.thumbnail =
                await videoAlbum.getThumbnail(highQuality: true);
          } catch (e) {
            if (kDebugMode) {
              print(e);
            }
          }
        } else if (lastVideoDate == null) {
        } else {
          if (lastVideoDate.isAfter(lastPhotoDate)) {
            try {
              entireGalleryAlbum.thumbnail =
                  await videoAlbum.getThumbnail(highQuality: true);
            } catch (e) {
              entireGalleryAlbum.thumbnail = null;
              if (kDebugMode) {
                print(e);
              }
            }
          }
        }
        for (var file in videoGalleryAlbum.files) {
          entireGalleryAlbum.addFile(file, locale: locale);
        }
        entireGalleryAlbum.sort();
        entireGalleryAlbum.setType = AlbumType.mixed;
        videoAlbums.remove(videoAlbum);
      }
      tempGalleryAlbums.add(entireGalleryAlbum);
    }
    for (var videoAlbum in videoAlbums) {
      GalleryAlbum galleryVideoAlbum = GalleryAlbum.album(videoAlbum);
      await galleryVideoAlbum.initialize(locale: locale);
      galleryVideoAlbum.setType = AlbumType.video;
      tempGalleryAlbums.add(galleryVideoAlbum);
    }

    return GalleryMedia(tempGalleryAlbums);
  } else {
    return null;
  }
}