From d051ae576bd498edbe699d7da5003afa6889f153 Mon Sep 17 00:00:00 2001 From: Aabed Khan <39409020+TheAabedKhan@users.noreply.github.com> Date: Fri, 23 Jun 2023 19:21:36 +0545 Subject: [PATCH] feat(updater): download successful dialog (#938) --- assets/i18n/en_US.json | 2 + lib/services/revanced_api.dart | 1 + lib/ui/views/home/home_viewmodel.dart | 176 +++++++++++++++++--------- 3 files changed, 121 insertions(+), 58 deletions(-) diff --git a/assets/i18n/en_US.json b/assets/i18n/en_US.json index ae4e5360..9a6b4a4f 100644 --- a/assets/i18n/en_US.json +++ b/assets/i18n/en_US.json @@ -24,11 +24,13 @@ "WIP": "Work in progress...", "noInstallations": "No patched applications installed", "installed": "Installed", + "installUpdate": "Continue to install the update?", "updateDialogTitle": "Update Manager", "updateChangelogTitle": "Changelog", "notificationTitle": "Update downloaded", "notificationText": "Tap to install the update", "downloadingMessage": "Downloading update...", + "downloadedMessage": "Update downloaded!", "installingMessage": "Installing update...", "errorDownloadMessage": "Unable to download update", "errorInstallMessage": "Unable to install update", diff --git a/lib/services/revanced_api.dart b/lib/services/revanced_api.dart index d613a132..a8bec19c 100644 --- a/lib/services/revanced_api.dart +++ b/lib/services/revanced_api.dart @@ -170,6 +170,7 @@ class RevancedAPI { updateManagerDownloadProgress(progress); } else if (result is FileInfo) { + disposeManagerUpdateProgress(); // The download is complete; convert the FileInfo object to a File object outputFile = File(result.file.path); } diff --git a/lib/ui/views/home/home_viewmodel.dart b/lib/ui/views/home/home_viewmodel.dart index edcc357d..b05f0e4d 100644 --- a/lib/ui/views/home/home_viewmodel.dart +++ b/lib/ui/views/home/home_viewmodel.dart @@ -1,7 +1,6 @@ // ignore_for_file: use_build_context_synchronously import 'dart:async'; import 'dart:io'; - import 'package:app_installer/app_installer.dart'; import 'package:cross_connectivity/cross_connectivity.dart'; import 'package:flutter/foundation.dart'; @@ -39,6 +38,7 @@ class HomeViewModel extends BaseViewModel { List patchedInstalledApps = []; List patchedUpdatableApps = []; String? _latestManagerVersion = ''; + File? downloadedApk; Future initialize(BuildContext context) async { _latestManagerVersion = await _managerAPI.getLatestManagerVersion(); @@ -162,74 +162,134 @@ class HomeViewModel extends BaseViewModel { } Future updateManager(BuildContext context) async { + final ValueNotifier downloaded = ValueNotifier(false); try { _toast.showBottom('homeView.downloadingMessage'); showDialog( context: context, - builder: (context) => SimpleDialog( - contentPadding: const EdgeInsets.all(16.0), - title: I18nText( - 'homeView.downloadingMessage', - child: Text( - '', - style: TextStyle( - fontSize: 20, - fontWeight: FontWeight.w500, - color: Theme.of(context).colorScheme.secondary, - ), - ), - ), - children: [ - Column( - children: [ - Row( - children: [ - Icon( - Icons.new_releases_outlined, - color: Theme.of(context).colorScheme.secondary, - ), - const SizedBox(width: 8.0), - Text( - '$_latestManagerVersion', - style: TextStyle( - fontSize: 18, - fontWeight: FontWeight.w500, - color: Theme.of(context).colorScheme.secondary, - ), - ), - ], - ), - const SizedBox(height: 16.0), - StreamBuilder( - initialData: 0.0, - stream: _revancedAPI.managerUpdateProgress.stream, - builder: (context, snapshot) { - return LinearProgressIndicator( - value: snapshot.data! * 0.01, - valueColor: AlwaysStoppedAnimation( - Theme.of(context).colorScheme.secondary, - ), - ); - }, - ), - const SizedBox(height: 16.0), - Align( - alignment: Alignment.centerRight, - child: CustomMaterialButton( - label: I18nText('cancelButton'), - onPressed: () { - _revancedAPI.disposeManagerUpdateProgress(); - Navigator.of(context).pop(); - }, + builder: (context) => ValueListenableBuilder( + valueListenable: downloaded, + builder: (context, value, child) { + return SimpleDialog( + contentPadding: const EdgeInsets.all(16.0), + title: I18nText( + !value + ? 'homeView.downloadingMessage' + : 'homeView.downloadedMessage', + child: Text( + '', + style: TextStyle( + fontSize: 20, + fontWeight: FontWeight.w500, + color: Theme.of(context).colorScheme.secondary, ), ), + ), + children: [ + Column( + children: [ + Row( + children: [ + Icon( + Icons.new_releases_outlined, + color: Theme.of(context).colorScheme.secondary, + ), + const SizedBox(width: 8.0), + Text( + '$_latestManagerVersion', + style: TextStyle( + fontSize: 18, + fontWeight: FontWeight.w500, + color: Theme.of(context).colorScheme.secondary, + ), + ), + ], + ), + const SizedBox(height: 16.0), + if (!value) + Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + StreamBuilder( + initialData: 0.0, + stream: _revancedAPI.managerUpdateProgress.stream, + builder: (context, snapshot) { + return LinearProgressIndicator( + value: snapshot.data! * 0.01, + valueColor: AlwaysStoppedAnimation( + Theme.of(context).colorScheme.secondary, + ), + ); + }, + ), + const SizedBox(height: 16.0), + Align( + alignment: Alignment.centerRight, + child: CustomMaterialButton( + label: I18nText('cancelButton'), + onPressed: () { + _revancedAPI.disposeManagerUpdateProgress(); + Navigator.of(context).pop(); + }, + ), + ), + ], + ), + if (value) + Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + I18nText( + 'homeView.installUpdate', + child: Text( + '', + style: TextStyle( + fontSize: 20, + fontWeight: FontWeight.w500, + color: Theme.of(context).colorScheme.secondary, + ), + ), + ), + const SizedBox(height: 16.0), + Row( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + Align( + alignment: Alignment.centerRight, + child: CustomMaterialButton( + isFilled: false, + label: I18nText('cancelButton'), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + ), + const SizedBox(width: 8.0), + Align( + alignment: Alignment.centerRight, + child: CustomMaterialButton( + label: I18nText('updateButton'), + onPressed: () async { + await AppInstaller.installApk( + downloadedApk!.path,); + }, + ), + ), + ], + ), + ], + ), + ], + ), ], - ), - ], + ); + }, ), ); final File? managerApk = await downloadManager(); if (managerApk != null) { + downloaded.value = true; + downloadedApk = managerApk; // await flutterLocalNotificationsPlugin.zonedSchedule( // 0, // FlutterI18n.translate(