import 'comicland/comicland_source.dart'; import 'mangaread/mangaread_source.dart'; import 'mangatown/mangatown_source.dart'; import 'source.dart'; import 'source_flags.dart'; class SourceRegistry { SourceRegistry({ Set? enabled, ComicSource? comicLand, ComicSource? mangaRead, ComicSource? mangaTown, }) : _sources = _build( enabled ?? enabledSourceIds(), comicLand: comicLand, mangaRead: mangaRead, mangaTown: mangaTown, ); final Map _sources; static Map _build( Set enabled, { ComicSource? comicLand, ComicSource? mangaRead, ComicSource? mangaTown, }) { final map = {}; for (final id in enabled) { switch (id) { case SourceId.comicland: map[id] = comicLand ?? ComicLandSource(); case SourceId.mangaread: map[id] = mangaRead ?? MangaReadSource(); case SourceId.mangatown: map[id] = mangaTown ?? MangaTownSource(); } } if (map.isEmpty) { map[SourceId.comicland] = comicLand ?? ComicLandSource(); } return map; } List get all { // Stable order matching SourceId declaration. return SourceId.values .where(_sources.containsKey) .map((id) => _sources[id]!) .toList(growable: false); } bool get hasMultiple => _sources.length > 1; ComicSource get(SourceId id) { final s = _sources[id]; if (s == null) { throw StateError('Source not enabled in this build: $id'); } return s; } bool isEnabled(SourceId id) => _sources.containsKey(id); ComicSource get defaultSource => all.first; }