Added download notifications

(removed toast during add app)
This commit is contained in:
Imran Remtulla
2022-12-04 12:48:12 -05:00
parent 17b1f6e5b0
commit 963f51dc53
3 changed files with 40 additions and 11 deletions

View File

@@ -107,7 +107,8 @@ class _AddAppPageState extends State<AddAppPage> {
throw ObtainiumError(tr('cancelled')); throw ObtainiumError(tr('cancelled'));
} }
app.preferredApkIndex = app.apkUrls.indexOf(apkUrl); 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; app.id = downloadedApk.appId;
} }
if (appsProvider.apps.containsKey(app.id)) { if (appsProvider.apps.containsKey(app.id)) {

View File

@@ -113,12 +113,16 @@ class AppsProvider with ChangeNotifier {
return downloadedFile; return downloadedFile;
} }
Future<DownloadedApk> downloadApp(App app) async { Future<DownloadedApk> downloadApp(App app, BuildContext? context) async {
var fileName = var fileName =
'${app.id}-${app.latestVersion}-${app.preferredApkIndex}.apk'; '${app.id}-${app.latestVersion}-${app.preferredApkIndex}.apk';
String downloadUrl = await SourceProvider() String downloadUrl = await SourceProvider()
.getSource(app.url) .getSource(app.url)
.apkUrlPrefetchModifier(app.apkUrls[app.preferredApkIndex]); .apkUrlPrefetchModifier(app.apkUrls[app.preferredApkIndex]);
NotificationsProvider? notificationsProvider =
context?.read<NotificationsProvider>();
var notif = DownloadNotification(app.name, 100);
notificationsProvider?.cancel(notif.id);
int? prevProg; int? prevProg;
File downloadedFile = File downloadedFile =
await downloadFile(downloadUrl, fileName, (double? progress) { await downloadFile(downloadUrl, fileName, (double? progress) {
@@ -126,13 +130,14 @@ class AppsProvider with ChangeNotifier {
if (apps[app.id] != null) { if (apps[app.id] != null) {
apps[app.id]!.downloadProgress = progress; apps[app.id]!.downloadProgress = progress;
notifyListeners(); notifyListeners();
} else if ((prog == 25 || prog == 50 || prog == 75) && prevProg != prog) { }
Fluttertoast.showToast( notif = DownloadNotification(app.name, prog ?? 100);
msg: tr('percentProgress', args: [prog.toString()]), if (prog != null && prevProg != prog) {
toastLength: Toast.LENGTH_SHORT); notificationsProvider?.notify(notif);
} }
prevProg = prog; prevProg = prog;
}); });
notificationsProvider?.cancel(notif.id);
// Delete older versions of the APK if any // Delete older versions of the APK if any
for (var file in downloadedFile.parent.listSync()) { for (var file in downloadedFile.parent.listSync()) {
var fn = file.path.split('/').last; var fn = file.path.split('/').last;
@@ -305,7 +310,7 @@ class AppsProvider with ChangeNotifier {
List<DownloadedApk?> downloadedFiles = List<DownloadedApk?> downloadedFiles =
await Future.wait(appsToInstall.map((id) async { await Future.wait(appsToInstall.map((id) async {
try { try {
return await downloadApp(apps[id]!.app); return await downloadApp(apps[id]!.app, context);
} catch (e) { } catch (e) {
errors.add(id, e.toString()); errors.add(id, e.toString());
} }

View File

@@ -13,9 +13,11 @@ class ObtainiumNotification {
late String channelName; late String channelName;
late String channelDescription; late String channelDescription;
Importance importance; Importance importance;
bool onlyAlertOnce;
ObtainiumNotification(this.id, this.title, this.message, this.channelCode, 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 { 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( final completeInstallationNotification = ObtainiumNotification(
1, 1,
tr('completeAppInstallation'), tr('completeAppInstallation'),
@@ -128,7 +145,9 @@ class NotificationsProvider {
String channelName, String channelName,
String channelDescription, String channelDescription,
Importance importance, Importance importance,
{bool cancelExisting = false}) async { {bool cancelExisting = false,
int? progPercent,
bool onlyAlertOnce = false}) async {
if (cancelExisting) { if (cancelExisting) {
await cancel(id); await cancel(id);
} }
@@ -144,12 +163,16 @@ class NotificationsProvider {
channelDescription: channelDescription, channelDescription: channelDescription,
importance: importance, importance: importance,
priority: importanceToPriority[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<void> notify(ObtainiumNotification notif, Future<void> notify(ObtainiumNotification notif,
{bool cancelExisting = false}) => {bool cancelExisting = false}) =>
notifyRaw(notif.id, notif.title, notif.message, notif.channelCode, notifyRaw(notif.id, notif.title, notif.message, notif.channelCode,
notif.channelName, notif.channelDescription, notif.importance, notif.channelName, notif.channelDescription, notif.importance,
cancelExisting: cancelExisting); cancelExisting: cancelExisting, onlyAlertOnce: notif.onlyAlertOnce);
} }