import 'dart:convert'; import 'package:flutter/services.dart'; /// Represents a single Surah with its verses. class Surah { final int id; final String nameArabic; final String nameLatin; final int verseCount; final int juzStart; final String revelationType; final List verses; Surah({ required this.id, required this.nameArabic, required this.nameLatin, required this.verseCount, this.juzStart = 1, this.revelationType = 'Meccan', this.verses = const [], }); factory Surah.fromJson(Map json) { return Surah( id: json['id'] as int, nameArabic: json['name_arabic'] as String? ?? '', nameLatin: json['name_latin'] as String? ?? '', verseCount: json['verse_count'] as int? ?? 0, juzStart: json['juz_start'] as int? ?? 1, revelationType: json['revelation_type'] as String? ?? 'Meccan', verses: (json['verses'] as List?) ?.map((v) => Verse.fromJson(v as Map)) .toList() ?? [], ); } } /// A single Quran verse. class Verse { final int id; final String arabic; final String? transliteration; final String translationId; Verse({ required this.id, required this.arabic, this.transliteration, required this.translationId, }); factory Verse.fromJson(Map json) { return Verse( id: json['id'] as int, arabic: json['arabic'] as String? ?? '', transliteration: json['transliteration'] as String?, translationId: json['translation_id'] as String? ?? '', ); } } /// Service to load Quran data from bundled JSON asset. class QuranService { QuranService._(); static final QuranService instance = QuranService._(); List? _cachedSurahs; /// Load all 114 Surahs from local JSON. Cached in memory after first load. Future> getAllSurahs() async { if (_cachedSurahs != null) return _cachedSurahs!; try { final jsonString = await rootBundle.loadString('assets/quran/quran_id.json'); final List data = json.decode(jsonString); _cachedSurahs = data .map((s) => Surah.fromJson(s as Map)) .toList(); } catch (_) { _cachedSurahs = []; } return _cachedSurahs!; } /// Get a single Surah by ID. Future getSurah(int id) async { final surahs = await getAllSurahs(); try { return surahs.firstWhere((s) => s.id == id); } catch (_) { return null; } } }