load static method

Returns the loaded data store (cached after first call).

Implementation

static Future<QpcV4AssetsStore> load() async {
  if (_cachedStore != null) return _cachedStore!;

  final ayahInfoDecoded = await _loadGzippedJson(_ayahInfoGzPath);
  if (ayahInfoDecoded is! List) {
    throw const FormatException('qpc_v4_ayah_info.json must be a JSON List');
  }

  final linesByPage = <int, List<QpcV4AyahInfoLine>>{};
  for (final item in ayahInfoDecoded) {
    if (item is! Map) continue;
    final line = QpcV4AyahInfoLine.fromJson(Map<String, dynamic>.from(item));
    (linesByPage[line.pageNumber] ??= <QpcV4AyahInfoLine>[]).add(line);
  }
  for (final entry in linesByPage.entries) {
    entry.value.sort((a, b) => a.lineNumber.compareTo(b.lineNumber));
  }

  final wordsDecoded = await _loadGzippedJson(_wordsGzPath);
  if (wordsDecoded is! Map) {
    throw const FormatException('qpc-v4.json must be a JSON Map');
  }

  final wordsById = <int, QpcV4Word>{};
  for (final value in wordsDecoded.values) {
    if (value is! Map) continue;
    final word = QpcV4Word.fromJson(Map<String, dynamic>.from(value));
    wordsById[word.id] = word;
  }

  _cachedStore = QpcV4AssetsStore(
    wordsById: wordsById,
    linesByPage: linesByPage,
  );
  return _cachedStore!;
}