Skip to content

Commit

Permalink
Merge pull request #398 from p-mazhnik/pavel/fix-config
Browse files Browse the repository at this point in the history
fix loading config from pubspec.yaml
  • Loading branch information
RatakondalaArun authored Aug 4, 2022
2 parents 4ffd0f5 + e2a077a commit 42fb273
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 230 deletions.
57 changes: 23 additions & 34 deletions lib/android.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'dart:io';
import 'package:flutter_launcher_icons/constants.dart';
import 'package:flutter_launcher_icons/flutter_launcher_icons_config.dart';
import 'package:flutter_launcher_icons/utils.dart';
import 'package:flutter_launcher_icons/xml_templates.dart' as xml_template;
import 'package:image/image.dart';
Expand Down Expand Up @@ -29,17 +31,21 @@ List<AndroidIconTemplate> androidIcons = <AndroidIconTemplate>[
AndroidIconTemplate(directoryName: 'mipmap-xxxhdpi', size: 192),
];

void createDefaultIcons(Map<String, dynamic> flutterLauncherIconsConfig, String? flavor) {
void createDefaultIcons(FlutterLauncherIconsConfig flutterLauncherIconsConfig, String? flavor) {
printStatus('Creating default icons Android');
final String filePath = getAndroidIconPath(flutterLauncherIconsConfig);
// todo: support prefixPath
final String? filePath = flutterLauncherIconsConfig.getImagePathAndroid();
if (filePath == null) {
throw const InvalidConfigException(errorMissingImagePath);
}
final Image? image = decodeImageFile(filePath);
if (image == null) {
return;
}
final File androidManifestFile = File(constants.androidManifestFile);
if (isCustomAndroidFile(flutterLauncherIconsConfig)) {
if (flutterLauncherIconsConfig.isCustomAndroidFile) {
printStatus('Adding a new Android launcher icon');
final String iconName = getNewIconName(flutterLauncherIconsConfig);
final String iconName = flutterLauncherIconsConfig.android;
isAndroidIconNameCorrectFormat(iconName);
final String iconPath = '$iconName.png';
for (AndroidIconTemplate template in androidIcons) {
Expand All @@ -64,12 +70,15 @@ bool isAndroidIconNameCorrectFormat(String iconName) {
return true;
}

void createAdaptiveIcons(Map<String, dynamic> flutterLauncherIconsConfig, String? flavor) {
void createAdaptiveIcons(FlutterLauncherIconsConfig flutterLauncherIconsConfig, String? flavor) {
printStatus('Creating adaptive icons Android');

// Retrieve the necessary Flutter Launcher Icons configuration from the pubspec.yaml file
final String backgroundConfig = flutterLauncherIconsConfig['adaptive_icon_background'];
final String foregroundImagePath = flutterLauncherIconsConfig['adaptive_icon_foreground'];
final String? backgroundConfig = flutterLauncherIconsConfig.adaptiveIconBackground;
final String? foregroundImagePath = flutterLauncherIconsConfig.adaptiveIconForeground;
if (backgroundConfig == null || foregroundImagePath == null) {
throw const InvalidConfigException(errorMissingImagePath);
}
final Image? foregroundImage = decodeImageFile(foregroundImagePath);
if (foregroundImage == null) {
return;
Expand Down Expand Up @@ -110,9 +119,9 @@ void updateColorsXmlFile(String backgroundConfig, String? flavor) {

/// Creates the xml file required for the adaptive launcher icon
/// FILE LOCATED HERE: res/mipmap-anydpi/{icon-name-from-yaml-config}.xml
void createAdaptiveIconMipmapXmlFile(Map<String, dynamic> flutterLauncherIconsConfig, String? flavor) {
if (isCustomAndroidFile(flutterLauncherIconsConfig)) {
File(constants.androidAdaptiveXmlFolder(flavor) + getNewIconName(flutterLauncherIconsConfig) + '.xml')
void createAdaptiveIconMipmapXmlFile(FlutterLauncherIconsConfig flutterLauncherIconsConfig, String? flavor) {
if (flutterLauncherIconsConfig.isCustomAndroidFile) {
File(constants.androidAdaptiveXmlFolder(flavor) + flutterLauncherIconsConfig.android + '.xml')
.create(recursive: true)
.then((File adaptiveIcon) {
adaptiveIcon.writeAsString(xml_template.icLauncherXml);
Expand All @@ -128,7 +137,7 @@ void createAdaptiveIconMipmapXmlFile(Map<String, dynamic> flutterLauncherIconsCo

/// creates adaptive background using png image
void _createAdaptiveBackgrounds(
Map<String, dynamic> yamlConfig,
FlutterLauncherIconsConfig flutterLauncherIconsConfig,
String adaptiveIconBackgroundImagePath,
String? flavor,
) {
Expand All @@ -146,8 +155,8 @@ void _createAdaptiveBackgrounds(

// Creates the xml file required for the adaptive launcher icon
// FILE LOCATED HERE: res/mipmap-anydpi/{icon-name-from-yaml-config}.xml
if (isCustomAndroidFile(yamlConfig)) {
File(constants.androidAdaptiveXmlFolder(flavor) + getNewIconName(yamlConfig) + '.xml')
if (flutterLauncherIconsConfig.isCustomAndroidFile) {
File(constants.androidAdaptiveXmlFolder(flavor) + flutterLauncherIconsConfig.android + '.xml')
.create(recursive: true)
.then((File adaptiveIcon) {
adaptiveIcon.writeAsString(xml_template.icLauncherDrawableBackgroundXml);
Expand Down Expand Up @@ -194,19 +203,6 @@ void updateColorsFile(File colorsFile, String backgroundColor) {
colorsFile.writeAsStringSync(lines.join('\n'));
}

/// Check to see if specified Android config is a string or bool
/// String - Generate new launcher icon with the string specified
/// bool - override the default flutter project icon
bool isCustomAndroidFile(Map<String, dynamic> config) {
final dynamic androidConfig = config['android'];
return androidConfig is String;
}

/// return the new launcher icon file name
String getNewIconName(Map<String, dynamic> config) {
return config['android'];
}

/// Overrides the existing launcher icons in the project
/// Note: Do not change interpolation unless you end up with better results (see issue for result when using cubic
/// interpolation)
Expand Down Expand Up @@ -271,7 +267,7 @@ List<String> _transformAndroidManifestWithNewLauncherIcon(List<String> oldManife
/// - build.gradle: `'android/app/build.gradle'`
/// - local.properties: `'android/local.properties'`
///
/// If found none returns 0
/// If found none returns [constants.androidDefaultAndroidMinSDK]
int minSdk() {
final androidGradleFile = File(constants.androidGradleFile);
final androidLocalPropertiesFile = File(constants.androidLocalPropertiesFile);
Expand Down Expand Up @@ -346,13 +342,6 @@ int? _getMinSdkFlutterGradle(File localPropertiesFile) {
return null;
}

/// Method for the retrieval of the Android icon path
/// If image_path_android is found, this will be prioritised over the image_path
/// value.
String getAndroidIconPath(Map<String, dynamic> config) {
return config['image_path_android'] ?? config['image_path'];
}

/// Returns true if the adaptive icon configuration is a PNG image
bool isAdaptiveIconConfigPngFile(String backgroundFile) {
return backgroundFile.endsWith('.png');
Expand Down
32 changes: 30 additions & 2 deletions lib/flutter_launcher_icons_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class FlutterLauncherIconsConfig {
final int minSdkAndroid;

/// IOS remove_alpha_ios
@JsonKey(name: 'remove_alpha_ios', defaultValue: true)
@JsonKey(name: 'remove_alpha_ios')
final bool removeAlphaIOS;

/// Web platform config
Expand All @@ -68,7 +68,7 @@ class FlutterLauncherIconsConfig {
this.adaptiveIconForeground,
this.adaptiveIconBackground,
this.minSdkAndroid = constants.androidDefaultAndroidMinSDK,
this.removeAlphaIOS = true,
this.removeAlphaIOS = false,
this.webConfig,
this.windowsConfig,
});
Expand Down Expand Up @@ -136,6 +136,34 @@ class FlutterLauncherIconsConfig {

@override
String toString() => 'FlutterLauncherIconsConfig: ${toJson()}';

bool get isNeedingNewIOSIcon => ios != false;
bool get isNeedingNewAndroidIcon => android != false;
bool get hasAndroidAdaptiveConfig =>
isNeedingNewAndroidIcon &&
adaptiveIconForeground != null &&
adaptiveIconBackground != null;

// todo: refactor after Android & iOS configs will be refactored to the new schema
// https://github.com/fluttercommunity/flutter_launcher_icons/issues/394
String? getImagePathIOS() => imagePathIOS ?? imagePath;

/// Method for the retrieval of the Android icon path
/// If image_path_android is found, this will be prioritised over the image_path
/// value.
String? getImagePathAndroid() => imagePathAndroid ?? imagePath;

/// Check to see if specified Android config is a string or bool
/// String - Generate new launcher icon with the string specified
/// bool - override the default flutter project icon
bool get isCustomAndroidFile => android is String;

/// Checks if contains any platform config
bool get hasPlatformConfig =>
ios != false ||
android != false ||
webConfig != null ||
windowsConfig != null;
}

/// Parse `web` config from `flutter_launcher_icons.yaml`
Expand Down
2 changes: 1 addition & 1 deletion lib/flutter_launcher_icons_config.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions lib/ios.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:convert';
import 'dart:io';
import 'package:flutter_launcher_icons/custom_exceptions.dart';
import 'package:flutter_launcher_icons/flutter_launcher_icons_config.dart';
import 'package:flutter_launcher_icons/utils.dart';
import 'package:image/image.dart';
import 'package:flutter_launcher_icons/constants.dart';
Expand Down Expand Up @@ -36,22 +38,26 @@ List<IosIconTemplate> iosIcons = <IosIconTemplate>[
IosIconTemplate(name: '-1024x1024@1x', size: 1024),
];

void createIcons(Map<String, dynamic> config, String? flavor) {
final String filePath = config['image_path_ios'] ?? config['image_path'];
void createIcons(FlutterLauncherIconsConfig config, String? flavor) {
// todo: support prefixPath
final String? filePath = config.getImagePathIOS();
if (filePath == null) {
throw const InvalidConfigException(errorMissingImagePath);
}
// decodeImageFile shows error message if null
// so can return here if image is null
final Image? image = decodeImage(File(filePath).readAsBytesSync());
if (image == null) {
return;
}
if (config['remove_alpha_ios'] is bool && config['remove_alpha_ios']) {
if (config.removeAlphaIOS) {
image.channels = Channels.rgb;
}
if (image.channels == Channels.rgba) {
print('\nWARNING: Icons with alpha channel are not allowed in the Apple App Store.\nSet "remove_alpha_ios: true" to remove it.\n');
}
String iconName;
final dynamic iosConfig = config['ios'];
final dynamic iosConfig = config.ios;
if (flavor != null) {
final String catalogName = 'AppIcon-$flavor';
printStatus('Building iOS launcher icon for $flavor');
Expand Down
Loading

0 comments on commit 42fb273

Please sign in to comment.