-
-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* fix: #1478 - additional cache folder for tintable svg files New files: * `arrow-top-right-thick.svg`: (tintable) optimized version of https://static.openfoodfacts.org/images/icons/dist/arrow-top-right-thick.svg * `vegetarian.svg`: (tintable) version of vegetarian.svg, taken from https://static.openfoodfacts.org/images/icons/dist/vegetarian.svg and optimized Moved files (from `assets/cache` to `assets/cacheTintable`): * `activity-walking.svg` * `agriculture.svg` * `arrow-bottom-right-thick.svg` * `car.svg` * `egg.svg` * `leaf.svg` * `monkey_happy.svg` * `monkey_uncertain.svg` * `monkey_unhappy.svg` * `packaging.svg` * `palm-oil.svg` * `public.svg` * `scale-balance.svg` * `transportation.svg` Impacted files: * `abstract_cache.dart`: refactored with new protected method `getFilename` * `pubspec.yaml`: added an asset folder for tintable svg cached files * `svg_async_asset.svg`: added comments about the new asset folder * `svg_cache.dart`: now we use 2 cache folders - one for colored icons, one for tintable icons that need a color as parameter * fix: #1478 - several asset cache folders Deleted file: * `abstract_async_asset.dart` New file: * `asset_cache_helper.dart`: Asset cache helper class Impacted files: * `abstract_cache.dart`: now we deal with several possible asset cache files (folders) * `raster_async_asset.svg`: now a `StatefulWidget` that uses new class `AssetCacheHelper` * `raster_cache.svg`: now uses new class `AssetCacheHelper` * `svg_async_asset.svg`: now a `StatefulWidget` that uses new class `AssetCacheHelper` * `svg_cache.dart`: now uses new class `AssetCacheHelper`
- Loading branch information
1 parent
b333d73
commit a3a02d1
Showing
24 changed files
with
167 additions
and
83 deletions.
There are no files selected for viewing
File renamed without changes
File renamed without changes
File renamed without changes
1 change: 1 addition & 0 deletions
1
packages/smooth_app/assets/cacheTintable/arrow-top-right-thick.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 0 additions & 28 deletions
28
packages/smooth_app/lib/cards/category_cards/abstract_async_asset.dart
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/smooth_app/lib/cards/category_cards/asset_cache_helper.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
/// Asset cache helper class | ||
class AssetCacheHelper { | ||
const AssetCacheHelper( | ||
this.cachedFilenames, | ||
this.url, { | ||
this.width, | ||
this.height, | ||
this.color, | ||
}); | ||
|
||
/// Full asset names, e.g. 'assets/cache/ab-agriculture-biologique.74x90.svg' | ||
final List<String> cachedFilenames; | ||
|
||
/// URL (for debug purpose), e.g. https://static.openfoodfacts.org/images/lang/fr/labels/ab-agriculture-biologique.74x90.svg | ||
final String url; | ||
|
||
final double? width; | ||
final double? height; | ||
final Color? color; | ||
|
||
Widget getEmptySpace() => SizedBox( | ||
width: width ?? height, | ||
height: height ?? width, | ||
); | ||
|
||
void notFound() => | ||
debugPrint('unexpected case: asset not found $cachedFilenames ($url)'); | ||
|
||
Exception loadException() => | ||
Exception('could not load any cached file ($cachedFilenames)'); | ||
} |
47 changes: 29 additions & 18 deletions
47
packages/smooth_app/lib/cards/category_cards/raster_async_asset.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,49 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/services.dart'; | ||
import 'package:smooth_app/cards/category_cards/abstract_async_asset.dart'; | ||
import 'package:smooth_app/cards/category_cards/asset_cache_helper.dart'; | ||
|
||
/// Widget with async load of raster asset file (png, jpeg). | ||
class RasterAsyncAsset extends AbstractAsyncAsset { | ||
const RasterAsyncAsset( | ||
final String fullFilename, | ||
final String url, { | ||
final double? width, | ||
final double? height, | ||
}) : super( | ||
fullFilename, | ||
url, | ||
width: width, | ||
height: height, | ||
); | ||
class RasterAsyncAsset extends StatefulWidget { | ||
const RasterAsyncAsset(this.assetCacheHelper); | ||
|
||
final AssetCacheHelper assetCacheHelper; | ||
|
||
@override | ||
State<RasterAsyncAsset> createState() => _RasterAsyncAssetState(); | ||
} | ||
|
||
class _RasterAsyncAssetState extends State<RasterAsyncAsset> { | ||
late final Future<ByteData> _loading = _load(); | ||
|
||
Future<ByteData> _load() { | ||
for (final String cachedFilename | ||
in widget.assetCacheHelper.cachedFilenames) { | ||
try { | ||
return rootBundle.load(cachedFilename); | ||
} catch (e) { | ||
// | ||
} | ||
} | ||
throw widget.assetCacheHelper.loadException(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) => FutureBuilder<ByteData>( | ||
future: rootBundle.load(fullFilename), | ||
future: _loading, | ||
builder: (BuildContext context, AsyncSnapshot<ByteData> snapshot) { | ||
if (snapshot.connectionState == ConnectionState.done) { | ||
if (snapshot.data != null) { | ||
return Image.memory( | ||
snapshot.data!.buffer.asUint8List(), | ||
width: width, | ||
height: height, | ||
width: widget.assetCacheHelper.width, | ||
height: widget.assetCacheHelper.height, | ||
fit: BoxFit.contain, | ||
); | ||
} else { | ||
notFound(); | ||
widget.assetCacheHelper.notFound(); | ||
} | ||
} | ||
return getEmptySpace(); | ||
return widget.assetCacheHelper.getEmptySpace(); | ||
}, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 38 additions & 23 deletions
61
packages/smooth_app/lib/cards/category_cards/svg_async_asset.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,63 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/services.dart'; | ||
import 'package:flutter_svg/flutter_svg.dart'; | ||
import 'package:smooth_app/cards/category_cards/abstract_async_asset.dart'; | ||
import 'package:smooth_app/cards/category_cards/asset_cache_helper.dart'; | ||
|
||
/// Widget with async load of SVG asset file | ||
/// | ||
/// SVG files may need to be optimized before being stored in the cache folder. | ||
/// SVG files may need to be optimized before being stored in the cache folders. | ||
/// There are two cache folders: | ||
/// * assets/cache, where most files should be put | ||
/// * assets/cacheTintable, where only colorless files should be put | ||
/// As an example, vegetarian.svg is in both folders: | ||
/// * the assets/cache version has different colors - no color should be applied | ||
/// * the assets/cacheTintable version works with a color applied to it | ||
/// E.g. with https://jakearchibald.github.io/svgomg/ | ||
/// C.f. https://github.com/openfoodfacts/smooth-app/issues/52 | ||
class SvgAsyncAsset extends AbstractAsyncAsset { | ||
const SvgAsyncAsset( | ||
final String fullFilename, | ||
final String url, { | ||
final double? width, | ||
final double? height, | ||
this.color, | ||
}) : super( | ||
fullFilename, | ||
url, | ||
width: width, | ||
height: height, | ||
); | ||
class SvgAsyncAsset extends StatefulWidget { | ||
const SvgAsyncAsset(this.assetCacheHelper); | ||
|
||
final Color? color; | ||
final AssetCacheHelper assetCacheHelper; | ||
|
||
@override | ||
State<SvgAsyncAsset> createState() => _SvgAsyncAssetState(); | ||
} | ||
|
||
class _SvgAsyncAssetState extends State<SvgAsyncAsset> { | ||
late final Future<String> _loading = _load(); | ||
|
||
Future<String> _load() async { | ||
for (final String cachedFilename | ||
in widget.assetCacheHelper.cachedFilenames) { | ||
try { | ||
return await rootBundle.loadString(cachedFilename); | ||
} catch (e) { | ||
// | ||
} | ||
} | ||
throw widget.assetCacheHelper.loadException(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) => FutureBuilder<String>( | ||
future: rootBundle.loadString(fullFilename), | ||
future: _loading, | ||
builder: (BuildContext context, AsyncSnapshot<String> snapshot) { | ||
if (snapshot.connectionState == ConnectionState.done) { | ||
if (snapshot.data != null) { | ||
return SvgPicture.string( | ||
snapshot.data!, | ||
width: width, | ||
height: height, | ||
color: color, | ||
width: widget.assetCacheHelper.width, | ||
height: widget.assetCacheHelper.height, | ||
color: widget.assetCacheHelper.color, | ||
fit: BoxFit.contain, | ||
placeholderBuilder: (BuildContext context) => getEmptySpace(), | ||
placeholderBuilder: (BuildContext context) => | ||
widget.assetCacheHelper.getEmptySpace(), | ||
); | ||
} else { | ||
notFound(); | ||
widget.assetCacheHelper.notFound(); | ||
} | ||
} | ||
return getEmptySpace(); | ||
return widget.assetCacheHelper.getEmptySpace(); | ||
}, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters