57 lines
1.3 KiB
Dart
57 lines
1.3 KiB
Dart
import 'dart:ui' as ui;
|
|
|
|
import 'package:dartterm/gen/generator.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
class Assets {
|
|
final _Table<ui.Image> _images = _Table((name) async {
|
|
final assetImageByteData = await rootBundle.load(name);
|
|
final codec =
|
|
await ui.instantiateImageCodec(assetImageByteData.buffer.asUint8List());
|
|
final image = (await codec.getNextFrame()).image;
|
|
|
|
return image;
|
|
});
|
|
|
|
final _Table<Vaults> _vaults = _Table(Vaults.load);
|
|
|
|
ui.Image? getImageIfAvailable(String name) {
|
|
return _images.getIfAvailable(name);
|
|
}
|
|
|
|
Vaults? getVaultsIfAvailable(String name) {
|
|
return _vaults.getIfAvailable(name);
|
|
}
|
|
}
|
|
|
|
class _Table<T> {
|
|
final Map<String, T> _loaded = {};
|
|
final Set<String> _waiting = {};
|
|
final Future<T> Function(String) _loadAsync;
|
|
|
|
_Table(this._loadAsync);
|
|
|
|
T? getIfAvailable(String name) {
|
|
if (!_waiting.contains(name)) {
|
|
_waiting.add(name);
|
|
_spawnLoadAsync(name);
|
|
}
|
|
return _loaded[name];
|
|
}
|
|
|
|
Future<void> _spawnLoadAsync(String name) async {
|
|
final asset = await _loadAsync(name);
|
|
_loaded[name] = asset;
|
|
}
|
|
}
|
|
|
|
final assets = Assets();
|
|
|
|
ui.Image? getImageIfAvailable(String name) {
|
|
return assets.getImageIfAvailable(name);
|
|
}
|
|
|
|
Vaults? getVaultsIfAvailable(String name) {
|
|
return assets.getVaultsIfAvailable(name);
|
|
}
|