Add release asset download button (#1493)

This commit is contained in:
Imran Remtulla
2024-04-07 01:28:45 -04:00
parent b26043ef5e
commit 3d1113c057
23 changed files with 227 additions and 49 deletions

View File

@@ -1,12 +1,15 @@
import 'package:animations/animations.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:obtainium/components/generated_form.dart';
import 'package:obtainium/components/generated_form_modal.dart';
import 'package:obtainium/custom_errors.dart';
import 'package:obtainium/main.dart';
import 'package:obtainium/pages/apps.dart';
import 'package:obtainium/pages/settings.dart';
import 'package:obtainium/providers/apps_provider.dart';
import 'package:obtainium/providers/notifications_provider.dart';
import 'package:obtainium/providers/settings_provider.dart';
import 'package:obtainium/providers/source_provider.dart';
import 'package:url_launcher/url_launcher_string.dart';
@@ -158,6 +161,87 @@ class _AppPageState extends State<AppPage> {
textAlign: TextAlign.center,
style: const TextStyle(fontStyle: FontStyle.italic, fontSize: 12),
),
if (app?.app.apkUrls.isNotEmpty == true ||
app?.app.otherAssetUrls.isNotEmpty == true)
GestureDetector(
onTap: app?.app == null || updating
? null
: () async {
var allAssetUrls = [
...app!.app.apkUrls,
...app.app.otherAssetUrls
].map((e) => MapEntry(e.value, e.key)).toList();
var values = await showModal(
context: globalNavigatorKey.currentContext ?? context,
builder: (BuildContext ctx) {
return GeneratedFormModal(
title:
tr('downloadX', args: [tr('releaseAsset')]),
initValid: true,
items: [
[
GeneratedFormDropdown(
'assetToDownload', allAssetUrls,
defaultValue: allAssetUrls[0].key,
label: tr('selectX', args: [
tr('releaseAsset').toLowerCase()
]))
]
]);
},
);
if (values != null) {
var downloadUrl = values['assetToDownload'] as String;
var fileName = allAssetUrls
.where((e) => e.key == downloadUrl)
.first
.value;
NotificationsProvider notificationsProvider =
(globalNavigatorKey.currentContext ?? context)
.read<NotificationsProvider>();
try {
showMessage(
'${tr('downloadingX', args: [fileName])}...',
globalNavigatorKey.currentContext ?? context);
await downloadFile(
downloadUrl,
fileName
.split('.')
.reversed
.toList()
.sublist(1)
.reversed
.join('.'), (double? progress) {
notificationsProvider.notify(DownloadNotification(
fileName, progress?.ceil() ?? 0));
}, '/storage/emulated/0/Download',
headers: await source?.getRequestHeaders(
app.app.additionalSettings,
forAPKDownload: fileName.endsWith('.apk')
? true
: false));
notificationsProvider.notify(
DownloadedNotification(fileName, downloadUrl));
} catch (e) {
showError(
e, globalNavigatorKey.currentContext ?? context);
} finally {
notificationsProvider
.cancel(DownloadNotification(fileName, 0).id);
}
}
},
child: Text(
tr('downloadX', args: [tr('releaseAsset').toLowerCase()]),
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.labelSmall!.copyWith(
decoration:
changeLogFn != null ? TextDecoration.underline : null,
fontStyle: changeLogFn != null ? FontStyle.italic : null,
),
),
),
const SizedBox(
height: 48,
),