From 8bc48858f9c30a3d063058ada745b9f0c5b6b6ab Mon Sep 17 00:00:00 2001 From: Marco Papula Date: Tue, 11 Jul 2023 08:58:01 +0200 Subject: [PATCH 1/6] use asset bundle of inherited widget instead of root --- lib/src/rive_file.dart | 6 ++---- lib/src/widgets/rive_animation.dart | 2 ++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/src/rive_file.dart b/lib/src/rive_file.dart index 3ce9179c..d54b4311 100644 --- a/lib/src/rive_file.dart +++ b/lib/src/rive_file.dart @@ -293,11 +293,9 @@ class RiveFile { FileAssetLoader? assetLoader, bool cdn = true, bool importEmbeddedAssets = true, - AssetBundle? bundle, + required AssetBundle bundle, }) async { - final bytes = await (bundle ?? rootBundle).load( - bundleKey, - ); + final bytes = await bundle.load(bundleKey); return RiveFile.import( bytes, diff --git a/lib/src/widgets/rive_animation.dart b/lib/src/widgets/rive_animation.dart index 2e1acd6c..35dd10f9 100644 --- a/lib/src/widgets/rive_animation.dart +++ b/lib/src/widgets/rive_animation.dart @@ -190,6 +190,7 @@ class RiveAnimationState extends State { @override void initState() { super.initState(); + WidgetsBinding.instance.addPostFrameCallback((_) => _configure()); _configure(); } @@ -208,6 +209,7 @@ class RiveAnimationState extends State { widget.name!, importEmbeddedAssets: widget.importEmbeddedAssets!, assetLoader: widget.assetLoader, + bundle: DefaultAssetBundle.of(context), ); case _Source.network: return RiveFile.network( From 3de9f49e07a798e0573410123c16055d2743bad3 Mon Sep 17 00:00:00 2001 From: Marco Papula Date: Tue, 11 Jul 2023 09:03:15 +0200 Subject: [PATCH 2/6] remove duplicate _configure call --- lib/src/widgets/rive_animation.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/src/widgets/rive_animation.dart b/lib/src/widgets/rive_animation.dart index 35dd10f9..11db3dcc 100644 --- a/lib/src/widgets/rive_animation.dart +++ b/lib/src/widgets/rive_animation.dart @@ -191,7 +191,6 @@ class RiveAnimationState extends State { void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) => _configure()); - _configure(); } /// Loads [RiveFile] and calls [_init] From c321dce473a54f47b5c88a388203e058610b219c Mon Sep 17 00:00:00 2001 From: Marco Papula Date: Tue, 11 Jul 2023 09:11:37 +0200 Subject: [PATCH 3/6] add mock asset bundle --- test/mocks/mocks.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/mocks/mocks.dart b/test/mocks/mocks.dart index 64a8b8de..7778b326 100644 --- a/test/mocks/mocks.dart +++ b/test/mocks/mocks.dart @@ -1,3 +1,4 @@ +import 'package:flutter/widgets.dart'; import 'package:mocktail/mocktail.dart'; import 'package:rive/src/rive_core/artboard.dart'; export 'fakes.dart'; @@ -8,3 +9,5 @@ abstract class _OnInitFunction { } class OnInitCallbackMock extends Mock implements _OnInitFunction {} + +class MockAssetBundle extends Mock implements AssetBundle {} From 71c9a21ec6cfea1b240a4268e9ee18378e3bf644 Mon Sep 17 00:00:00 2001 From: Marco Papula Date: Tue, 11 Jul 2023 09:11:56 +0200 Subject: [PATCH 4/6] add test ensuring correct asset bundle is used --- test/asset_test.dart | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/asset_test.dart b/test/asset_test.dart index 10444f0e..d07eb65a 100644 --- a/test/asset_test.dart +++ b/test/asset_test.dart @@ -248,5 +248,24 @@ void main() { 'https://public.uat.rive.app/cdn/uuid/69a03ce3-83f0-4fcb-94a5-0d401b8c030e'), )).called(1); }); + + testWidgets('Uses AssetBundle of context instead of rootBundle', + (WidgetTester tester) async { + await HttpOverrides.runZoned(() async { + final assetBundle = MockAssetBundle(); + final riveBytes = loadFile('assets/image_asset_uat.riv'); + + when(() => assetBundle.load(any())).thenAnswer((_) async => riveBytes); + + await tester.pumpWidget( + DefaultAssetBundle( + bundle: assetBundle, + child: const RiveAnimation.asset('assets/image_asset_uat.riv'), + ), + ); + + verify(() => assetBundle.load(any())).called(1); + }, createHttpClient: (_) => mockHttpClient); + }); }); } From 4e6c6c30c8c0c3a6650b0b28e65316648da9d906 Mon Sep 17 00:00:00 2001 From: Marco Papula Date: Tue, 11 Jul 2023 09:13:12 +0200 Subject: [PATCH 5/6] fix typo --- lib/src/widgets/rive_animation.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/widgets/rive_animation.dart b/lib/src/widgets/rive_animation.dart index 11db3dcc..e63f1410 100644 --- a/lib/src/widgets/rive_animation.dart +++ b/lib/src/widgets/rive_animation.dart @@ -63,7 +63,7 @@ class RiveAnimation extends StatefulWidget { /// disable this customize assets even when embedded final bool? importEmbeddedAssets; - /// Specify an assetLoader explicitley, leave this blank to let rive + /// Specify an assetLoader explicitly, leave this blank to let rive /// chose how to load an asset itself. final FileAssetLoader? assetLoader; From 1d816e1fefb1e2611217149254dbefbb3e01a2b6 Mon Sep 17 00:00:00 2001 From: Marco Papula Date: Mon, 17 Jul 2023 19:54:42 +0200 Subject: [PATCH 6/6] fix merge conflicts --- example/lib/custom_asset_loading.dart | 2 ++ example/lib/custom_cached_asset_loading.dart | 2 ++ lib/src/rive_file.dart | 2 +- lib/src/widgets/rive_animation.dart | 1 + 4 files changed, 6 insertions(+), 1 deletion(-) diff --git a/example/lib/custom_asset_loading.dart b/example/lib/custom_asset_loading.dart index 7569b1d0..560df9bb 100644 --- a/example/lib/custom_asset_loading.dart +++ b/example/lib/custom_asset_loading.dart @@ -89,6 +89,7 @@ class _RiveRandomImageState extends State<_RiveRandomImage> { return true; }, ), + bundle: DefaultAssetBundle.of(context), ); setState(() { @@ -149,6 +150,7 @@ class _RiveRandomFontState extends State<_RiveRandomFont> { return true; }, ), + bundle: DefaultAssetBundle.of(context), ); setState(() { diff --git a/example/lib/custom_cached_asset_loading.dart b/example/lib/custom_cached_asset_loading.dart index 406f46d0..98a0a092 100644 --- a/example/lib/custom_cached_asset_loading.dart +++ b/example/lib/custom_cached_asset_loading.dart @@ -174,6 +174,7 @@ class __RiveRandomCachedImageState extends State<_RiveRandomCachedImage> { return false; }, ), + bundle: DefaultAssetBundle.of(context), ); setState(() => _riveImageSampleFile = imageFile); @@ -248,6 +249,7 @@ class __RiveRandomCachedFontState extends State<_RiveRandomCachedFont> { return false; }, ), + bundle: DefaultAssetBundle.of(context), ); setState(() { diff --git a/lib/src/rive_file.dart b/lib/src/rive_file.dart index 4803e037..d5abbfda 100644 --- a/lib/src/rive_file.dart +++ b/lib/src/rive_file.dart @@ -309,7 +309,7 @@ class RiveFile { /// file - as set in the editor. static Future asset( String bundleKey, { - AssetBundle? bundle, + required AssetBundle bundle, FileAssetLoader? assetLoader, bool loadCdnAssets = true, bool loadEmbeddedAssets = true, diff --git a/lib/src/widgets/rive_animation.dart b/lib/src/widgets/rive_animation.dart index d843c668..83a31571 100644 --- a/lib/src/widgets/rive_animation.dart +++ b/lib/src/widgets/rive_animation.dart @@ -189,6 +189,7 @@ class RiveAnimationState extends State { case _Source.asset: return RiveFile.asset( widget.name!, + bundle: DefaultAssetBundle.of(context), ); case _Source.network: return RiveFile.network(