Skip to content
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

Ybbaigo asset provider #214

Merged
merged 79 commits into from
Dec 26, 2019
Merged
Show file tree
Hide file tree
Changes from 76 commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
2e1fcaa
Updating flare_dart dependency to dev branch of Flare-Flutter.
luigi-rosso Apr 24, 2019
d8defc4
Fixing relative path for flare_dart.
luigi-rosso Apr 24, 2019
2ec15af
Merge branch 'master' of https://github.com/2d-inc/Flare-Flutter into…
luigi-rosso Apr 24, 2019
113d791
Merge branch 'master' of https://github.com/2d-inc/Flare-Flutter into…
luigi-rosso Apr 26, 2019
4aebb0f
Merge branch 'master' of https://github.com/2d-inc/Flare-Flutter into…
luigi-rosso Apr 26, 2019
c51192d
Merge pull request #89 from 2d-inc/master
luigi-rosso May 9, 2019
006d85b
Merge branch 'master' of https://github.com/2d-inc/Flare-Flutter into…
luigi-rosso May 20, 2019
396cd88
Merge branch 'master' of https://github.com/2d-inc/Flare-Flutter into…
luigi-rosso May 20, 2019
6606b73
Merge branch 'master' into dev
luigi-rosso Jun 4, 2019
8f04aa6
Merge pull request #102 from 2d-inc/master
luigi-rosso Jun 13, 2019
0ee2efe
Merge pull request #108 from 2d-inc/master
luigi-rosso Jun 20, 2019
01387cd
Merge pull request #115 from 2d-inc/master
luigi-rosso Jul 3, 2019
5533e73
Merge pull request #116 from 2d-inc/master
luigi-rosso Jul 3, 2019
e1a55f9
Merge pull request #119 from 2d-inc/master
luigi-rosso Jul 6, 2019
28846c5
Merge pull request #121 from 2d-inc/master
luigi-rosso Jul 6, 2019
56cea07
Updating stable to latest flare_flutter from dev.
luigi-rosso Jul 9, 2019
f24a688
Squashed commit of the following:
luigi-rosso Jul 9, 2019
307d63f
Squashed commit of the following:
luigi-rosso Jul 9, 2019
629866a
Squashed commit of the following:
luigi-rosso Jul 16, 2019
92d9dcb
Squashed commit of the following:
luigi-rosso Jul 24, 2019
9c2bd8c
Squashed commit of the following:
luigi-rosso Jul 24, 2019
b5e86e9
Squashed commit of the following:
luigi-rosso Jul 26, 2019
74f372f
Adding flutter environment requirement for #126
luigi-rosso Jul 26, 2019
998dc6f
Squashed commit of the following:
luigi-rosso Aug 23, 2019
c82e81e
Squashed commit of the following:
luigi-rosso Aug 23, 2019
2fddc7d
Squashed commit of the following:
luigi-rosso Aug 26, 2019
5d6ab3d
Squashed commit of the following:
luigi-rosso Aug 26, 2019
d26a59c
Squashed commit of the following:
luigi-rosso Aug 26, 2019
fa043d8
Squashed commit of the following:
luigi-rosso Aug 26, 2019
6c905cd
Squashed commit of the following:
luigi-rosso Sep 4, 2019
5340270
Squashed commit of the following:
luigi-rosso Sep 4, 2019
2875d00
Merge branch 'master' into dev
luigi-rosso Sep 23, 2019
f14d597
Merge branch 'dev' into stable
luigi-rosso Sep 23, 2019
e550a5e
Bumping flare_dart version in flare_flutter pubspec..
luigi-rosso Sep 23, 2019
68ae10c
Fix critical lints
mehmetf Sep 28, 2019
2df5759
Merge pull request #163 from mehmetf/b01
luigi-rosso Sep 30, 2019
9386337
Squashed commit of the following:
luigi-rosso Sep 30, 2019
177c441
Squashed commit of the following:
luigi-rosso Oct 2, 2019
b1ae611
Squashed commit of the following:
luigi-rosso Oct 4, 2019
13759e2
Squashed commit of the following:
luigi-rosso Oct 4, 2019
e3d2414
Squashed commit of the following:
luigi-rosso Oct 5, 2019
cfa4b99
Squashed commit of the following:
luigi-rosso Oct 5, 2019
e59d88d
Squashed commit of the following:
luigi-rosso Oct 7, 2019
18fda21
Squashed commit of the following:
luigi-rosso Oct 7, 2019
0b9c173
Squashed commit of the following:
luigi-rosso Oct 8, 2019
d74c0c8
Squashed commit of the following:
luigi-rosso Oct 8, 2019
a4f9cd3
Merge branch 'dev' into stable
luigi-rosso Oct 8, 2019
3c27f27
Fixing typo in last version.
luigi-rosso Oct 8, 2019
8282f2e
Update README.md
luigi-rosso Oct 8, 2019
226bba2
Bumping version.
luigi-rosso Oct 9, 2019
b646638
Merge branch 'stable' of https://github.com/2d-inc/Flare-Flutter into…
luigi-rosso Oct 9, 2019
ea16c60
Squashed commit of the following:
luigi-rosso Oct 9, 2019
7eb56d7
Squashed commit of the following:
luigi-rosso Oct 9, 2019
63b7b2c
Fixing merge issues.
luigi-rosso Oct 9, 2019
86b8f41
Squashed commit of the following:
luigi-rosso Oct 9, 2019
f695594
Squashed commit of the following:
luigi-rosso Oct 9, 2019
17896a7
Squashed commit of the following:
luigi-rosso Oct 11, 2019
ffab254
Merge branch 'dev' into stable
luigi-rosso Oct 11, 2019
fe016ef
Squashed commit of the following:
luigi-rosso Oct 11, 2019
eb99974
Squashed commit of the following:
luigi-rosso Oct 30, 2019
9dedc74
Squashed commit of the following:
luigi-rosso Nov 7, 2019
d28651d
Squashed commit of the following:
luigi-rosso Nov 7, 2019
2c16da8
Bumping dependency
luigi-rosso Nov 7, 2019
006c90b
Squashed commit of the following:
luigi-rosso Nov 7, 2019
88832dd
Squashed commit of the following:
luigi-rosso Nov 21, 2019
a2fdffd
Squashed commit of the following:
luigi-rosso Dec 5, 2019
b1602d9
Squashed commit of the following:
luigi-rosso Dec 5, 2019
1f4b4c5
Squashed commit of the following:
luigi-rosso Dec 10, 2019
c0dc639
Squashed commit of the following:
luigi-rosso Dec 16, 2019
838e986
Squashed commit of the following:
luigi-rosso Dec 16, 2019
398bbd6
Remove unused import.
luigi-rosso Dec 16, 2019
8c73e5e
Add AssetProvider
Dec 26, 2019
e6c0191
fix conflict
Dec 26, 2019
c6659b1
Merge branch 'stable' of https://github.com/ybbaigo/Flare-Flutter int…
luigi-rosso Dec 26, 2019
578fa37
Getting ybbaigo’s PR into a master branch and working on fixing up th…
luigi-rosso Dec 26, 2019
6a1d064
Fixing pubspec and revving version to 2.0.0 as the asset provider bre…
luigi-rosso Dec 26, 2019
8edbb59
Fixing change color example.
luigi-rosso Dec 26, 2019
b12e951
Fixing smiley_switch example.
luigi-rosso Dec 26, 2019
04ac1f8
Fixing space_reload example.
luigi-rosso Dec 26, 2019
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 @@ -12,7 +12,7 @@ This repository has three primary branches:
- This is the branch we publish to pub from.
- This branch and the associated pub packages are guaranteed to work on the flutter stable channel.
```
flare_flutter: ^1.7.1
flare_flutter: ^1.8.3
```
- dev
- This branch has the latest changes should work with the flutter dev channel.
Expand Down
12 changes: 8 additions & 4 deletions example/simple/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import "package:flare_flutter/flare_actor.dart";
import "package:flare_flutter/flare_cache_builder.dart";
import "package:flutter/material.dart";
import 'package:flutter/services.dart';
import 'package:flare_flutter/provider/asset_flare.dart';

