Skip to content

Commit

Permalink
Plugin speedup! 🚅 (#558)
Browse files Browse the repository at this point in the history
* Added the initial setup of Isolates for speed up

* Updated README.md and CHANGELOG.md with new changes

* Removed the commented code

* Updated the name of the inner variable

* Updated one more reference to depracted call
  • Loading branch information
vlazdra committed Jun 4, 2023
1 parent f584a7c commit dd7a57c
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 54 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## Unreleased
- Package speed up using Isolates to generate files at the same time
- New command for generating all flavors at once: --flavors

## [2.3.0] - (2023-May-15)
- Support for GIFs in splash screen for web. Thanks [OutdatedGuy](https://github.com/OutdatedGuy) for [PR #547](https://github.com/jonbhanson/flutter_native_splash/pull/547).
- Using inline css & js in index.html to decrease load time. Thanks [OutdatedGuy](https://github.com/OutdatedGuy) for [PR #548](https://github.com/jonbhanson/flutter_native_splash/pull/548).
Expand Down
14 changes: 10 additions & 4 deletions README.md
Expand Up @@ -313,21 +313,27 @@ flutter_native_splash:
web: false
```

Great, now comes the fun part running the new command!

The new command is:
If you'd like to generate only a single flavor (maybe you are
testing something out), you can use only the single command like this:

```bash
# If you have a flavor called production you would do this:
dart run flutter_native_splash:create --flavor production

# For a flavor with a name staging you would provide it's name like so:
dart run flutter_native_splash:create --flavor staging
dart run flutter_native_splash:create --flavor acceptance

# And if you have a local version for devs you could do that:
dart run flutter_native_splash:create --flavor development
```

You also have the ability to specify all the flavors in one command
as shown bellow:
```bash
dart run flutter_native_splash:create --flavors development,staging,production
```
Note: the available flavors need to be comma separated for this option to work.

### Android setup

You're done! No, really, Android doesn't need any additional setup.
Expand Down
28 changes: 24 additions & 4 deletions bin/create.dart
Expand Up @@ -6,11 +6,31 @@ void main(List<String> args) {

parser.addOption('path');
parser.addOption('flavor');
parser.addOption('flavors');

final parsedArgs = parser.parse(args);

createSplash(
path: parsedArgs['path']?.toString(),
flavor: parsedArgs['flavor']?.toString(),
);
if (parsedArgs['flavor'] != null && parsedArgs['flavors'] != null) {
throw Exception('Cannot use both flavor and flavors arguments');
}

if (parsedArgs['flavor'] != null) {
createSplash(
path: parsedArgs['path']?.toString(),
flavor: parsedArgs['flavor']?.toString(),
);
} else if (parsedArgs['flavors'] != null) {
final flavors = parsedArgs['flavors']?.toString().split(',');
for (final flavor in flavors!) {
createSplash(
path: parsedArgs['path']?.toString(),
flavor: flavor,
);
}
} else {
createSplash(
path: parsedArgs['path']?.toString(),
flavor: null,
);
}
}
54 changes: 32 additions & 22 deletions lib/android.dart
Expand Up @@ -248,9 +248,12 @@ void _applyImageAndroid({
exit(1);
}

for (final template in templates) {
_saveImageAndroid(template: template, image: image, fileName: fileName);
}
_saveImageAndroid(
templates: templates,
image: image,
fileName: fileName,
androidResFolder: _flavorHelper.androidResFolder,
);
}
}

Expand All @@ -269,28 +272,35 @@ List<_AndroidDrawableTemplate> _getAssociatedTemplates({
/// Note: Do not change interpolation unless you end up with better results
/// https://github.com/fluttercommunity/flutter_launcher_icons/issues/101#issuecomment-495528733
void _saveImageAndroid({
required _AndroidDrawableTemplate template,
required List<_AndroidDrawableTemplate> templates,
required Image image,
required fileName,
}) {
//added file name attribute to make this method generic for splash image and branding image.
final newFile = copyResize(
image,
width: image.width * template.pixelDensity ~/ 4,
height: image.height * template.pixelDensity ~/ 4,
interpolation: Interpolation.cubic,
);

// Whne the flavor value is not specified we will place all the data inside the main directory.
// However if the flavor value is specified, we need to place the data in the correct directory.
// Default: android/app/src/main/res/
// With a flavor: android/app/src/[flavor name]/res/
final file = File(
'${_flavorHelper.androidResFolder}${template.directoryName}/$fileName',
required String androidResFolder,
}) async {
await Future.wait(
templates.map(
(template) => Isolate.run(() async {
//added file name attribute to make this method generic for splash image and branding image.
final newFile = copyResize(
image,
width: image.width * template.pixelDensity ~/ 4,
height: image.height * template.pixelDensity ~/ 4,
interpolation: Interpolation.cubic,
);

// When the flavor value is not specified we will place all the data inside the main directory.
// However if the flavor value is specified, we need to place the data in the correct directory.
// Default: android/app/src/main/res/
// With a flavor: android/app/src/[flavor name]/res/
final file = File(
'$androidResFolder${template.directoryName}/$fileName',
);
// File(_androidResFolder + template.directoryName + '/' + 'splash.png');
await file.create(recursive: true);
await file.writeAsBytes(encodePng(newFile));
}),
),
);
// File(_androidResFolder + template.directoryName + '/' + 'splash.png');
file.createSync(recursive: true);
file.writeAsBytesSync(encodePng(newFile));
}

void _deleteImageAndroid({
Expand Down
7 changes: 4 additions & 3 deletions lib/cli_commands.dart
Expand Up @@ -3,6 +3,8 @@
/// This is the main entry point for the Flutter Native Splash package.
library flutter_native_splash_cli;

import 'dart:isolate';

import 'package:html/parser.dart' as html_parser;
import 'package:image/image.dart';
import 'package:meta/meta.dart';
Expand All @@ -29,10 +31,9 @@ void createSplash({
print(
'''
╔════════════════════════════════════════════════════════════════════════════╗
║ Flavor detected! ║
╠════════════════════════════════════════════════════════════════════════════╣
║ Setting up the $flavor flavor. ║
║ Setting up flavors! ║
╚════════════════════════════════════════════════════════════════════════════╝
===> Setting up the $flavor flavor.
''',
);
}
Expand Down
36 changes: 16 additions & 20 deletions lib/ios.dart
Expand Up @@ -173,7 +173,7 @@ void _applyImageiOS({
bool dark = false,
required List<_IosLaunchImageTemplate> list,
String? targetPath,
}) {
}) async {
// Because the path is no longer static, targetPath can't have a default value.
// That's why this was added, as a setup for a default value.
targetPath ??= _flavorHelper.iOSAssetsLaunchImageFolder;
Expand All @@ -185,27 +185,23 @@ void _applyImageiOS({
print('$imagePath could not be loaded.');
exit(1);
}
for (final template in list) {
_saveImageiOS(template: template, image: image, targetPath: targetPath);
}
}

/// Saves splash screen image to the project
void _saveImageiOS({
required _IosLaunchImageTemplate template,
required Image image,
required String targetPath,
}) {
final newFile = copyResize(
image,
width: image.width * template.pixelDensity ~/ 4,
height: image.height * template.pixelDensity ~/ 4,
interpolation: Interpolation.cubic,
);
await Future.wait(
list.map(
(template) => Isolate.run(() async {
final newFile = copyResize(
image,
width: image.width * template.pixelDensity ~/ 4,
height: image.height * template.pixelDensity ~/ 4,
interpolation: Interpolation.cubic,
);

final file = File(targetPath + template.fileName);
file.createSync(recursive: true);
file.writeAsBytesSync(encodePng(newFile));
final file = File(targetPath! + template.fileName);
await file.create(recursive: true);
await file.writeAsBytes(encodePng(newFile));
}),
),
);
}

/// Updates LaunchScreen.storyboard adding splash image path
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Expand Up @@ -6,7 +6,7 @@ repository: https://github.com/jonbhanson/flutter_native_splash
issue_tracker: https://github.com/jonbhanson/flutter_native_splash/issues

environment:
sdk: '>=2.18.0 <3.0.0'
sdk: '>=2.19.0 <3.0.0'
flutter: ">=2.5.0"

dependencies:
Expand Down

0 comments on commit dd7a57c

Please sign in to comment.