uploadBannerIfNeeded method

Future<void> uploadBannerIfNeeded()

If the user sets a banner image, upload it to Firebase Storage. This is done when the user submits the form. If the user is editing an activity and the banner is already uploaded, this function does nothing.

Implementation

Future<void> uploadBannerIfNeeded() async {
  if (state.bannerDownloadURL == null ||
      state.bannerDownloadURL!.contains('firebasestorage.googleapis.com')) {
    return;
  }
  final fileExtension = await ref
      .read(remoteConfigServiceProvider)
      .getString('image_extension');
  final filename =
      'ActivityBanners/${state.id}${DateTime.now()}.$fileExtension';
  final file = XFile(state.bannerDownloadURL!);
  return ref
      .read(storageServiceProvider)
      .uploadFile(
        file,
        filename,
        ImageType.banner,
      )
      .then((newURL) async {
    return encodeBlurhash(file).then((newBlurhash) {
      // Update the state with the new banner URL and blurhash
      state = state.copyWith(
        bannerDownloadURL: newURL,
        blurhash: newBlurhash,
      );
    });
  });
}