code improvements.

development
Sikander Saleem 5 years ago
parent c70252c613
commit 1e42ba6bd7

@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart';
import 'package:http/io_client.dart';
import 'package:tangheem/exceptions/api_exception.dart';
@ -60,14 +61,15 @@ class ApiClient {
if (headers != null && headers.isNotEmpty) {
_headers.addAll(headers);
}
print("Url:$url");
print("body:$jsonObject");
if (!kReleaseMode) {
print("Url:$url");
print("body:$jsonObject");
}
var response = await postJsonForResponse(url, jsonObject, token: token, queryParameters: queryParameters, headers: _headers, retryTimes: retryTimes);
try {
var jsonData = jsonDecode(response.body);
return factoryConstructor(jsonData);
} catch (ex, tr) {
print('${ex.runtimeType}\n$ex\n$tr');
throw APIException(APIException.BAD_RESPONSE_FORMAT, arguments: ex);
}
}

@ -51,6 +51,5 @@ class BookMarkModel {
List encodedList = list.map((e) => jsonEncode(e.toJson())).toList();
var abc = await prefs.setStringList(GlobalConsts.bookmark, encodedList);
print("saveToPrefs:$abc");
}
}

@ -38,7 +38,6 @@ class _CountrySelectionBottomSheetState extends State<CountrySelectionBottomShee
void _onTextChange() {
var _searchText = _searchCountryController.text;
print("_searchText:$_searchText");
_filterList(_searchText);
}

@ -54,7 +54,6 @@ class _HomeScreenState extends State<HomeScreen> {
setState(() {});
Utils.hideLoading(context);
} catch (ex, tr) {
print(ex);
Utils.hideLoading(context);
Utils.handleException(ex, null);
}

