From 995d44551cd020d143ed2cb2180c90ff5ebd14eb Mon Sep 17 00:00:00 2001 From: Imran Remtulla Date: Sun, 23 Jul 2023 15:02:58 -0400 Subject: [PATCH] Auto retry failed download (3 times) (#634) --- lib/main.dart | 2 +- lib/providers/apps_provider.dart | 23 +++++++++++++++++++++-- pubspec.yaml | 2 +- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 5e22d51..51c252c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -21,7 +21,7 @@ import 'package:easy_localization/src/easy_localization_controller.dart'; // ignore: implementation_imports import 'package:easy_localization/src/localization.dart'; -const String currentVersion = '0.13.20'; +const String currentVersion = '0.13.21'; const String currentReleaseTag = 'v$currentVersion-beta'; // KEEP THIS IN SYNC WITH GITHUB RELEASES diff --git a/lib/providers/apps_provider.dart b/lib/providers/apps_provider.dart index 6cf8507..7bd83be 100644 --- a/lib/providers/apps_provider.dart +++ b/lib/providers/apps_provider.dart @@ -144,6 +144,24 @@ class AppsProvider with ChangeNotifier { }(); } + Future downloadFileWithRetry( + String url, String fileNameNoExt, Function? onProgress, + {bool useExisting = true, + Map? headers, + int retries = 3}) async { + try { + return await downloadFile(url, fileNameNoExt, onProgress, + useExisting: useExisting, headers: headers); + } catch (e) { + if (retries > 0 && e is ClientException) { + return await downloadFileWithRetry(url, fileNameNoExt, onProgress, + useExisting: useExisting, headers: headers, retries: (retries - 1)); + } else { + rethrow; + } + } + } + Future downloadFile( String url, String fileNameNoExt, Function? onProgress, {bool useExisting = true, Map? headers}) async { @@ -236,8 +254,9 @@ class AppsProvider with ChangeNotifier { notificationsProvider?.cancel(notif.id); int? prevProg; var fileNameNoExt = '${app.id}-${downloadUrl.hashCode}'; - var downloadedFile = await downloadFile(downloadUrl, fileNameNoExt, - headers: source.requestHeaders, (double? progress) { + var downloadedFile = await downloadFileWithRetry( + downloadUrl, fileNameNoExt, headers: source.requestHeaders, + (double? progress) { int? prog = progress?.ceil(); if (apps[app.id] != null) { apps[app.id]!.downloadProgress = progress; diff --git a/pubspec.yaml b/pubspec.yaml index 801a7d9..9cdc749 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -17,7 +17,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 0.13.20+184 # When changing this, update the tag in main() accordingly +version: 0.13.21+185 # When changing this, update the tag in main() accordingly environment: sdk: '>=2.18.2 <3.0.0'