pdf download added.

development-design-2.0
Sikander Saleem 3 years ago
parent d6c2245783
commit d634729615

@ -33,7 +33,7 @@ if (keystorePropertiesFile.exists()) {
} }
android { android {
compileSdkVersion 31 compileSdkVersion 33
sourceSets { sourceSets {
main.java.srcDirs += 'src/main/kotlin' main.java.srcDirs += 'src/main/kotlin'
@ -43,11 +43,11 @@ android {
disable 'InvalidPackage' disable 'InvalidPackage'
} }
defaultConfig { defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.cloudsolutions.tangheem" applicationId "com.cloudsolutions.tangheem"
minSdkVersion 19 minSdkVersion 19
targetSdkVersion 31 targetSdkVersion 33
versionCode 1 versionCode 1
versionName "1.0.0" versionName "1.0.0"
multiDexEnabled true multiDexEnabled true
@ -65,6 +65,11 @@ android {
debug { debug {
signingConfig signingConfigs.debug signingConfig signingConfigs.debug
} }
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
// release { // release {
// minifyEnabled true // minifyEnabled true
// shrinkResources true // shrinkResources true

@ -1,5 +1,5 @@
sdk.dir=/Users/sikandersaleem/Library/Android/sdk sdk.dir=/Users/sikandersaleem/Library/Android/sdk
flutter.sdk=/Users/sikandersaleem/sdks/flutter flutter.sdk=/Users/sikandersaleem/sdks/flutter
flutter.buildMode=debug flutter.buildMode=profile
flutter.versionName=1.0.0 flutter.versionName=1.0.0
flutter.versionCode=1 flutter.versionCode=1

@ -1,8 +1,12 @@
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart'; import 'package:fluttertoast/fluttertoast.dart';
import 'package:path_provider/path_provider.dart';
import 'package:tangheem/exceptions/api_exception.dart'; import 'package:tangheem/exceptions/api_exception.dart';
import 'package:tangheem/ui/dialogs/loading_dialog.dart'; import 'package:tangheem/ui/dialogs/loading_dialog.dart';
import 'package:http/http.dart' as http;
import 'colors.dart'; import 'colors.dart';
class Utils { class Utils {
@ -11,8 +15,7 @@ class Utils {
static bool get isLoading => _isLoadingVisible; static bool get isLoading => _isLoadingVisible;
static void showToast(String message) { static void showToast(String message) {
Fluttertoast.showToast( Fluttertoast.showToast(msg: message, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, timeInSecForIosWeb: 1, backgroundColor: Colors.black54, textColor: Colors.white, fontSize: 16.0);
msg: message, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, timeInSecForIosWeb: 1, backgroundColor: Colors.black54, textColor: Colors.white, fontSize: 16.0);
} }
static dynamic getNotNullValue(List<dynamic> list, int index) { static dynamic getNotNullValue(List<dynamic> list, int index) {
@ -75,4 +78,108 @@ class Utils {
showToast(errorMessage); showToast(errorMessage);
} }
} }
static Future<String> getStoragePath(String fileName) async {
Directory storageDirectory;
if (Platform.isAndroid) {
storageDirectory = await getExternalStorageDirectory();
} else if (Platform.isIOS) {
storageDirectory = await getApplicationDocumentsDirectory();
} else {
return "";
}
String storagePath = storageDirectory.path;
if (storagePath.contains("/Android/data")) {
storagePath = storagePath.substring(0, storagePath.indexOf("/Android/data"));
}
if (Platform.isAndroid) {
storagePath += "/Download/Tangheem/";
} else {
storagePath += "/PDFs/";
}
var d = Directory(storagePath);
if (!d.existsSync()) {
d.createSync(recursive: true);
}
storagePath += fileName;
return storagePath;
}
static void downloadFile(String url, String path, {Function(bool) onResponse}) {
var httpClient = http.Client();
var request = http.Request('GET', Uri.parse(url));
var response = httpClient.send(request);
String dir = path;
List<List<int>> chunks = [];
int downloaded = 0;
int lastVal;
response.asStream().listen((http.StreamedResponse r) {
r.stream.listen((List<int> chunk) {
debugPrint('downloadPercentage: ${downloaded / r.contentLength * 100}');
lastVal = (downloaded / r.contentLength * 100).toInt();
chunks.add(chunk);
downloaded += chunk.length;
}, onDone: () async {
debugPrint('downloadPercentage: ${downloaded / r.contentLength * 100}');
if (lastVal == 0) {
onResponse(false);
return;
}
// Save the file
File file = new File('$dir');
final Uint8List bytes = Uint8List(r.contentLength);
int offset = 0;
for (List<int> chunk in chunks) {
bytes.setRange(offset, offset + chunk.length, chunk);
offset += chunk.length;
}
await file.writeAsBytes(bytes);
onResponse(true);
return;
}, onError: (ex) {
debugPrint("onError:$ex");
}, cancelOnError: true);
}).onError((ex) {
debugPrint("onError:$ex");
onResponse(false);
});
}
void saveToPhoneStorage(String filePath) async {
File file = File(filePath);
Directory storageDirectory;
if (Platform.isAndroid) {
storageDirectory = await getExternalStorageDirectory();
} else if (Platform.isIOS) {
storageDirectory = await getApplicationDocumentsDirectory();
} else {
return;
}
String storagePath = storageDirectory.path;
if (storagePath.contains("/Android/data")) {
storagePath = storagePath.substring(0, storagePath.indexOf("/Android/data"));
}
if (Platform.isAndroid) {
storagePath += "/Download/Tangheem/";
} else {
storagePath += "/Recording/";
}
var d = Directory(storagePath);
if (!d.existsSync()) {
d.createSync(recursive: true);
}
storagePath += "Tangheem${DateTime.now().millisecondsSinceEpoch}.mp3";
print("storagePath:$storagePath");
await file.copy(storagePath);
Utils.showToast("تم التنزيل");
}
} }

@ -33,11 +33,11 @@ class _AboutScreenState extends State<AboutScreen> {
void getContents() async { void getContents() async {
Utils.showLoading(context); Utils.showLoading(context);
try { try {
if (haqooqAlMosasa == null && tareefAlMosasa == null) { if ((haqooqAlMosasa ?? []).isEmpty || (tareefAlMosasa ?? []).isEmpty) {
List<ContentInfoModel> responseList = await Future.wait([TangheemUserApiClient().getContentInfo(2), if (haqooqAlMosasa == null) TangheemUserApiClient().getContentInfo(1)]); List<ContentInfoModel> responseList = await Future.wait([TangheemUserApiClient().getContentInfo(2), if ((haqooqAlMosasa ?? []).isEmpty) TangheemUserApiClient().getContentInfo(1)]);
tareefAlMosasa = responseList[0].data ?? []; tareefAlMosasa = responseList[0].data ?? [];
AppState().tareefAlMosasa = tareefAlMosasa; AppState().tareefAlMosasa = tareefAlMosasa;
if (haqooqAlMosasa == null) { if ((haqooqAlMosasa ?? []).isEmpty) {
haqooqAlMosasa = responseList[1].data ?? []; haqooqAlMosasa = responseList[1].data ?? [];
AppState().haqooqAlMosasa = haqooqAlMosasa; AppState().haqooqAlMosasa = haqooqAlMosasa;
} }

@ -61,6 +61,24 @@ class _PdfListScreenState extends State<PdfListScreen> {
super.dispose(); super.dispose();
} }
void startFileDownload(String url, String fileName) async {
try {
Utils.showLoading(context);
String path = await Utils.getStoragePath(fileName);
Utils.downloadFile(ApiConsts.baseUrl + url, path, onResponse: (isSuccess) {
Utils.hideLoading(context);
if (isSuccess) {
Utils.showToast("تم حفظ الملف بنجاح");
} else {
Utils.showToast("فشل حفظ الملف ، حاول مرة أخرى");
}
});
} catch (ex) {
print(ex);
Utils.hideLoading(context);
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return SizedBox( return SizedBox(
@ -91,7 +109,9 @@ class _PdfListScreenState extends State<PdfListScreen> {
"assets/icons/new/download_pdf.svg", "assets/icons/new/download_pdf.svg",
width: 44, width: 44,
height: 50, height: 50,
), ).onPress(() {
startFileDownload(contentList[index].exposeFilePath, contentList[index].fileName);
}),
7.width, 7.width,
ClipRRect( ClipRRect(
borderRadius: BorderRadius.circular(7), borderRadius: BorderRadius.circular(7),
@ -122,10 +142,10 @@ class _PdfListScreenState extends State<PdfListScreen> {
Directionality( Directionality(
textDirection: TextDirection.rtl, textDirection: TextDirection.rtl,
child: (contentList[index].fileName?.trim() ?? "").toText(18, color: ColorConsts.darkText).paddingOnly(left: 12, right: 12), child: (contentList[index].fileName?.trim() ?? "").toText(18, color: ColorConsts.darkText).paddingOnly(left: 12, right: 12),
), ).expanded,
], ],
), ),
).expanded, ),
).onPress(() { ).onPress(() {
Navigator.pushNamed(context, PdfViewerScreen.routeName, arguments: contentList[index]); Navigator.pushNamed(context, PdfViewerScreen.routeName, arguments: contentList[index]);
}).expanded, }).expanded,

@ -341,7 +341,7 @@ class _AyaPlayerWidgetState extends State<AyaPlayerWidget> {
} }
storagePath += "Tangheem-$name-${DateTime.now().millisecondsSinceEpoch}${filePath.substring(filePath.lastIndexOf('.'), filePath.length)}"; storagePath += "Tangheem-$name-${DateTime.now().millisecondsSinceEpoch}${filePath.substring(filePath.lastIndexOf('.'), filePath.length)}";
Utils.showLoading(context); Utils.showLoading(context);
downloadFile(ApiConsts.baseUrl + filePath, storagePath, onResponse: (isSuccess) { Utils.downloadFile(ApiConsts.baseUrl + filePath, storagePath, onResponse: (isSuccess) {
Utils.hideLoading(context); Utils.hideLoading(context);
if (isSuccess) { if (isSuccess) {
Utils.showToast("تم حفظ الآية بنجاح"); Utils.showToast("تم حفظ الآية بنجاح");

Loading…
Cancel
Save