diff --git a/lib/pages/add_app.dart b/lib/pages/add_app.dart index dafbdc5..450475f 100644 --- a/lib/pages/add_app.dart +++ b/lib/pages/add_app.dart @@ -107,7 +107,8 @@ class _AddAppPageState extends State { throw ObtainiumError(tr('cancelled')); } app.preferredApkIndex = app.apkUrls.indexOf(apkUrl); - var downloadedApk = await appsProvider.downloadApp(app); + // ignore: use_build_context_synchronously + var downloadedApk = await appsProvider.downloadApp(app, context); app.id = downloadedApk.appId; } if (appsProvider.apps.containsKey(app.id)) { diff --git a/lib/providers/apps_provider.dart b/lib/providers/apps_provider.dart index a8542ba..7ade187 100644 --- a/lib/providers/apps_provider.dart +++ b/lib/providers/apps_provider.dart @@ -113,12 +113,16 @@ class AppsProvider with ChangeNotifier { return downloadedFile; } - Future downloadApp(App app) async { + Future downloadApp(App app, BuildContext? context) async { var fileName = '${app.id}-${app.latestVersion}-${app.preferredApkIndex}.apk'; String downloadUrl = await SourceProvider() .getSource(app.url) .apkUrlPrefetchModifier(app.apkUrls[app.preferredApkIndex]); + NotificationsProvider? notificationsProvider = + context?.read(); + var notif = DownloadNotification(app.name, 100); + notificationsProvider?.cancel(notif.id); int? prevProg; File downloadedFile = await downloadFile(downloadUrl, fileName, (double? progress) { @@ -126,13 +130,14 @@ class AppsProvider with ChangeNotifier { if (apps[app.id] != null) { apps[app.id]!.downloadProgress = progress; notifyListeners(); - } else if ((prog == 25 || prog == 50 || prog == 75) && prevProg != prog) { - Fluttertoast.showToast( - msg: tr('percentProgress', args: [prog.toString()]), - toastLength: Toast.LENGTH_SHORT); + } + notif = DownloadNotification(app.name, prog ?? 100); + if (prog != null && prevProg != prog) { + notificationsProvider?.notify(notif); } prevProg = prog; }); + notificationsProvider?.cancel(notif.id); // Delete older versions of the APK if any for (var file in downloadedFile.parent.listSync()) { var fn = file.path.split('/').last; @@ -305,7 +310,7 @@ class AppsProvider with ChangeNotifier { List downloadedFiles = await Future.wait(appsToInstall.map((id) async { try { - return await downloadApp(apps[id]!.app); + return await downloadApp(apps[id]!.app, context); } catch (e) { errors.add(id, e.toString()); } diff --git a/lib/providers/notifications_provider.dart b/lib/providers/notifications_provider.dart index cdb22a4..8fe64f4 100644 --- a/lib/providers/notifications_provider.dart +++ b/lib/providers/notifications_provider.dart @@ -13,9 +13,11 @@ class ObtainiumNotification { late String channelName; late String channelDescription; Importance importance; + bool onlyAlertOnce; ObtainiumNotification(this.id, this.title, this.message, this.channelCode, - this.channelName, this.channelDescription, this.importance); + this.channelName, this.channelDescription, this.importance, + {this.onlyAlertOnce = false}); } class UpdateNotification extends ObtainiumNotification { @@ -73,6 +75,21 @@ class AppsRemovedNotification extends ObtainiumNotification { } } +class DownloadNotification extends ObtainiumNotification { + DownloadNotification(String appName, int progPercent) + : super( + appName.hashCode, + 'Downloading $appName', + '$progPercent%', + 'APP_DOWNLOADING', + 'Downloading App', + 'Notifies the user of the progress in downloading an App', + Importance.defaultImportance, + onlyAlertOnce: true) { + message = tr('percentProgress', args: [progPercent.toString()]); + } +} + final completeInstallationNotification = ObtainiumNotification( 1, tr('completeAppInstallation'), @@ -128,7 +145,9 @@ class NotificationsProvider { String channelName, String channelDescription, Importance importance, - {bool cancelExisting = false}) async { + {bool cancelExisting = false, + int? progPercent, + bool onlyAlertOnce = false}) async { if (cancelExisting) { await cancel(id); } @@ -144,12 +163,16 @@ class NotificationsProvider { channelDescription: channelDescription, importance: importance, priority: importanceToPriority[importance]!, - groupKey: 'dev.imranr.obtainium.$channelCode'))); + groupKey: 'dev.imranr.obtainium.$channelCode', + progress: progPercent ?? 0, + maxProgress: 100, + showProgress: progPercent != null, + onlyAlertOnce: onlyAlertOnce))); } Future notify(ObtainiumNotification notif, {bool cancelExisting = false}) => notifyRaw(notif.id, notif.title, notif.message, notif.channelCode, notif.channelName, notif.channelDescription, notif.importance, - cancelExisting: cancelExisting); + cancelExisting: cancelExisting, onlyAlertOnce: notif.onlyAlertOnce); }