diff --git a/lib/app_state/surah_app_state.dart b/lib/app_state/surah_app_state.dart new file mode 100644 index 0000000..913c93e --- /dev/null +++ b/lib/app_state/surah_app_state.dart @@ -0,0 +1,13 @@ +import 'package:tangheem/models/surah_model.dart'; + +class SurahAppState { + static final SurahAppState _instance = SurahAppState._internal(); + SurahAppState._internal(); + factory SurahAppState() => _instance; + + SurahModel _surahModel; + SurahModel get getSurahModel => _surahModel; + void setSurahModel(SurahModel _surahModel) { + this._surahModel = _surahModel; + } +} diff --git a/lib/ui/screens/home_screen.dart b/lib/ui/screens/home_screen.dart index 2d78933..30b6f29 100644 --- a/lib/ui/screens/home_screen.dart +++ b/lib/ui/screens/home_screen.dart @@ -4,6 +4,7 @@ import 'package:flutter/painting.dart'; import 'package:flutter/services.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:tangheem/api/tangheem_user_api_client.dart'; +import 'package:tangheem/app_state/surah_app_state.dart'; import 'package:tangheem/classes/colors.dart'; import 'package:tangheem/classes/utils.dart'; import 'package:tangheem/models/surah_model.dart'; @@ -47,6 +48,7 @@ class _HomeScreenState extends State { Utils.showLoading(context); try { _surahModel = await TangheemUserApiClient().getSurahs(); + SurahAppState().setSurahModel(_surahModel); _tangheemType = await TangheemUserApiClient().getTangheemType(); _surahList = _surahModel.data.map((element) => element.nameAR).toList(); _tangheemList = _tangheemType?.data?.where((element) => element.isActive)?.toList()?.map((element) => element.tangheemTypeName)?.toList() ?? []; diff --git a/lib/ui/screens/quran_screen.dart b/lib/ui/screens/quran_screen.dart index 4c7b034..0723f21 100644 --- a/lib/ui/screens/quran_screen.dart +++ b/lib/ui/screens/quran_screen.dart @@ -3,6 +3,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:tangheem/api/tangheem_user_api_client.dart'; +import 'package:tangheem/app_state/surah_app_state.dart'; import 'package:tangheem/classes/colors.dart'; import 'package:tangheem/classes/utils.dart'; import 'package:tangheem/models/aya_model.dart'; @@ -18,10 +19,9 @@ import 'tangheem_detail_screen.dart'; class QuranScreen extends StatefulWidget { static const String routeName = "/surah"; - final SurahModel surahModel; final String query; - QuranScreen({Key key, this.surahModel, @required this.query}) : super(key: key); + QuranScreen({Key key, @required this.query}) : super(key: key); @override _QuranScreenState createState() { @@ -37,7 +37,7 @@ class _QuranScreenState extends State { int _currentSurahIndex = -1; int _currentPage = 0; - int _ayaInPage = 5; + List _surahList = []; List _fromAyaList = []; List _toAyaList = []; @@ -56,19 +56,20 @@ class _QuranScreenState extends State { } void getSurah() async { - Utils.showLoading(context); try { - if (widget.surahModel == null) { + if (SurahAppState().getSurahModel == null) { + Utils.showLoading(context); _surahModel = await TangheemUserApiClient().getSurahs(); + SurahAppState().setSurahModel(_surahModel); } else { - _surahModel = widget.surahModel; + _surahModel = SurahAppState().getSurahModel; } _surahList = _surahModel.data.map((element) => element.nameAR).toList(); - + _currentPage = 1; + Utils.hideLoading(context); //filterData(); } catch (ex, tr) { Utils.handleException(ex, null); - } finally { Utils.hideLoading(context); } setState(() {}); @@ -119,11 +120,11 @@ class _QuranScreenState extends State { Utils.showLoading(context); try { // _currentPage = 603; - _ayaModel = await TangheemUserApiClient().getQuranByPageNo(_currentPage + 1); + _ayaModel = await TangheemUserApiClient().getQuranByPageNo(_currentPage); + Utils.hideLoading(context); setState(() {}); } catch (ex, tr) { Utils.handleException(ex, null); - } finally { Utils.hideLoading(context); } scrollToId.animateTo("$_currentPage", duration: Duration(milliseconds: 300), curve: Curves.ease); @@ -161,21 +162,26 @@ class _QuranScreenState extends State { } } + void _clearFilterAndRefreshData() { + _selectedSurah = -1; + _selectedFromAya = -1; + _selectedToAya = -1; + getQuranByPageNo(); + } + @override Widget build(BuildContext context) { - var tempList = _ayaModel?.data?.map((e) => e.surahNameEN)?.toList()?.toSet()?.toList() ?? []; - + var tempList = _ayaModel?.data?.map((e) => e.surahNameAR)?.toList()?.toSet()?.toList() ?? []; String _surahName = ""; _surahName = tempList.isEmpty ? "" : tempList?.last ?? ""; int index = _surahList.indexWhere((element) => element == _surahName); if (index != null) { _currentSurahIndex = index; } - String _surahAya = ""; _ayaModel?.data?.forEach((element) { - var temp = element.numberInSurah == 1 ? getBismillahWithSurahName(element.surahNameEN, element.surahID != 1, _surahAya.length <= 1) + element.ayahText : element.ayahText; + var temp = element.numberInSurah == 1 ? getBismillahWithSurahName(element.surahNameAR, element.surahID != 1, _surahAya.length <= 1) + element.ayahText : element.ayahText; _surahAya = _surahAya + temp; }); @@ -208,10 +214,6 @@ class _QuranScreenState extends State { if (_selectedFromAya != index) { _selectedFromAya = index; setState(() {}); - - /// _selectedToAya = index; - // _currentPage = index; - // getAyaByRange(); } }), ), @@ -221,8 +223,6 @@ class _QuranScreenState extends State { if (_selectedToAya != index) { _selectedToAya = index; setState(() {}); - // _currentPage = index; - // getAyaByRange(); } }), ), @@ -253,10 +253,7 @@ class _QuranScreenState extends State { (_selectedSurah == -1 || _selectedFromAya == -1 || _selectedToAya == -1) ? null : () { - _selectedSurah = -1; - _selectedFromAya = -1; - _selectedToAya = -1; - getQuranByPageNo(); + _clearFilterAndRefreshData(); }), ], ), @@ -280,18 +277,18 @@ class _QuranScreenState extends State { _currentSurahIndex <= 0 ? "" : getNotNullValue(_surahList, _currentSurahIndex - 1), _currentSurahIndex <= 0 ? null - : () { - // _selectedSurah = _selectedSurah - 1; - // filterData(); + : (value) { + _currentPage = _surahModel?.data?.elementAt(_surahList.indexOf(value))?.startPageNo ?? _currentPage; + _clearFilterAndRefreshData(); }), previousOptionButton( "assets/icons/next.svg", _currentSurahIndex >= (_surahList.isNotEmpty ? (_surahList.length - 1) : 0) ? "" : getNotNullValue(_surahList, _currentSurahIndex + 1), _currentSurahIndex >= (_surahList.isNotEmpty ? (_surahList.length - 1) : 0) ? null - : () { - // _selectedSurah = _selectedSurah + 1; - // filterData(); + : (value) { + _currentPage = _surahModel?.data?.singleWhere((element) => element.nameAR == value)?.startPageNo ?? _currentPage; + _clearFilterAndRefreshData(); }), ], ), @@ -372,27 +369,21 @@ class _QuranScreenState extends State { children: [ nextOptionButton( "assets/icons/prev_single.svg", - "الآيات السابقة", - _currentPage == 0 + "الصفحة السابقة", + _currentPage <= 1 ? null - : () { + : (value) { _currentPage = _currentPage - 1; - _selectedSurah = -1; - _selectedFromAya = -1; - _selectedToAya = -1; - getQuranByPageNo(); + _clearFilterAndRefreshData(); }), previousOptionButton( "assets/icons/next_single.svg", - "الآيات التالية", + "الصفحة التالية", _currentPage == 604 ? null - : () { + : (value) { _currentPage = _currentPage + 1; - _selectedSurah = -1; - _selectedFromAya = -1; - _selectedToAya = -1; - getQuranByPageNo(); + _clearFilterAndRefreshData(); }), ], ), @@ -436,9 +427,9 @@ class _QuranScreenState extends State { ); } - Widget nextOptionButton(String icon, String text, VoidCallback onPressed) { + Widget nextOptionButton(String icon, String text, Function(String) onPressed) { return InkWell( - onTap: onPressed, + onTap: () => onPressed(text), child: onPressed == null ? SizedBox() : Row( @@ -486,9 +477,9 @@ class _QuranScreenState extends State { ); } - Widget previousOptionButton(String icon, String text, VoidCallback onPressed) { + Widget previousOptionButton(String icon, String text, Function(String) onPressed) { return InkWell( - onTap: onPressed, + onTap: () => onPressed(text), child: onPressed == null ? SizedBox() : Row(