loadQuran method

Future<void> loadQuran({
  1. dynamic quranPages = QuranRepository.hafsPagesNumber,
})

Implementation

Future<void> loadQuran({quranPages = QuranRepository.hafsPagesNumber}) async {
  lastPage = _quranRepository.getLastPage() ?? 1;
  if (lastPage != 0) {
    jumpToPage(lastPage - 1);
  }
  if (staticPages.isEmpty || quranPages != staticPages.length) {
    staticPages.value = List.generate(quranPages,
        (index) => QuranPage(pageNumber: index + 1, ayahs: [], lines: []));
    final quranJson = await _quranRepository.getQuran();
    int hizb = 1;
    int surahsIndex = 1;
    List<AyahModel> thisSurahAyahs = [];
    for (int i = 0; i < quranJson.length; i++) {
      final ayah = AyahModel._fromJson(quranJson[i]);
      if (ayah.surahNumber != surahsIndex) {
        surahs.last.endPage = ayahs.last.page;
        surahs.last.ayahs = thisSurahAyahs;
        surahsIndex = ayah.surahNumber;
        thisSurahAyahs = [];
      }
      ayahs.add(ayah);
      thisSurahAyahs.add(ayah);
      staticPages[ayah.page - 1].ayahs.add(ayah);
      if (ayah.text.contains('۞')) {
        staticPages[ayah.page - 1].hizb = hizb++;
        quranStops.add(ayah.page);
      }
      if (ayah.text.contains('۩')) {
        staticPages[ayah.page - 1].hasSajda = true;
      }
      if (ayah.ayahNumber == 1) {
        ayah.text = ayah.text.replaceAll('۞', '');
        staticPages[ayah.page - 1].numberOfNewSurahs++;
        surahs.add(Surah(
            index: ayah.surahNumber,
            startPage: ayah.page,
            endPage: 0,
            nameEn: ayah.englishName,
            nameAr: ayah.arabicName,
            ayahs: []));
        surahsStart.add(ayah.page - 1);
      }
    }
    surahs.last.endPage = ayahs.last.page;
    surahs.last.ayahs = thisSurahAyahs;
    for (QuranPage staticPage in staticPages) {
      List<AyahModel> ayas = [];
      for (AyahModel aya in staticPage.ayahs) {
        if (aya.ayahNumber == 1 && ayas.isNotEmpty) {
          ayas.clear();
        }
        if (aya.text.contains('\n')) {
          final lines = aya.text.split('\n');
          for (int i = 0; i < lines.length; i++) {
            bool centered = false;
            if ((aya.centered && i == lines.length - 2)) {
              centered = true;
            }
            final a = AyahModel._fromAya(
                ayah: aya,
                aya: lines[i],
                ayaText: lines[i],
                centered: centered);
            ayas.add(a);
            if (i < lines.length - 1) {
              staticPage.lines.add(Line([...ayas]));
              ayas.clear();
            }
          }
        } else {
          ayas.add(aya);
        }
      }
      ayas.clear();
    }
    update();
  }
}