@ -13,7 +13,6 @@ import 'package:tangheem/models/aya_model.dart';
import 'package:tangheem/models/aya_tangheem_type_mapped.dart';
import 'package:tangheem/models/bookmark_model.dart';
import 'package:tangheem/models/surah_model.dart';
import 'package:tangheem/widgets/auto_scroll_view/scroll_id.dart';
import 'package:tangheem/widgets/common_dropdown_button.dart';
import 'tangheem_detail_screen.dart';
@ -53,7 +52,6 @@ class _QuranScreenState extends State<QuranScreen> {
@override
void initState() {
super.initState();
scrollToId = ScrollToId(scrollController: scrollController);
getBookMark();
getPrefs();
getSurah();
@ -133,7 +131,6 @@ class _QuranScreenState extends State<QuranScreen> {
Utils.hideLoading(context);
}
getTangheemBySurahId();
scrollToId.animateTo("$_currentPage", duration: Duration(milliseconds: 300), curve: Curves.ease);
}
void getQuranByPageNo() async {
@ -146,7 +143,6 @@ class _QuranScreenState extends State<QuranScreen> {
Utils.handleException(ex, null);
Utils.hideLoading(context);
}
scrollToId.animateTo("$_currentPage", duration: Duration(milliseconds: 300), curve: Curves.ease);
}
int getNextMultiple(int num) {
@ -161,9 +157,6 @@ class _QuranScreenState extends State<QuranScreen> {
super.dispose();
}
final ScrollController scrollController = ScrollController();
ScrollToId scrollToId;
String getBismillahWithSurahName(int _surahID, String _surahName, bool isShowBismillah, bool isFirstIsAya) {
String _bismillah = "\n بِسۡمِ ٱللَّهِ ٱلرَّحۡمَٰنِ ٱلرَّحِيم";
if (_surahID == 9) {
@ -242,7 +235,6 @@ class _QuranScreenState extends State<QuranScreen> {
if (_selectedFromAya != index) {
_selectedFromAya = index;
var abc = filteredAyahList.indexOf(_selectedFromAya);
print("index:$abc");
_toAyaList = filteredAyahList.getRange(_selectedFromAya + 1, filteredAyahList.length)?.toList() ?? [];
setState(() {});
}

@ -96,7 +96,6 @@ class _TangheemDetailScreenState extends State<TangheemDetailScreen> {
Utils.hideLoading(context);
Navigator.pop(context);
} catch (ex, tr) {
print(ex);
Utils.handleException(ex, null);
Utils.hideLoading(context);
}

@ -1,78 +0,0 @@
import 'package:flutter/material.dart';
import 'package:tangheem/widgets/auto_scroll_view/scroll_id.dart';
class AyaScrollViewer extends StatefulWidget {
final ScrollToId scrollToId;
final List<ScrollContent> children;
final Axis scrollDirection;
AyaScrollViewer({@required this.children, @required this.scrollToId, this.scrollDirection = Axis.vertical});
@override
_InteractiveScrollViewerState createState() => _InteractiveScrollViewerState();
}
class _InteractiveScrollViewerState extends State<AyaScrollViewer> {
List<String> _idList = [];
@override
void initState() {
super.initState();
setData();
}
void setData() {
widget.scrollToId.scrollDirection = widget.scrollDirection;
_idList = [];
widget.scrollToId.scrollContentsList = [];
for (ScrollContent scrollContents in widget.children) {
if (_idList.contains(scrollContents.id)) {
throw Exception('Do not use the same id');
} else {
_idList.add(scrollContents.id);
}
widget.scrollToId.scrollContentsList.add(ScrollContentWithKey.fromWithout(scrollContents));
}
}
@override
void didUpdateWidget(covariant AyaScrollViewer oldWidget) {
if (widget.children != oldWidget.children) {
setData();
}
super.didUpdateWidget(oldWidget);
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
scrollDirection: widget.scrollDirection,
controller: widget.scrollToId.scrollController,
physics: BouncingScrollPhysics(),
child: buildContent(),
);
}
Widget buildContent() {
if (widget.scrollDirection == Axis.vertical) {
return Column(
children: widget.scrollToId.scrollContentsList.map((scrollContents) {
return buildRepaintBoundary(scrollContents);
}).toList(),
);
} else {
return Row(
children: widget.scrollToId.scrollContentsList.map((scrollContents) {
return buildRepaintBoundary(scrollContents);
}).toList(),
);
}
}
RepaintBoundary buildRepaintBoundary(ScrollContentWithKey scrollContents) {
return RepaintBoundary(
key: scrollContents.key,
child: scrollContents.child,
);
}
}

@ -1,136 +0,0 @@
import 'package:flutter/material.dart';
class ScrollToId {
final ScrollController scrollController;
List<ScrollContentWithKey> scrollContentsList = [];
Axis scrollDirection;
ScrollToId({this.scrollController});
String idPosition() {
double sumSize = 0;
for (ScrollContentWithKey scrollContents in scrollContentsList) {
if (scrollDirection == Axis.vertical) {
sumSize += scrollContents.key.currentContext.size.height;
} else {
sumSize += scrollContents.key.currentContext.size.width;
}
if (scrollController.offset < sumSize) {
return scrollContents.id;
}
}
return null;
}
Future<void> animateTo(String id, {@required Duration duration, @required Curve curve}) async {
Function _function = (double offset) {
scrollController.animateTo(offset, duration: duration, curve: curve);
};
_scroll(id: id, scrollFunction: _function);
}
Future<void> jumpTo(String id) async {
Function _function = (double offset) {
scrollController.jumpTo(offset);
};
_scroll(id: id, scrollFunction: _function);
}
Future<void> animateToNext({@required Duration duration, @required Curve curve}) async {
Function _function = (double offset) {
scrollController.animateTo(offset, duration: duration, curve: curve);
};
_scroll(id: _getNextId(number: 1), scrollFunction: _function);
}
Future<void> jumpToNext() async {
Function _function = (double offset) {
scrollController.jumpTo(offset);
};
_scroll(id: _getNextId(number: 1), scrollFunction: _function);
}
Future<void> animateToBefore({@required Duration duration, @required Curve curve}) async {
Function _function = (double offset) {
scrollController.animateTo(offset, duration: duration, curve: curve);
};
_scroll(id: _getNextId(number: -1), scrollFunction: _function);
}
Future<void> jumpToBefore() async {
Function _function = (double offset) {
scrollController.jumpTo(offset);
};
_scroll(id: _getNextId(number: -1), scrollFunction: _function);
}
String _getNextId({int number}) {
double sumSize = 0;
for (int i = 0; i < scrollContentsList.length; i++) {
if (scrollDirection == Axis.vertical) {
sumSize += scrollContentsList[i].key.currentContext.size.height;
} else {
sumSize += scrollContentsList[i].key.currentContext.size.width;
}
if (scrollController.offset < sumSize) {
if (number == 1 && i == scrollContentsList.length - 1) return null;
if (number == -1 && i == 0) return null;
return scrollContentsList[i + number].id;
}
}
return null;
}
void _scroll({String id, Function scrollFunction}) {
double sumSize = 0;
for (ScrollContentWithKey scrollContents in scrollContentsList) {
if (scrollContents.id == id) {
try {
if (sumSize < scrollController.position.maxScrollExtent) {
scrollFunction(sumSize);
break;
} else {
scrollFunction(scrollController.position.maxScrollExtent);
break;
}
} catch (e) {
print('$e');
break;
}
} else {
try {
if (scrollDirection == Axis.vertical) {
sumSize += scrollContents.key.currentContext.size.height;
} else {
sumSize += scrollContents.key.currentContext.size.width;
}
} catch (e) {
print('$e');
}
}
}
}
}
class ScrollContent {
final String id;
final Widget child;
ScrollContent({@required this.id, @required this.child});
}
class ScrollContentWithKey {
final Widget child;
final String id;
final GlobalKey key;
ScrollContentWithKey({this.child, this.key, this.id});
factory ScrollContentWithKey.fromWithout(ScrollContent scrollContent) {
return ScrollContentWithKey(child: scrollContent.child, id: scrollContent.id, key: GlobalKey());
}
}
Loading…
Cancel
Save