react-native-bootsplash 3.2.5 → 4.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/generate.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  import chalk from "chalk";
2
2
  import fs from "fs-extra";
3
- import jimp from "jimp";
3
+ import Jimp from "jimp";
4
4
  import path from "path";
5
5
 
6
6
  const logoFileName = "bootsplash_logo";
7
7
  const xcassetName = "BootSplashLogo";
8
+ // https://github.com/androidx/androidx/blob/androidx-main/core/core-splashscreen/src/main/res/values/dimens.xml#L22
9
+ const splashScreenIconSizeNoBackground = 288;
8
10
  const androidColorName = "bootsplash_background";
9
11
  const androidColorRegex = /<color name="bootsplash_background">#\w+<\/color>/g;
10
12
 
@@ -96,17 +98,6 @@ const getStoryboard = ({
96
98
  `;
97
99
  };
98
100
 
99
- const bootSplashXml = `<?xml version="1.0" encoding="utf-8"?>
100
-
101
- <layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
102
- <item android:drawable="@color/${androidColorName}" />
103
-
104
- <item>
105
- <bitmap android:src="@mipmap/${logoFileName}" android:gravity="center" />
106
- </item>
107
- </layer-list>
108
- `;
109
-
110
101
  const log = (text: string, dim = false) => {
111
102
  console.log(dim ? chalk.dim(text) : text);
112
103
  };
@@ -127,7 +118,7 @@ export const generate = async ({
127
118
  android,
128
119
  ios,
129
120
 
130
- workingDirectory,
121
+ workingPath,
131
122
  logoPath,
132
123
  backgroundColor,
133
124
  logoWidth,
@@ -142,12 +133,13 @@ export const generate = async ({
142
133
  projectPath: string;
143
134
  } | null;
144
135
 
145
- workingDirectory: string;
136
+ workingPath: string;
146
137
  logoPath: string;
138
+ assetsPath?: string;
139
+
147
140
  backgroundColor: string;
148
- logoWidth: number;
149
141
  flavor: string;
150
- assetsPath?: string;
142
+ logoWidth: number;
151
143
  }) => {
152
144
  if (!isValidHexadecimal(backgroundColor)) {
153
145
  throw new Error(
@@ -155,61 +147,48 @@ export const generate = async ({
155
147
  );
156
148
  }
157
149
 
158
- const image = await jimp.read(logoPath);
150
+ const image = await Jimp.read(logoPath);
159
151
  const backgroundColorHex = toFullHexadecimal(backgroundColor);
160
152
 
161
- const images: {
162
- filePath: string;
163
- width: number;
164
- height: number;
165
- }[] = [];
166
-
167
153
  const getHeight = (size: number) =>
168
154
  Math.ceil(size * (image.bitmap.height / image.bitmap.width));
169
155
 
170
- const width = {
171
- "@1x": logoWidth,
172
- "@1,5x": logoWidth * 1.5,
173
- "@2x": logoWidth * 2,
174
- "@3x": logoWidth * 3,
175
- "@4x": logoWidth * 4,
176
- };
177
-
178
- const height = {
179
- "@1x": getHeight(width["@1x"]),
180
- "@1,5x": getHeight(width["@1,5x"]),
181
- "@2x": getHeight(width["@2x"]),
182
- "@3x": getHeight(width["@3x"]),
183
- "@4x": getHeight(width["@4x"]),
184
- };
156
+ const logWrite = (
157
+ emoji: string,
158
+ filePath: string,
159
+ dimensions?: { width: number; height: number },
160
+ ) =>
161
+ log(
162
+ `${emoji} ${path.relative(workingPath, filePath)}` +
163
+ (dimensions != null
164
+ ? ` (${dimensions.width}x${dimensions.height})`
165
+ : ""),
166
+ true,
167
+ );
185
168
 
186
169
  if (assetsPath && fs.existsSync(assetsPath)) {
187
- images.push(
188
- {
189
- filePath: path.resolve(assetsPath, logoFileName + ".png"),
190
- width: width["@1x"],
191
- height: height["@1x"],
192
- },
193
- {
194
- filePath: path.resolve(assetsPath, logoFileName + "@1,5x.png"),
195
- width: width["@1,5x"],
196
- height: height["@1,5x"],
197
- },
198
- {
199
- filePath: path.resolve(assetsPath, logoFileName + "@2x.png"),
200
- width: width["@2x"],
201
- height: height["@2x"],
202
- },
203
- {
204
- filePath: path.resolve(assetsPath, logoFileName + "@3x.png"),
205
- width: width["@3x"],
206
- height: height["@3x"],
207
- },
208
- {
209
- filePath: path.resolve(assetsPath, logoFileName + "@4x.png"),
210
- width: width["@4x"],
211
- height: height["@4x"],
212
- },
170
+ await Promise.all(
171
+ [
172
+ { ratio: 1, suffix: "" },
173
+ { ratio: 1.5, suffix: "@1,5x" },
174
+ { ratio: 2, suffix: "@2x" },
175
+ { ratio: 3, suffix: "@3x" },
176
+ { ratio: 4, suffix: "@4x" },
177
+ ].map(({ ratio, suffix }) => {
178
+ const fileName = `${logoFileName}${suffix}.png`;
179
+ const filePath = path.resolve(assetsPath, fileName);
180
+ const width = logoWidth * ratio;
181
+ const height = getHeight(width);
182
+
183
+ return image
184
+ .clone()
185
+ .resize(width, height)
186
+ .quality(100)
187
+ .writeAsync(filePath)
188
+ .then(() => {
189
+ logWrite("", filePath, { width, height });
190
+ });
191
+ }),
213
192
  );
214
193
  }
215
194
 
@@ -219,16 +198,10 @@ export const generate = async ({
219
198
  : path.resolve(android.sourceDir); // @react-native-community/cli 2.x & 3.x support
220
199
 
221
200
  const resPath = path.resolve(appPath, "src", flavor, "res");
222
- const drawablePath = path.resolve(resPath, "drawable");
223
201
  const valuesPath = path.resolve(resPath, "values");
224
202
 
225
- fs.ensureDirSync(drawablePath);
226
203
  fs.ensureDirSync(valuesPath);
227
204
 
228
- const bootSplashXmlPath = path.resolve(drawablePath, "bootsplash.xml");
229
- fs.writeFileSync(bootSplashXmlPath, bootSplashXml, "utf-8");
230
- log(`✨ ${path.relative(workingDirectory, bootSplashXmlPath)}`, true);
231
-
232
205
  const colorsXmlPath = path.resolve(valuesPath, "colors.xml");
233
206
  const colorsXmlEntry = `<color name="${androidColorName}">${backgroundColorHex}</color>`;
234
207
 
@@ -252,7 +225,7 @@ export const generate = async ({
252
225
  );
253
226
  }
254
227
 
255
- log(`✏️ ${path.relative(workingDirectory, colorsXmlPath)}`, true);
228
+ logWrite("✏️ ", colorsXmlPath);
256
229
  } else {
257
230
  fs.writeFileSync(
258
231
  colorsXmlPath,
@@ -260,39 +233,39 @@ export const generate = async ({
260
233
  "utf-8",
261
234
  );
262
235
 
263
- log(`✨ ${path.relative(workingDirectory, colorsXmlPath)}`, true);
236
+ logWrite("✨", colorsXmlPath);
264
237
  }
265
238
 
266
- images.push(
267
- {
268
- filePath: path.resolve(resPath, "mipmap-mdpi", logoFileName + ".png"),
269
- width: width["@1x"],
270
- height: height["@1x"],
271
- },
272
- {
273
- filePath: path.resolve(resPath, "mipmap-hdpi", logoFileName + ".png"),
274
- width: width["@1,5x"],
275
- height: height["@1,5x"],
276
- },
277
- {
278
- filePath: path.resolve(resPath, "mipmap-xhdpi", logoFileName + ".png"),
279
- width: width["@2x"],
280
- height: height["@2x"],
281
- },
282
- {
283
- filePath: path.resolve(resPath, "mipmap-xxhdpi", logoFileName + ".png"),
284
- width: width["@3x"],
285
- height: height["@3x"],
286
- },
287
- {
288
- filePath: path.resolve(
289
- resPath,
290
- "mipmap-xxxhdpi",
291
- logoFileName + ".png",
292
- ),
293
- width: width["@4x"],
294
- height: height["@4x"],
295
- },
239
+ await Promise.all(
240
+ [
241
+ { ratio: 1, directory: "mipmap-mdpi" },
242
+ { ratio: 1.5, directory: "mipmap-hdpi" },
243
+ { ratio: 2, directory: "mipmap-xhdpi" },
244
+ { ratio: 3, directory: "mipmap-xxhdpi" },
245
+ { ratio: 4, directory: "mipmap-xxxhdpi" },
246
+ ].map(({ ratio, directory }) => {
247
+ const fileName = `${logoFileName}.png`;
248
+ const filePath = path.resolve(resPath, directory, fileName);
249
+ const width = logoWidth * ratio;
250
+ const height = getHeight(width);
251
+
252
+ const canvasSize = splashScreenIconSizeNoBackground * ratio;
253
+
254
+ // https://github.com/oliver-moran/jimp/tree/master/packages/jimp#creating-new-images
255
+ const canvas = new Jimp(canvasSize, canvasSize, 0xffffff00);
256
+ const logo = image.clone().resize(width, height);
257
+
258
+ const x = (canvasSize - width) / 2;
259
+ const y = (canvasSize - height) / 2;
260
+
261
+ return canvas
262
+ .blit(logo, x, y)
263
+ .quality(100)
264
+ .writeAsync(filePath)
265
+ .then(() => {
266
+ logWrite("✨", filePath, { width: canvasSize, height: canvasSize });
267
+ });
268
+ }),
296
269
  );
297
270
  }
298
271
 
@@ -306,14 +279,14 @@ export const generate = async ({
306
279
  fs.writeFileSync(
307
280
  storyboardPath,
308
281
  getStoryboard({
309
- height: height["@1x"],
310
- width: width["@1x"],
282
+ height: getHeight(logoWidth),
283
+ width: logoWidth,
311
284
  backgroundColor: backgroundColorHex,
312
285
  }),
313
286
  "utf-8",
314
287
  );
315
288
 
316
- log(`✨ ${path.relative(workingDirectory, storyboardPath)}`, true);
289
+ log(`✨ ${path.relative(workingPath, storyboardPath)}`, true);
317
290
  } else {
318
291
  log(
319
292
  `No "${projectPath}" directory found. Skipping iOS storyboard generation…`,
@@ -330,22 +303,26 @@ export const generate = async ({
330
303
  "utf-8",
331
304
  );
332
305
 
333
- images.push(
334
- {
335
- filePath: path.resolve(imageSetPath, logoFileName + ".png"),
336
- width: width["@1x"],
337
- height: height["@1x"],
338
- },
339
- {
340
- filePath: path.resolve(imageSetPath, logoFileName + "@2x.png"),
341
- width: width["@2x"],
342
- height: height["@2x"],
343
- },
344
- {
345
- filePath: path.resolve(imageSetPath, logoFileName + "@3x.png"),
346
- width: width["@3x"],
347
- height: height["@3x"],
348
- },
306
+ await Promise.all(
307
+ [
308
+ { ratio: 1, suffix: "" },
309
+ { ratio: 2, suffix: "@2x" },
310
+ { ratio: 3, suffix: "@3x" },
311
+ ].map(({ ratio, suffix }) => {
312
+ const fileName = `${logoFileName}${suffix}.png`;
313
+ const filePath = path.resolve(imageSetPath, fileName);
314
+ const width = logoWidth * ratio;
315
+ const height = getHeight(width);
316
+
317
+ return image
318
+ .clone()
319
+ .resize(width, height)
320
+ .quality(100)
321
+ .writeAsync(filePath)
322
+ .then(() => {
323
+ logWrite("✨", filePath, { width, height });
324
+ });
325
+ }),
349
326
  );
350
327
  } else {
351
328
  log(
@@ -354,24 +331,6 @@ export const generate = async ({
354
331
  }
355
332
  }
356
333
 
357
- await Promise.all(
358
- images.map(({ filePath, width, height }) =>
359
- image
360
- .clone()
361
- .cover(width, height)
362
- .writeAsync(filePath)
363
- .then(() => {
364
- log(
365
- `✨ ${path.relative(
366
- workingDirectory,
367
- filePath,
368
- )} (${width}x${height})`,
369
- true,
370
- );
371
- }),
372
- ),
373
- );
374
-
375
334
  log(
376
335
  `✅ Done! Thanks for using ${chalk.underline("react-native-bootsplash")}.`,
377
336
  );
package/src/index.ts CHANGED
@@ -4,15 +4,10 @@ export type VisibilityStatus = "visible" | "hidden" | "transitioning";
4
4
  export type Config = { fade?: boolean };
5
5
 
6
6
  const NativeModule: {
7
- show: (fade: boolean) => Promise<true>;
8
7
  hide: (fade: boolean) => Promise<true>;
9
8
  getVisibilityStatus: () => Promise<VisibilityStatus>;
10
9
  } = NativeModules.RNBootSplash;
11
10
 
12
- export function show(config: Config = {}): Promise<void> {
13
- return NativeModule.show({ fade: false, ...config }.fade).then(() => {});
14
- }
15
-
16
11
  export function hide(config: Config = {}): Promise<void> {
17
12
  return NativeModule.hide({ fade: false, ...config }.fade).then(() => {});
18
13
  }
@@ -21,4 +16,7 @@ export function getVisibilityStatus(): Promise<VisibilityStatus> {
21
16
  return NativeModule.getVisibilityStatus();
22
17
  }
23
18
 
24
- export default { show, hide, getVisibilityStatus };
19
+ export default {
20
+ hide,
21
+ getVisibilityStatus,
22
+ };
@@ -1,55 +0,0 @@
1
- package com.zoontek.rnbootsplash;
2
-
3
- import android.content.Context;
4
- import android.content.Intent;
5
- import android.os.Bundle;
6
-
7
- import androidx.annotation.Nullable;
8
- import androidx.appcompat.app.AppCompatActivity;
9
-
10
- public class RNBootSplashActivity extends AppCompatActivity {
11
-
12
- protected Class<?> getMainActivityClass() throws Exception {
13
- final Context appContext = getApplicationContext();
14
- final Package appPackage = appContext.getClass().getPackage();
15
- assert appPackage != null;
16
- final String className = appPackage.getName() + ".MainActivity";
17
-
18
- return Class.forName(className);
19
- }
20
-
21
- protected void forwardIntentToMainActivity(Intent intent) {
22
- try {
23
- Intent intentCopy = new Intent(intent);
24
-
25
- intentCopy.setClass(this, getMainActivityClass());
26
- intentCopy.putExtras(intent);
27
- intentCopy.setData(intent.getData());
28
- intentCopy.setAction(intent.getAction());
29
-
30
- String type = intent.getType();
31
-
32
- if (type != null) {
33
- intentCopy.setType(type);
34
- }
35
-
36
- startActivity(intentCopy);
37
- finish();
38
- } catch (Exception e) {
39
- e.printStackTrace();
40
- finishAffinity();
41
- }
42
- }
43
-
44
- @Override
45
- protected void onNewIntent(Intent intent) {
46
- super.onNewIntent(intent);
47
- forwardIntentToMainActivity(intent);
48
- }
49
-
50
- @Override
51
- protected void onCreate(@Nullable Bundle savedInstanceState) {
52
- super.onCreate(savedInstanceState);
53
- forwardIntentToMainActivity(getIntent());
54
- }
55
- }
@@ -1,3 +0,0 @@
1
- <resources>
2
- <item type="id" name="bootsplash_layout_id" />
3
- </resources>