Skip to content

Commit 42fb273

Browse files
Merge pull request #398 from p-mazhnik/pavel/fix-config
fix loading config from pubspec.yaml
2 parents 4ffd0f5 + e2a077a commit 42fb273

10 files changed

+244
-230
lines changed

lib/android.dart

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import 'dart:io';
2+
import 'package:flutter_launcher_icons/constants.dart';
3+
import 'package:flutter_launcher_icons/flutter_launcher_icons_config.dart';
24
import 'package:flutter_launcher_icons/utils.dart';
35
import 'package:flutter_launcher_icons/xml_templates.dart' as xml_template;
46
import 'package:image/image.dart';
@@ -29,17 +31,21 @@ List<AndroidIconTemplate> androidIcons = <AndroidIconTemplate>[
2931
AndroidIconTemplate(directoryName: 'mipmap-xxxhdpi', size: 192),
3032
];
3133

32-
void createDefaultIcons(Map<String, dynamic> flutterLauncherIconsConfig, String? flavor) {
34+
void createDefaultIcons(FlutterLauncherIconsConfig flutterLauncherIconsConfig, String? flavor) {
3335
printStatus('Creating default icons Android');
34-
final String filePath = getAndroidIconPath(flutterLauncherIconsConfig);
36+
// todo: support prefixPath
37+
final String? filePath = flutterLauncherIconsConfig.getImagePathAndroid();
38+
if (filePath == null) {
39+
throw const InvalidConfigException(errorMissingImagePath);
40+
}
3541
final Image? image = decodeImageFile(filePath);
3642
if (image == null) {
3743
return;
3844
}
3945
final File androidManifestFile = File(constants.androidManifestFile);
40-
if (isCustomAndroidFile(flutterLauncherIconsConfig)) {
46+
if (flutterLauncherIconsConfig.isCustomAndroidFile) {
4147
printStatus('Adding a new Android launcher icon');
42-
final String iconName = getNewIconName(flutterLauncherIconsConfig);
48+
final String iconName = flutterLauncherIconsConfig.android;
4349
isAndroidIconNameCorrectFormat(iconName);
4450
final String iconPath = '$iconName.png';
4551
for (AndroidIconTemplate template in androidIcons) {
@@ -64,12 +70,15 @@ bool isAndroidIconNameCorrectFormat(String iconName) {
6470
return true;
6571
}
6672

67-
void createAdaptiveIcons(Map<String, dynamic> flutterLauncherIconsConfig, String? flavor) {
73+
void createAdaptiveIcons(FlutterLauncherIconsConfig flutterLauncherIconsConfig, String? flavor) {
6874
printStatus('Creating adaptive icons Android');
6975

7076
// Retrieve the necessary Flutter Launcher Icons configuration from the pubspec.yaml file
71-
final String backgroundConfig = flutterLauncherIconsConfig['adaptive_icon_background'];
72-
final String foregroundImagePath = flutterLauncherIconsConfig['adaptive_icon_foreground'];
77+
final String? backgroundConfig = flutterLauncherIconsConfig.adaptiveIconBackground;
78+
final String? foregroundImagePath = flutterLauncherIconsConfig.adaptiveIconForeground;
79+
if (backgroundConfig == null || foregroundImagePath == null) {
80+
throw const InvalidConfigException(errorMissingImagePath);
81+
}
7382
final Image? foregroundImage = decodeImageFile(foregroundImagePath);
7483
if (foregroundImage == null) {
7584
return;
@@ -110,9 +119,9 @@ void updateColorsXmlFile(String backgroundConfig, String? flavor) {
110119

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

129138
/// creates adaptive background using png image
130139
void _createAdaptiveBackgrounds(
131-
Map<String, dynamic> yamlConfig,
140+
FlutterLauncherIconsConfig flutterLauncherIconsConfig,
132141
String adaptiveIconBackgroundImagePath,
133142
String? flavor,
134143
) {
@@ -146,8 +155,8 @@ void _createAdaptiveBackgrounds(
146155

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

197-
/// Check to see if specified Android config is a string or bool
198-
/// String - Generate new launcher icon with the string specified
199-
/// bool - override the default flutter project icon
200-
bool isCustomAndroidFile(Map<String, dynamic> config) {
201-
final dynamic androidConfig = config['android'];
202-
return androidConfig is String;
203-
}
204-
205-
/// return the new launcher icon file name
206-
String getNewIconName(Map<String, dynamic> config) {
207-
return config['android'];
208-
}
209-
210206
/// Overrides the existing launcher icons in the project
211207
/// Note: Do not change interpolation unless you end up with better results (see issue for result when using cubic
212208
/// interpolation)
@@ -271,7 +267,7 @@ List<String> _transformAndroidManifestWithNewLauncherIcon(List<String> oldManife
271267
/// - build.gradle: `'android/app/build.gradle'`
272268
/// - local.properties: `'android/local.properties'`
273269
///
274-
/// If found none returns 0
270+
/// If found none returns [constants.androidDefaultAndroidMinSDK]
275271
int minSdk() {
276272
final androidGradleFile = File(constants.androidGradleFile);
277273
final androidLocalPropertiesFile = File(constants.androidLocalPropertiesFile);
@@ -346,13 +342,6 @@ int? _getMinSdkFlutterGradle(File localPropertiesFile) {
346342
return null;
347343
}
348344

349-
/// Method for the retrieval of the Android icon path
350-
/// If image_path_android is found, this will be prioritised over the image_path
351-
/// value.
352-
String getAndroidIconPath(Map<String, dynamic> config) {
353-
return config['image_path_android'] ?? config['image_path'];
354-
}
355-
356345
/// Returns true if the adaptive icon configuration is a PNG image
357346
bool isAdaptiveIconConfigPngFile(String backgroundFile) {
358347
return backgroundFile.endsWith('.png');

lib/flutter_launcher_icons_config.dart

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class FlutterLauncherIconsConfig {
4747
final int minSdkAndroid;
4848

4949
/// IOS remove_alpha_ios
50-
@JsonKey(name: 'remove_alpha_ios', defaultValue: true)
50+
@JsonKey(name: 'remove_alpha_ios')
5151
final bool removeAlphaIOS;
5252

5353
/// Web platform config
@@ -68,7 +68,7 @@ class FlutterLauncherIconsConfig {
6868
this.adaptiveIconForeground,
6969
this.adaptiveIconBackground,
7070
this.minSdkAndroid = constants.androidDefaultAndroidMinSDK,
71-
this.removeAlphaIOS = true,
71+
this.removeAlphaIOS = false,
7272
this.webConfig,
7373
this.windowsConfig,
7474
});
@@ -136,6 +136,34 @@ class FlutterLauncherIconsConfig {
136136

137137
@override
138138
String toString() => 'FlutterLauncherIconsConfig: ${toJson()}';
139+
140+
bool get isNeedingNewIOSIcon => ios != false;
141+
bool get isNeedingNewAndroidIcon => android != false;
142+
bool get hasAndroidAdaptiveConfig =>
143+
isNeedingNewAndroidIcon &&
144+
adaptiveIconForeground != null &&
145+
adaptiveIconBackground != null;
146+
147+
// todo: refactor after Android & iOS configs will be refactored to the new schema
148+
// https://github.com/fluttercommunity/flutter_launcher_icons/issues/394
149+
String? getImagePathIOS() => imagePathIOS ?? imagePath;
150+
151+
/// Method for the retrieval of the Android icon path
152+
/// If image_path_android is found, this will be prioritised over the image_path
153+
/// value.
154+
String? getImagePathAndroid() => imagePathAndroid ?? imagePath;
155+
156+
/// Check to see if specified Android config is a string or bool
157+
/// String - Generate new launcher icon with the string specified
158+
/// bool - override the default flutter project icon
159+
bool get isCustomAndroidFile => android is String;
160+
161+
/// Checks if contains any platform config
162+
bool get hasPlatformConfig =>
163+
ios != false ||
164+
android != false ||
165+
webConfig != null ||
166+
windowsConfig != null;
139167
}
140168

141169
/// Parse `web` config from `flutter_launcher_icons.yaml`

lib/flutter_launcher_icons_config.g.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ios.dart

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import 'dart:convert';
22
import 'dart:io';
3+
import 'package:flutter_launcher_icons/custom_exceptions.dart';
4+
import 'package:flutter_launcher_icons/flutter_launcher_icons_config.dart';
35
import 'package:flutter_launcher_icons/utils.dart';
46
import 'package:image/image.dart';
57
import 'package:flutter_launcher_icons/constants.dart';
@@ -36,22 +38,26 @@ List<IosIconTemplate> iosIcons = <IosIconTemplate>[
3638
IosIconTemplate(name: '-1024x1024@1x', size: 1024),
3739
];
3840

39-
void createIcons(Map<String, dynamic> config, String? flavor) {
40-
final String filePath = config['image_path_ios'] ?? config['image_path'];
41+
void createIcons(FlutterLauncherIconsConfig config, String? flavor) {
42+
// todo: support prefixPath
43+
final String? filePath = config.getImagePathIOS();
44+
if (filePath == null) {
45+
throw const InvalidConfigException(errorMissingImagePath);
46+
}
4147
// decodeImageFile shows error message if null
4248
// so can return here if image is null
4349
final Image? image = decodeImage(File(filePath).readAsBytesSync());
4450
if (image == null) {
4551
return;
4652
}
47-
if (config['remove_alpha_ios'] is bool && config['remove_alpha_ios']) {
53+
if (config.removeAlphaIOS) {
4854
image.channels = Channels.rgb;
4955
}
5056
if (image.channels == Channels.rgba) {
5157
print('\nWARNING: Icons with alpha channel are not allowed in the Apple App Store.\nSet "remove_alpha_ios: true" to remove it.\n');
5258
}
5359
String iconName;
54-
final dynamic iosConfig = config['ios'];
60+
final dynamic iosConfig = config.ios;
5561
if (flavor != null) {
5662
final String catalogName = 'AppIcon-$flavor';
5763
printStatus('Building iOS launcher icon for $flavor');

0 commit comments

Comments
 (0)