import 'dart:async'; import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; import 'app_font.dart'; import 'app_font_store.dart'; /// Registers [assets] under [family]. Production reads them from the bundle; /// tests substitute a recorder. typedef AppFontAssetLoader = Future Function(String family, List assets); /// Loads bundled font files on demand with a [FontLoader]. On the web each /// asset is fetched only now, when its family is first previewed or chosen. Future loadBundledFontAssets(String family, List assets) async { final loader = FontLoader(family); for (final asset in assets) { loader.addFont(rootBundle.load(asset)); } await loader.load(); } /// The device's interface font: the stored choice, the family the theme /// actually uses, and the on-demand loading between the two. /// /// The theme keeps the previous family (at first the platform's) until the /// chosen files are registered, then switches once; text never lays out in a /// family that has not loaded, so nothing reflows more than once. class AppFontController extends ChangeNotifier { AppFontController({ this._store = const SharedPreferencesAppFontStore(), this._loader = loadBundledFontAssets, }); /// The app's own controller: one per running app, shared by the theme /// hosts and the Settings picker. static final AppFontController instance = AppFontController(); final AppFontStore _store; final AppFontAssetLoader _loader; final Map> _assetLoads = >{}; final Set _loadedAssets = {}; AppFont _selected = AppFont.system; AppFont _applied = AppFont.system; bool _saveFailed = false; AppFont? _loadFailed; /// The option the picker shows as chosen. AppFont get selected => _selected; /// The option the theme uses now. AppFont get applied => _applied; /// The theme's family; null keeps the platform typography. String? get family => _applied.family; /// The chosen family's files are still downloading. bool get loading => _selected != _applied && _loadFailed != _selected; /// The last choice could not be stored on this device. bool get saveFailed => _saveFailed; /// The chosen family's files could not be loaded; the theme kept [applied]. AppFont? get loadFailed => _loadFailed; /// Whether [font]'s regular weight is registered, so its name can be drawn /// in its own face. bool canPreview(AppFont font) => font.previewAsset == null || _loadedAssets.contains(font.previewAsset); /// Reads the stored choice and starts loading it. With [wait], returns once /// it is applied or after [wait], whichever is first; a slower load still /// applies when it finishes. Future restore({Duration? wait}) async { final font = await _store.load(); if (font == AppFont.system || font == _selected) return; _selected = font; notifyListeners(); final applying = _apply(font); if (wait == null) return applying; await Future.any(>[ applying, Future.delayed(wait), ]); } /// Chooses [font], stores it, and applies it once its files are loaded. Future select(AppFont font) async { if (font == _selected && _loadFailed != font) return; _selected = font; _loadFailed = null; notifyListeners(); final saved = await _store.save(font); if (saved == _saveFailed) { _saveFailed = !saved; notifyListeners(); } await _apply(font); } /// Loads [font]'s regular weight for its picker row. Failures are quiet: the /// row keeps the current face and choosing it reports the problem. Future preview(AppFont font) async { final asset = font.previewAsset; final family = font.family; if (asset == null || family == null || _loadedAssets.contains(asset)) { return; } try { await _ensure(family, [asset]); } catch (_) { return; } notifyListeners(); } Future _apply(AppFont font) async { final family = font.family; if (family != null) { try { await _ensure(family, font.assets); } catch (_) { if (_selected == font) { _loadFailed = font; notifyListeners(); } return; } } // A later choice supersedes this one. if (_selected != font || _applied == font) return; _applied = font; notifyListeners(); } /// Registers the assets not yet loaded or loading, in one [FontLoader], and /// waits for all of [assets]. A failed asset can be retried. Future _ensure(String family, List assets) { final missing = assets.where((a) => !_assetLoads.containsKey(a)).toList(); if (missing.isNotEmpty) { final load = _loader(family, missing); for (final asset in missing) { _assetLoads[asset] = load.then( (_) => _loadedAssets.add(asset), onError: (Object error, StackTrace stack) { _assetLoads.remove(asset); Error.throwWithStackTrace(error, stack); }, ); } } return Future.wait(assets.map((a) => _assetLoads[a]!)); } }