Created
April 3, 2024 10:18
-
-
Save nkenna/b12905ed1b286e7d7c9d898c63669001 to your computer and use it in GitHub Desktop.
Download story
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
downloadAudioFile(MuseumObject object) async { | |
var size = object.media!.mediaSize ?? 0; | |
var fileSize = (object.media!.mediaSize! / 1000000).toStringAsPrecision(3); | |
var result = await Get.defaultDialog( | |
title: '', | |
middleText: | |
'You are about to download ${fileSize}MB of data. Do you want to continue?', | |
onCancel: () { | |
Navigator.of(context).pop(false); | |
}, | |
onConfirm: () { | |
Get.back(result: true); | |
}, | |
buttonColor: Color(0xffFC0606), | |
confirmTextColor: Colors.white, | |
cancelTextColor: Color(0xffFC0606), | |
); | |
if (result != null && result == true) { | |
PermissionStatus status = await Permission.storage.request(); | |
print('is status granted: ${status.isGranted}'); | |
if (status.isGranted) { | |
var dir = await getApplicationDocumentsDirectory(); | |
print(dir); | |
String? filePath = '${dir.path}/${object.id}-object.mp3'; | |
print(filePath); | |
// check if invoice exist before downloading | |
bool isExist = await checkIfFileExist(filePath); | |
print('does file exist:: ${isExist}'); | |
if (!isExist) { | |
final response = await _downloadObject(object); | |
if (response == null) { | |
return; | |
} | |
if (response.statusCode == 200) { | |
// add to downloads list | |
object.filePath = filePath; | |
object.fileName = '${object.id}-object.mp3'; | |
//Provider.of<MuseumProvider>(context, listen: false). | |
List<MuseumObject>? objects = | |
await SharedPrefs.instance.retrieveSavedObjects(); | |
objects ??= []; | |
objects.add(object); | |
SharedPrefs.instance.saveDownload(objects); | |
return; | |
} | |
} else { | |
/*object.filePath = filePath; | |
object.fileName = '${object.id}-object.mp3'; | |
//Provider.of<MuseumProvider>(context, listen: false). | |
List<MuseumObject>? objects = await SharedPrefs.instance.retrieveSavedObjects(); | |
objects ??= []; | |
objects.add(object); | |
SharedPrefs.instance.saveDownload(objects);*/ | |
} | |
} | |
} | |
} | |
Future<dynamic> _downloadObject(MuseumObject object) async { | |
di.Dio dio = di.Dio(); | |
try { | |
var dir = await getApplicationDocumentsDirectory(); | |
print(dir); | |
showInfoToast('Downloading file. Please wait...'); | |
di.Response response = await dio.download( | |
object.media!.audioUrl!, '${dir.path}/${object.id}-object.mp3', | |
onReceiveProgress: (rec, total) { | |
setState(() { | |
_downloading = true; | |
_progress = ((rec / total) * 100).toInt(); | |
//_progressString = ((rec / total) * 100).toStringAsFixed(0) + "%"; | |
print('download progress:::: $_progress'); | |
if (_progress == 100) { | |
showInfoToast('file download done.'); | |
_progress = 0; | |
_downloading = false; | |
/*if (_progressString == "100%") { | |
Scaffold.of(context) | |
.showSnackBar(SnackBar(content: Text("Next Action..."))); | |
// NextAction();*/ | |
} | |
}); | |
}); | |
return response; | |
} catch (exp) { | |
showErrorToast(exp.toString()); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment