-
-
Notifications
You must be signed in to change notification settings - Fork 151
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
Add Lottie integration #285
Conversation
* example & test asset * auto strip "lottie" from final asset var name
* dropped support for '*_lottie.json' per FlutterGen#70 PR * include root path for tests * update example
* rename resourcesPath for better code readability
For Discussion
|
@onlymice Thank you for your nice PR. I'll check it next week, so please wait. 🙏🏽 |
@@ -14,6 +14,7 @@ version_gen: | |||
path: lib/ | |||
|
|||
dependencies: | |||
lottie: '>=1.4.1 <2.0.0' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably don't need dependencies on the core package.
AssetType(this.path); | ||
AssetType(this.path, String rootPath) : absolutePath = p.join(rootPath, path); | ||
|
||
final String absolutePath; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't prefer to widen the influence range because it is only used in LottieIntegration#isSupport.
@@ -29,7 +30,7 @@ class AssetsGenConfig { | |||
|
|||
factory AssetsGenConfig.fromConfig(File pubspecFile, Config config) { | |||
return AssetsGenConfig._( | |||
pubspecFile.parent.path, | |||
pubspecFile.parent.absolute.path, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We believe the problem can be solved by simply passing the RootPath to isSupport. No big changes to assets_generator.dart is needed.
@@ -31,8 +33,10 @@ void main() { | |||
expect(integration.className, 'SvgGenImage'); | |||
expect(integration.classInstantiate('assets/path'), | |||
'SvgGenImage(\'assets/path\')'); | |||
expect(integration.isSupport(AssetType('assets/path/dog.svg')), isTrue); | |||
expect(integration.isSupport(AssetType('assets/path/dog.png')), isFalse); | |||
expect(integration.isSupport(AssetType('assets/path/dog.svg', resPath)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't add any variables to the AssetType, so no changes here are needed either.
My codes. integration.dart abstract class Integration {
// ..
bool isSupport(AssetType type, {String? rootPath});
// ..
} svg/rive/flare integratinos.dart class SvgIntegration extends Integration {
// ..
@override
bool isSupport(AssetType type, {String? rootPath}) => type.mime == 'image/svg+xml';
// ..
} lottie integratinos.dart class LottieIntegration extends Integration {
// ..
@override
bool isSupport(AssetType type, {String? rootPath}) =>
isLottieFile(type, rootPath ?? p.current);
@override
bool get isConstConstructor => true;
bool isLottieFile(AssetType type, String rootPath) {
if (!type.path.endsWith('.json')) {
return false;
}
var input = File(p.join(rootPath, type.path)).readAsStringSync();
var fileKeys = jsonDecode(input);
if (fileKeys.runtimeType != Map &&
!lottieKeys.every((key) => fileKeys.containsKey(key))) {
return false;
}
var versions = fileKeys['v'];
if (versions is! String) {
return false;
}
var version = int.tryParse(versions.replaceAll('.', '')) ?? 0;
// Lottie version 4.4.0 is the first version that supports BodyMovin.
// https://github.com/xvrh/lottie-flutter/blob/0e7499d82ea1370b6acf023af570395bbb59b42f/lib/src/parser/lottie_composition_parser.dart#L60
return version / 1000 >= 0.440;
}
// ..
} assets_generator.dart // ... only this
(element) => element.isSupport(assetType, rootPath: rootPath),
// ... assets_gen_integrations_test.dart // No change except here
// only add.
test('Assets with Lottie integrations on pubspec.yaml', () async {
const pubspec = 'test_resources/pubspec_assets_lottie_integrations.yaml';
const fact =
'test_resources/actual_data/assets_lottie_integrations.gen.dart';
const generated =
'test_resources/lib/gen/assets_lottie_integrations.gen.dart';
await expectedAssetsGen(pubspec, generated, fact);
final integration = LottieIntegration();
expect(integration.className, 'LottieGenImage');
expect(integration.classInstantiate('assets/lottie'),
'LottieGenImage(\'assets/lottie\')');
final testResPath = p.absolute('test_resources'); // 🌟
expect(
integration.isSupport(AssetType('assets/lottie/hamburger_arrow.json'),
rootPath: testResPath), // 🌟
isTrue);
expect(
integration.isSupport(
AssetType('assets/lottie/hamburger_arrow_without_version.json'),
rootPath: testResPath),
isFalse);
expect(integration.isConstConstructor, isTrue);
}); |
@onlymice Thanks for the great suggestions for a lottie users and me. |
Hi @wasabeef, updated code 🎯 bool isLottieFile(AssetType type) {
try {
if (type.extension != '.json') {
return false;
}
String input = File(type.absolutePath).readAsStringSync();
final fileKeys = jsonDecode(input) as Map<String, dynamic>;
if (lottieKeys.every((key) => fileKeys.containsKey(key)) &&
fileKeys['v'] != null) {
var version = Version.parse(fileKeys['v']);
// Lottie version 4.4.0 is the first version that supports BodyMovin.
// https://github.com/xvrh/lottie-flutter/blob/0e7499d82ea1370b6acf023af570395bbb59b42f/lib/src/parser/lottie_composition_parser.dart#L60
return version >= Version(4, 4, 0); 🎯
}
} on FormatException catch (e) {
// Catches bad/corrupted json and reports it to user. 🎯
stderr.writeln(e.message);
}
return false;
} |
Hi @onlymice, |
What does this change?
Adds lottie integration.
Checks for lottie files by in json tags
Example includes on how to use it
Docs changed accordingly
Related
Initial Feature Request: #47
Previous PR that tried to introduce support for Lottie: #70
What's different now?
Previously HiroyukiTamura #70 implemented a Lottie integration per request #47 (comment) but as stated by britannio's #70 (comment) in the initial PR, their implementation will be better off checking for keys in the file itself, rather than only it's extension
*_lottie.json
as it was initially requested two years ago.So I did.
Other issues
Why there is a lot of changes to tests
Initially Implementation of this feature required File read access, there was no issues with that if you run code as designed e.g
But if you try to run tests you might ended up seeing that the relative asset path that you get in
isSupport
is not relative to theDirectory.current.path
and the code in'packages/core/lib'
can't access it while running tests.So I introduced mocked
rootPath
#assets_gen_integrations_test.dart#L13 for tests and now the AssetType has field forabsolutePath
that is constructed from the passed rootPath (config.rootPath
) and the key/path
.Other integrations haven't tried to read files, cause they don't need it, so here we are, feel free to propose a better solution.
What is the value of this and can you measure success?
Measure
Pass tests.
No linting issues with generated code.
Example runs, plays the animation and works well.
Value