generateIosImages function

Future<void> generateIosImages({
  1. String? imageSource,
  2. String? color,
  3. String? iosContentMode,
  4. String? backgroundImage,
  5. String? iosBackgroundContentMode,
  6. String? darkImageSource,
  7. String? darkColor,
  8. String? darkBackgroundImage,
})

Generate splash images for the iOS

Implementation

Future<void> generateIosImages({
  String? imageSource,
  String? color,
  String? iosContentMode,
  String? backgroundImage,
  String? iosBackgroundContentMode,
  String? darkImageSource,
  String? darkColor,
  String? darkBackgroundImage,
}) async {
  if (darkImageSource != null && imageSource == null) {
    throw SplashMasterException(
      message:
          'For iOS, image is required when image_dark is provided. Add image to provide the base Any appearance asset.',
    );
  }

  if (darkBackgroundImage != null && backgroundImage == null) {
    throw SplashMasterException(
      message:
          'For iOS, background_image is required when background_image_dark is provided. Add background_image to provide the base Any appearance asset.',
    );
  }

  const iosAssetsFolder = CmdStrings.iosAssetsDirectory;

  final directory = Directory(iosAssetsFolder);
  if (!await directory.exists()) {
    log("$iosAssetsFolder path doesn't exists. Creating it...");
    await directory.create(recursive: true);
  }

  await _removeLegacyLaunchImageFiles(iosAssetsFolder);
  await _cleanupGeneratedLaunchImageFiles(
    iosAssetsFolder,
    keepLightAssets: imageSource != null,
    keepDarkAssets: darkImageSource != null,
  );

  final List<Image> images = [];

  if (imageSource != null) {
    final sourceImage = File(imageSource);
    if (!await sourceImage.exists()) {
      throw SplashMasterException(message: 'Asset not found. $imageSource');
    }

    for (final scale in IosScale.values) {
      final fileName = '${IOSStrings.splashImage}${scale.fileEndWith}.png';
      final imagePath = '$iosAssetsFolder/$fileName';

      /// Creating a splash image from the provided asset source
      await _replaceFileFromSource(
        source: sourceImage,
        destinationPath: imagePath,
      );

      log('Generated $fileName.');
      images.add(Image(
        idiom: IOSStrings.iOSContentJsonIdiom,
        filename: fileName,
        scale: scale.scale,
      ));
    }
  }

  if (darkImageSource != null) {
    final darkSourceImage = File(darkImageSource);
    if (!await darkSourceImage.exists()) {
      throw SplashMasterException(message: 'Asset not found. $darkImageSource');
    }

    for (final scale in IosScale.values) {
      final fileName = '${IOSStrings.splashImageDark}${scale.fileEndWith}.png';
      final imagePath = '$iosAssetsFolder/$fileName';

      await _replaceFileFromSource(
        source: darkSourceImage,
        destinationPath: imagePath,
      );

      log('Generated $fileName.');
      images.add(Image(
        idiom: IOSStrings.iOSContentJsonIdiom,
        filename: fileName,
        scale: scale.scale,
        appearances: [
          {
            IOSStrings.appearanceKey: IOSStrings.appearanceLuminosity,
            IOSStrings.appearanceValueKey: IOSStrings.appearanceDark,
          }
        ],
      ));
    }
  }

  await updateContentOfStoryboard(
    imagePath: imageSource,
    color: color,
    iosContentMode: iosContentMode,
    backgroundImage: backgroundImage,
    iosBackgroundContentMode: iosBackgroundContentMode,
    darkColor: darkColor,
    darkBackgroundImage: darkBackgroundImage,
  );

  await updateContentJson(images);
}