Skip to content

Support Lottie ZIP archive files #587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ Widget build(BuildContext context) {
| [flutter_svg](https://pub.dev/packages/flutter_svg) | .svg | `flutter_svg: true` | Assets.images.icons.paint.**svg()** |
| [flare_flutter](https://pub.dev/packages/flare_flutter) | .flr | `flare_flutter: true` | Assets.flare.penguin.**flare()** |
| [rive](https://pub.dev/packages/rive) | .flr | `rive: true` | Assets.rive.vehicles.**rive()** |
| [lottie](https://pub.dev/packages/lottie) | .json | `lottie: true` | Assets.lottie.hamburgerArrow.**lottie()** |
| [lottie](https://pub.dev/packages/lottie) | .json, .zip | `lottie: true` | Assets.lottie.hamburgerArrow.**lottie()** |


In other cases, the asset is generated as String class.
Expand Down
Binary file not shown.
Binary file added examples/example/assets/lottie/wrong/dummy.zip
Binary file not shown.
17 changes: 14 additions & 3 deletions examples/example/lib/gen/assets.gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,20 @@ class $AssetsLottieGen {
LottieGenImage get hamburgerArrow =>
const LottieGenImage('assets/lottie/hamburger_arrow.json');

/// File path: assets/lottie/spinning_carrousel.zip
LottieGenImage get spinningCarrousel =>
const LottieGenImage('assets/lottie/spinning_carrousel.zip');

/// Directory path: assets/lottie/wrong
$AssetsLottieWrongGen get wrong => const $AssetsLottieWrongGen();

/// List of all assets
List<LottieGenImage> get values =>
[alarmClockLottieV440, geometricalAnimation, hamburgerArrow];
List<LottieGenImage> get values => [
alarmClockLottieV440,
geometricalAnimation,
hamburgerArrow,
spinningCarrousel
];
}

class $AssetsMixGen {
Expand Down Expand Up @@ -193,11 +201,14 @@ class $AssetsImagesIconsGen {
class $AssetsLottieWrongGen {
const $AssetsLottieWrongGen();

/// File path: assets/lottie/wrong/dummy.zip
String get dummy => 'assets/lottie/wrong/dummy.zip';

/// File path: assets/lottie/wrong/rocket-lottie-v439.json
String get rocketLottieV439 => 'assets/lottie/wrong/rocket-lottie-v439.json';

/// List of all assets
List<String> get values => [rocketLottieV439];
List<String> get values => [dummy, rocketLottieV439];
}

class MyAssets {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:convert';
import 'dart:io';

import 'package:archive/archive_io.dart';
import 'package:collection/collection.dart';
import 'package:flutter_gen_core/generators/integrations/integration.dart';
import 'package:path/path.dart' as p;
import 'package:pub_semver/pub_semver.dart';
Expand All @@ -19,6 +21,11 @@ class LottieIntegration extends Integration {
'layers', // Must include layers
];

static const _supportedMimeTypes = [
'application/json',
'application/zip',
];

String get packageExpression => isPackage ? ' = package' : '';

@override
Expand Down Expand Up @@ -110,12 +117,33 @@ ${isPackage ? "\n static const String package = '$packageName';" : ''}
bool get isConstConstructor => true;

bool isLottieFile(AssetType type) {
if (type.mime != 'application/json') {
if (!_supportedMimeTypes.contains(type.mime)) {
return false;
}
if (type.mime == 'application/zip') {
final inputStream = InputFileStream(type.fullPath);
final archive = ZipDecoder().decodeBuffer(inputStream);
final jsonFile = archive.files.firstWhereOrNull(
(e) => e.name.endsWith('.json'),
);
if (jsonFile?.isFile != true) {
return false;
}
final content = utf8.decode(jsonFile!.content);
return _isValidJsonFile(type, overrideInput: content);
}
return _isValidJsonFile(type);
}

bool _isValidJsonFile(AssetType type, {String? overrideInput}) {
try {
final absolutePath = p.join(type.rootPath, type.path);
String input = File(absolutePath).readAsStringSync();
final String input;
if (overrideInput != null) {
input = overrideInput;
} else {
final absolutePath = p.join(type.rootPath, type.path);
input = File(absolutePath).readAsStringSync();
}
final fileKeys = jsonDecode(input) as Map<String, dynamic>;
if (lottieKeys.every(fileKeys.containsKey) && fileKeys['v'] != null) {
var version = Version.parse(fileKeys['v']);
Expand Down
27 changes: 15 additions & 12 deletions packages/core/lib/settings/asset_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:flutter_gen_core/utils/identifer.dart';
import 'package:flutter_gen_core/utils/string.dart';
import 'package:mime/mime.dart' show lookupMimeType;
import 'package:path/path.dart' as p;
import 'package:path/path.dart';

/// https://github.com/dart-lang/mime/blob/master/lib/src/default_extension_map.dart
class AssetType {
Expand Down Expand Up @@ -40,12 +39,15 @@ class AssetType {

bool get isUnKnownMime => mime == null;

String get extension => p.extension(path);
/// Returns a name for this asset.
String get name => p.withoutExtension(path);

String get baseName => p.basenameWithoutExtension(path);

String get extension => p.extension(path);

/// Returns the full absolute path for reading the asset file.
String get fullPath => join(rootPath, path);
String get fullPath => p.join(rootPath, path);

// Replace to Posix style for Windows separator.
String get posixStylePath => path.replaceAll(r'\', r'/');
Expand All @@ -56,10 +58,12 @@ class AssetType {
_children.add(type);
}

/// Returns a name for this asset.
String get name {
return withoutExtension(path);
}
@override
String toString() => 'AssetType('
'rootPath: $rootPath, '
'path: $path, '
'flavors: $flavors'
')';
}

/// Represents a AssetType with modifiers on it to mutate the [name] to ensure
Expand Down Expand Up @@ -99,15 +103,14 @@ class UniqueAssetType extends AssetType {
String get name {
// Omit root directory from the name if it is either asset or assets.
// TODO(bramp): Maybe move this into the _flatStyleDefinition
String p = path.replaceFirst(RegExp(r'^asset(s)?[/\\]'), '');
String result = path.replaceFirst(RegExp(r'^asset(s)?[/\\]'), '');
if (basenameOnly) {
p = basename(p);
result = p.basename(result);
}
if (!needExtension) {
p = withoutExtension(p);
result = p.withoutExtension(result);
}

return style(convertToIdentifier(p)) + suffix;
return style(convertToIdentifier(result)) + suffix;
}

@override
Expand Down
1 change: 1 addition & 0 deletions packages/core/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies:
glob: ^2.0.0

dart_style: ^2.2.4
archive: ^3.4.0
args: ^2.0.0
pub_semver: ^2.0.0
vector_graphics_compiler: ^1.1.9
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
import 'package:collection/collection.dart';
import 'package:flutter_gen_core/settings/asset_type.dart';
import 'package:flutter_gen_core/settings/flavored_asset.dart';
import 'package:test/test.dart';

void main() {
group(AssetType, () {
test('constructor', () {
final assetType = AssetType(
rootPath: 'root',
path: 'assets/single.jpg',
flavors: {'flavor'},
);
expect(assetType, isA<AssetType>());
expect(assetType.name, 'assets/single');
expect(assetType.baseName, 'single');
expect(assetType.extension, '.jpg');
expect(assetType.isUnKnownMime, false);
expect(
assetType,
predicate<AssetType>(
(e) => SetEquality().equals(e.flavors, {'flavor'}),
),
);
expect(
assetType.toString(),
'AssetType(rootPath: root, path: assets/single.jpg, flavors: {flavor})',
);
});
});

group(FlavoredAsset, () {
test('constructor', () {
expect(
Expand Down

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

Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ flutter_gen:
flutter:
assets:
- assets/lottie/hamburger_arrow.json
- assets/lottie/spinning_carrousel.zip
Loading