fetchAlbums method

void fetchAlbums(
  1. RequestType type,
  2. FilterOptionGroup? filterOptionGroup
)

Implementation

void fetchAlbums(
    RequestType type, FilterOptionGroup? filterOptionGroup) async {
  albumsNotifier.value = const BaseStateGallery(isLoading: true);
  albumNotifier.value = const BaseStateGallery(isLoading: true);
  entitiesNotifier.value = const BaseStateGallery(isLoading: true);

  try {
    final albums = await PhotoManager.getAssetPathList(
        type: type, filterOption: filterOptionGroup);

    albumsNotifier.value =
        BaseStateGallery(data: albums, hasPermission: true);

    final album = albums.isNotEmpty
        ? albums[albums.indexWhere((element) => element.isAll)]
        : null;

    albumNotifier.value = BaseStateGallery(data: album, hasPermission: true);
    final assetCount = await album?.assetCountAsync ?? 0;

    List<AssetEntity> entities = [];

    if (assetCount > 0) {
      entities = await album?.getAssetListRange(start: 0, end: assetCount) ??
          <AssetEntity>[];
    }

    entitiesNotifier.value =
        BaseStateGallery(data: entities, hasPermission: true);
  } catch (e) {
    albumsNotifier.value = BaseStateGallery(
      hasPermission: false,
      hasError: true,
      error: e.toString(),
    );
    entitiesNotifier.value = BaseStateGallery(
      hasPermission: false,
      hasError: true,
      error: e.toString(),
    );
  }
}