|
|
|
|
@ -1,8 +1,12 @@
|
|
|
|
|
import 'dart:io';
|
|
|
|
|
import 'dart:typed_data';
|
|
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
|
import 'package:tangheem/exceptions/api_exception.dart';
|
|
|
|
|
import 'package:tangheem/ui/dialogs/loading_dialog.dart';
|
|
|
|
|
|
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
|
import 'colors.dart';
|
|
|
|
|
|
|
|
|
|
class Utils {
|
|
|
|
|
@ -11,8 +15,7 @@ class Utils {
|
|
|
|
|
static bool get isLoading => _isLoadingVisible;
|
|
|
|
|
|
|
|
|
|
static void showToast(String message) {
|
|
|
|
|
Fluttertoast.showToast(
|
|
|
|
|
msg: message, toastLength: Toast.LENGTH_LONG, gravity: ToastGravity.BOTTOM, timeInSecForIosWeb: 1, backgroundColor: Colors.black54, textColor: Colors.white, fontSize: 16.0);
|
|
|
|
|
Fluttertoast.showToast(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) {
|
|
|
|
|
@ -75,4 +78,108 @@ class Utils {
|
|
|
|
|
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("تم التنزيل");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|