/
ios.dart
374 lines (353 loc) · 11.9 KB
/
ios.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
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';
/// File to handle the creation of icons for iOS platform
class IosIconTemplate {
IosIconTemplate({required this.size, required this.name});
final String name;
final int size;
}
List<IosIconTemplate> iosIcons = <IosIconTemplate>[
IosIconTemplate(name: '-20x20@1x', size: 20),
IosIconTemplate(name: '-20x20@2x', size: 40),
IosIconTemplate(name: '-20x20@3x', size: 60),
IosIconTemplate(name: '-29x29@1x', size: 29),
IosIconTemplate(name: '-29x29@2x', size: 58),
IosIconTemplate(name: '-29x29@3x', size: 87),
IosIconTemplate(name: '-40x40@1x', size: 40),
IosIconTemplate(name: '-40x40@2x', size: 80),
IosIconTemplate(name: '-40x40@3x', size: 120),
IosIconTemplate(name: '-50x50@1x', size: 50),
IosIconTemplate(name: '-50x50@2x', size: 100),
IosIconTemplate(name: '-57x57@1x', size: 57),
IosIconTemplate(name: '-57x57@2x', size: 114),
IosIconTemplate(name: '-60x60@2x', size: 120),
IosIconTemplate(name: '-60x60@3x', size: 180),
IosIconTemplate(name: '-72x72@1x', size: 72),
IosIconTemplate(name: '-72x72@2x', size: 144),
IosIconTemplate(name: '-76x76@1x', size: 76),
IosIconTemplate(name: '-76x76@2x', size: 152),
IosIconTemplate(name: '-83.5x83.5@2x', size: 167),
IosIconTemplate(name: '-1024x1024@1x', size: 1024),
];
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.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;
if (flavor != null) {
final String catalogName = 'AppIcon-$flavor';
printStatus('Building iOS launcher icon for $flavor');
for (IosIconTemplate template in iosIcons) {
saveNewIcons(template, image, catalogName);
}
iconName = iosDefaultIconName;
changeIosLauncherIcon(catalogName, flavor);
modifyContentsFile(catalogName);
} else if (iosConfig is String) {
// If the IOS configuration is a string then the user has specified a new icon to be created
// and for the old icon file to be kept
final String newIconName = iosConfig;
printStatus('Adding new iOS launcher icon');
for (IosIconTemplate template in iosIcons) {
saveNewIcons(template, image, newIconName);
}
iconName = newIconName;
changeIosLauncherIcon(iconName, flavor);
modifyContentsFile(iconName);
}
// Otherwise the user wants the new icon to use the default icons name and
// update config file to use it
else {
printStatus('Overwriting default iOS launcher icon with new icon');
for (IosIconTemplate template in iosIcons) {
overwriteDefaultIcons(template, image);
}
iconName = iosDefaultIconName;
changeIosLauncherIcon('AppIcon', flavor);
}
}
/// Note: Do not change interpolation unless you end up with better results (see issue for result when using cubic
/// interpolation)
/// https://github.com/fluttercommunity/flutter_launcher_icons/issues/101#issuecomment-495528733
void overwriteDefaultIcons(IosIconTemplate template, Image image) {
final Image newFile = createResizedImage(template, image);
File(iosDefaultIconFolder + iosDefaultIconName + template.name + '.png')
..writeAsBytesSync(encodePng(newFile));
}
/// Note: Do not change interpolation unless you end up with better results (see issue for result when using cubic
/// interpolation)
/// https://github.com/fluttercommunity/flutter_launcher_icons/issues/101#issuecomment-495528733
void saveNewIcons(IosIconTemplate template, Image image, String newIconName) {
final String newIconFolder = iosAssetFolder + newIconName + '.appiconset/';
final Image newFile = createResizedImage(template, image);
File(newIconFolder + newIconName + template.name + '.png')
.create(recursive: true)
.then((File file) {
file.writeAsBytesSync(encodePng(newFile));
});
}
Image createResizedImage(IosIconTemplate template, Image image) {
if (image.width >= template.size) {
return copyResize(image,
width: template.size,
height: template.size,
interpolation: Interpolation.average);
} else {
return copyResize(image,
width: template.size,
height: template.size,
interpolation: Interpolation.linear);
}
}
Future<void> changeIosLauncherIcon(String iconName, String? flavor) async {
final File iOSConfigFile = File(iosConfigFile);
final List<String> lines = await iOSConfigFile.readAsLines();
bool onConfigurationSection = false;
String? currentConfig;
for (int x = 0; x < lines.length; x++) {
final String line = lines[x];
if (line.contains('/* Begin XCBuildConfiguration section */')) {
onConfigurationSection = true;
}
if (line.contains('/* End XCBuildConfiguration section */')) {
onConfigurationSection = false;
}
if (onConfigurationSection) {
final match = RegExp('.*/\\* (.*)\.xcconfig \\*/;').firstMatch(line);
if (match != null) {
currentConfig = match.group(1);
}
if (currentConfig != null &&
(flavor == null || currentConfig.contains('-$flavor')) &&
line.contains('ASSETCATALOG')) {
lines[x] = line.replaceAll(RegExp('\=(.*);'), '= $iconName;');
}
}
}
final String entireFile = lines.join('\n');
await iOSConfigFile.writeAsString(entireFile);
}
/// Create the Contents.json file
void modifyContentsFile(String newIconName) {
final String newIconFolder =
iosAssetFolder + newIconName + '.appiconset/Contents.json';
File(newIconFolder).create(recursive: true).then((File contentsJsonFile) {
final String contentsFileContent =
generateContentsFileAsString(newIconName);
contentsJsonFile.writeAsString(contentsFileContent);
});
}
String generateContentsFileAsString(String newIconName) {
final Map<String, dynamic> contentJson = <String, dynamic>{
'images': createImageList(newIconName),
'info': ContentsInfoObject(version: 1, author: 'xcode').toJson()
};
return json.encode(contentJson);
}
class ContentsImageObject {
ContentsImageObject({
required this.size,
required this.idiom,
required this.filename,
required this.scale,
});
final String size;
final String idiom;
final String filename;
final String scale;
Map<String, String> toJson() {
return <String, String>{
'size': size,
'idiom': idiom,
'filename': filename,
'scale': scale
};
}
}
class ContentsInfoObject {
ContentsInfoObject({required this.version, required this.author});
final int version;
final String author;
Map<String, dynamic> toJson() {
return <String, dynamic>{
'version': version,
'author': author,
};
}
}
List<Map<String, String>> createImageList(String fileNamePrefix) {
final List<Map<String, String>> imageList = <Map<String, String>>[
ContentsImageObject(
size: '20x20',
idiom: 'iphone',
filename: '$fileNamePrefix-20x20@2x.png',
scale: '2x')
.toJson(),
ContentsImageObject(
size: '20x20',
idiom: 'iphone',
filename: '$fileNamePrefix-20x20@3x.png',
scale: '3x')
.toJson(),
ContentsImageObject(
size: '29x29',
idiom: 'iphone',
filename: '$fileNamePrefix-29x29@1x.png',
scale: '1x')
.toJson(),
ContentsImageObject(
size: '29x29',
idiom: 'iphone',
filename: '$fileNamePrefix-29x29@2x.png',
scale: '2x')
.toJson(),
ContentsImageObject(
size: '29x29',
idiom: 'iphone',
filename: '$fileNamePrefix-29x29@3x.png',
scale: '3x')
.toJson(),
ContentsImageObject(
size: '40x40',
idiom: 'iphone',
filename: '$fileNamePrefix-40x40@2x.png',
scale: '2x')
.toJson(),
ContentsImageObject(
size: '40x40',
idiom: 'iphone',
filename: '$fileNamePrefix-40x40@3x.png',
scale: '3x')
.toJson(),
ContentsImageObject(
size: '50x50',
idiom: 'ipad',
filename: '$fileNamePrefix-50x50@1x.png',
scale: '1x')
.toJson(),
ContentsImageObject(
size: '50x50',
idiom: 'ipad',
filename: '$fileNamePrefix-50x50@2x.png',
scale: '2x')
.toJson(),
ContentsImageObject(
size: '57x57',
idiom: 'iphone',
filename: '$fileNamePrefix-57x57@1x.png',
scale: '1x')
.toJson(),
ContentsImageObject(
size: '57x57',
idiom: 'iphone',
filename: '$fileNamePrefix-57x57@2x.png',
scale: '2x')
.toJson(),
ContentsImageObject(
size: '60x60',
idiom: 'iphone',
filename: '$fileNamePrefix-60x60@2x.png',
scale: '2x')
.toJson(),
ContentsImageObject(
size: '60x60',
idiom: 'iphone',
filename: '$fileNamePrefix-60x60@3x.png',
scale: '3x')
.toJson(),
ContentsImageObject(
size: '20x20',
idiom: 'ipad',
filename: '$fileNamePrefix-20x20@1x.png',
scale: '1x')
.toJson(),
ContentsImageObject(
size: '20x20',
idiom: 'ipad',
filename: '$fileNamePrefix-20x20@2x.png',
scale: '2x')
.toJson(),
ContentsImageObject(
size: '29x29',
idiom: 'ipad',
filename: '$fileNamePrefix-29x29@1x.png',
scale: '1x')
.toJson(),
ContentsImageObject(
size: '29x29',
idiom: 'ipad',
filename: '$fileNamePrefix-29x29@2x.png',
scale: '2x')
.toJson(),
ContentsImageObject(
size: '40x40',
idiom: 'ipad',
filename: '$fileNamePrefix-40x40@1x.png',
scale: '1x')
.toJson(),
ContentsImageObject(
size: '40x40',
idiom: 'ipad',
filename: '$fileNamePrefix-40x40@2x.png',
scale: '2x')
.toJson(),
ContentsImageObject(
size: '72x72',
idiom: 'ipad',
filename: '$fileNamePrefix-72x72@1x.png',
scale: '1x')
.toJson(),
ContentsImageObject(
size: '72x72',
idiom: 'ipad',
filename: '$fileNamePrefix-72x72@2x.png',
scale: '2x')
.toJson(),
ContentsImageObject(
size: '76x76',
idiom: 'ipad',
filename: '$fileNamePrefix-76x76@1x.png',
scale: '1x')
.toJson(),
ContentsImageObject(
size: '76x76',
idiom: 'ipad',
filename: '$fileNamePrefix-76x76@2x.png',
scale: '2x')
.toJson(),
ContentsImageObject(
size: '83.5x83.5',
idiom: 'ipad',
filename: '$fileNamePrefix-83.5x83.5@2x.png',
scale: '2x')
.toJson(),
ContentsImageObject(
size: '1024x1024',
idiom: 'ios-marketing',
filename: '$fileNamePrefix-1024x1024@1x.png',
scale: '1x')
.toJson()
];
return imageList;
}