void main() => runApp(MyApp());

Expand All @@ -27,6 +29,8 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> {
String _animationName = "idle";

final asset = AssetFlare(bundle: rootBundle, name: "assets/Filip.flr");

@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -39,12 +43,12 @@ class _MyHomePageState extends State<MyHomePage> {
children: [
Expanded(
child: FlareCacheBuilder(
["assets/Filip.flr"],
[asset],
builder: (BuildContext context, bool isWarm) {
return !isWarm
? Container(child:Text("NO"))
: FlareActor(
"assets/Filip.flr",
? Container(child: Text("NO"))
: FlareActor.asset(
asset,
alignment: Alignment.center,
fit: BoxFit.contain,
animation: _animationName,
Expand Down
10 changes: 0 additions & 10 deletions flare_flutter/lib/asset_bundle_cache.dart

This file was deleted.

9 changes: 9 additions & 0 deletions flare_flutter/lib/asset_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'dart:typed_data';

/// Identifies an asset, to obtain asset from an [AssetProvider], call [load].
abstract class AssetProvider {
const AssetProvider();

/// Loads the asset.
Future<ByteData> load();
}
35 changes: 15 additions & 20 deletions flare_flutter/lib/cache.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'dart:async';

import 'asset_provider.dart';
import 'cache_asset.dart';

typedef CacheAsset AssetFactoryMethod();

/// A base class for loading cached resources
abstract class Cache<T extends CacheAsset> {
final Map<String, T> _assets = {};
final Map<AssetProvider, T> _assets = {};
final Set<T> _toPrune = Set<T>();
Timer _pruneTimer;

Expand All @@ -16,7 +18,7 @@ abstract class Cache<T extends CacheAsset> {

void _prune() {
for (final T asset in _toPrune) {
_assets.removeWhere((String filename, T cached) {
_assets.removeWhere((AssetProvider assetProvider, T cached) {
return cached == asset;
});
}
Expand All @@ -39,34 +41,27 @@ abstract class Cache<T extends CacheAsset> {
}

/// Get an asset from the cache or load it.
Future<T> getAsset(String filename) async {
T asset = _assets[filename];
Future<T> getAsset(AssetProvider assetProvider) async {
T asset = _assets[assetProvider];
if (asset != null) {
if (asset.isAvailable) {
return asset;
} else {
return await asset.onLoaded() as T;
}
}
int lastDot = filename.lastIndexOf(".");
if (lastDot != -1) {
asset = makeAsset();
if (asset != null) {
_assets[filename] = asset;
asset.load(this, filename);
if (asset.isAvailable) {
return asset;
} else {
return await asset.onLoaded() as T;
}
}
}
return asset;

asset = makeAsset();
assert(asset != null);

_assets[assetProvider] = asset;
asset.load(this, assetProvider);
return asset.isAvailable ? asset : await asset.onLoaded() as T;
}

/// Get an asset from the cache.
T getWarmAsset(String filename) {
T asset = _assets[filename];
T getWarmAsset(AssetProvider assetProvider) {
T asset = _assets[assetProvider];
return (asset?.isAvailable ?? false) ? asset : null;
}
}
6 changes: 3 additions & 3 deletions flare_flutter/lib/cache_asset.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'dart:async';

import 'asset_provider.dart';
import 'cache.dart';

/// A reference counted asset in a cache.
Expand Down Expand Up @@ -32,9 +34,7 @@ abstract class CacheAsset {
return completer.future;
}

void load(Cache cache, String filename) {
_cache = cache;
}
void load(Cache cache, AssetProvider assetProvider) => _cache = cache;

void completeLoad() {
if (_callbacks != null) {
Expand Down
83 changes: 65 additions & 18 deletions flare_flutter/lib/flare_actor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,29 @@ import 'package:flutter/rendering.dart';
import 'package:flare_dart/actor_drawable.dart';
import 'package:flare_dart/math/mat2d.dart';
import 'package:flare_dart/math/aabb.dart';
import 'package:flutter/services.dart';

import 'asset_provider.dart';
import 'flare.dart';
import 'flare_controller.dart';
import 'provider/asset_flare.dart';

typedef void FlareCompletedCallback(String name);

/// A widget that displays a Flare.
///
/// Several constructors are provided for the various ways that a Flare can be
/// specified:
/// * [FlareActor], for obtaining a Flare from an asset [filename].
/// * [FlareActor.asset], for obtaining a Flare from an [AssetBundle]
/// using a key.
class FlareActor extends LeafRenderObjectWidget {
/// Name of the Flare file to be loaded from the AssetBundle.
final String filename;

/// The Flare asset to display.
final AssetProvider flareProvider;

/// The name of the artboard to display.
final String artboard;

Expand Down Expand Up @@ -72,13 +86,46 @@ class FlareActor extends LeafRenderObjectWidget {
this.shouldClip = true,
this.sizeFromArtboard = false,
this.artboard,
});
}) : flareProvider = null;

FlareActor.rootBundle(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ybbaigo I changed the syntax slightly here so you can do FlareActor.rootBundle('assets/my.flr') and FlareActor.asset(AssetFlare(...)). You can see how I use it in example/simple/lib/main.dart

String name, {
this.boundsNode,
this.animation,
this.fit = BoxFit.contain,
this.alignment = Alignment.center,
this.isPaused = false,
this.snapToEnd = false,
this.controller,
this.callback,
this.color,
this.shouldClip = true,
this.sizeFromArtboard = false,
this.artboard,
}) : filename = null,
flareProvider = AssetFlare(bundle: rootBundle, name: name);

const FlareActor.asset(
this.flareProvider, {
this.boundsNode,
this.animation,
this.fit = BoxFit.contain,
this.alignment = Alignment.center,
this.isPaused = false,
this.snapToEnd = false,
this.controller,
this.callback,
this.color,
this.shouldClip = true,
this.sizeFromArtboard = false,
this.artboard,
}) : filename = null;

@override
RenderObject createRenderObject(BuildContext context) {
return FlareActorRenderObject()
..assetBundle = DefaultAssetBundle.of(context)
..filename = filename
..assetProvider =
flareProvider ?? AssetFlare(bundle: rootBundle, name: filename)
..fit = fit
..alignment = alignment
..animationName = animation
Expand All @@ -97,8 +144,8 @@ class FlareActor extends LeafRenderObjectWidget {
void updateRenderObject(
BuildContext context, covariant FlareActorRenderObject renderObject) {
renderObject
..assetBundle = DefaultAssetBundle.of(context)
..filename = filename
..assetProvider =
flareProvider ?? AssetFlare(bundle: rootBundle, name: filename)
..fit = fit
..alignment = alignment
..animationName = animation
Expand Down Expand Up @@ -131,7 +178,7 @@ class FlareAnimationLayer {

class FlareActorRenderObject extends FlareRenderBox {
Mat2D _lastControllerViewTransform;
String _filename;
AssetProvider _assetProvider;
String _artboardName;
String _animationName;
String _boundsNodeName;
Expand Down Expand Up @@ -248,14 +295,14 @@ class FlareActorRenderObject extends FlareRenderBox {
_animationLayers.clear();
}

String get filename => _filename;
set filename(String value) {
if (value == _filename) {
AssetProvider get assetProvider => _assetProvider;
set assetProvider(AssetProvider value) {
if (value == _assetProvider) {
return;
}
_filename = value;
_assetProvider = value;

if (_filename == null) {
if (_assetProvider == null) {
markNeedsPaint();
}
// file will change, let's clear out old animations.
Expand Down Expand Up @@ -298,28 +345,28 @@ class FlareActorRenderObject extends FlareRenderBox {

@override
bool get canLoad {
return super.canLoad && _filename != null;
return super.canLoad && _assetProvider != null;
}

/// Attempt a warm load, thfis is the optimal case when the
/// Attempt a warm load, this is the optimal case when the
/// required asset is already in the cache.
@override
bool warmLoad() {
if (_filename == null) {
if (_assetProvider == null) {
return false;
}
_actor = getWarmFlare(_filename);
_actor = getWarmFlare(_assetProvider);
return _instanceArtboard();
}

/// Load the necessary Flare file specified by _filename.
/// Load the necessary Flare file specified by [AssetProvider].
/// this occurs when the optimal warmLoad fails to find an asset in cache.
@override
Future<void> coldLoad() async {
if (_filename == null) {
if (_assetProvider == null) {
return;
}
_actor = await loadFlare(_filename);
_actor = await loadFlare(_assetProvider);
_instanceArtboard();
}

Expand Down
32 changes: 11 additions & 21 deletions flare_flutter/lib/flare_cache.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import 'dart:async';
import 'package:flutter/services.dart';

import 'asset_bundle_cache.dart';
import 'asset_provider.dart';
import 'cache.dart';
import 'flare_cache_asset.dart';

/// Cache that instances flare assets from ".flr" extension.
class FlareCache extends AssetBundleCache<FlareCacheAsset> {
FlareCache(AssetBundle bundle) : super(bundle);

/// Cache that instances Flare assets from ".flr" extension.
class FlareCache extends Cache<FlareCacheAsset> {
static bool doesPrune = true;
static Duration pruneDelay = const Duration(seconds: 2);

Expand All @@ -18,24 +16,16 @@ class FlareCache extends AssetBundleCache<FlareCacheAsset> {
Duration get pruneAfter => pruneDelay;

@override
FlareCacheAsset makeAsset() {
return FlareCacheAsset();
}
FlareCacheAsset makeAsset() => FlareCacheAsset();
}

/// A mapping of loaded Flare assets.
final Map<AssetBundle, FlareCache> _cache = {};
/// Cache for loaded Flare assets.
final _cache = FlareCache();

/// Get a cached Flare actor, or load it if it's not yet available.
Future<FlareCacheAsset> cachedActor(AssetBundle bundle, String filename) async {
FlareCache cache = _cache[bundle];
if (cache == null) {
_cache[bundle] = cache = FlareCache(bundle);
}
return cache.getAsset(filename);
}
Future<FlareCacheAsset> cachedActor(AssetProvider assetProvider) =>
_cache.getAsset(assetProvider);

/// Get a warm Flare actor that's already in the cache.
FlareCacheAsset getWarmActor(AssetBundle bundle, String filename) {
return _cache[bundle]?.getWarmAsset(filename);
}
FlareCacheAsset getWarmActor(AssetProvider assetProvider) =>
_cache.getWarmAsset(assetProvider);
Loading