From fc525602448001a442d2f2dfe09c431eed80cd7b Mon Sep 17 00:00:00 2001 From: kitadai31 <90122968+kitadai31@users.noreply.github.com> Date: Wed, 19 Jun 2024 16:44:09 +0900 Subject: [PATCH] fix: Update dialog shows dev version & loading gets stuck in certain circumstances (#1792) Signed-off-by: validcube Co-authored-by: validcube --- lib/services/github_api.dart | 35 ++-- lib/ui/views/home/home_viewmodel.dart | 24 +-- .../homeView/update_confirmation_sheet.dart | 176 +++++++++--------- 3 files changed, 114 insertions(+), 121 deletions(-) diff --git a/lib/services/github_api.dart b/lib/services/github_api.dart index 277ad0de..b4ab03a6 100644 --- a/lib/services/github_api.dart +++ b/lib/services/github_api.dart @@ -49,36 +49,27 @@ class GithubAPI { } } - Future?> getLatestManagerRelease( - String repoName, - ) async { + Future getManagerChangelogs() async { try { final response = await _dioGetSynchronously( - '/repos/$repoName/releases', + '/repos/${_managerAPI.defaultManagerRepo}/releases?per_page=50', ); - final Map releases = response.data[0]; - int updates = 0; + final buffer = StringBuffer(); final String currentVersion = await _managerAPI.getCurrentManagerVersion(); - while (response.data[updates]['tag_name'] != currentVersion) { - updates++; - } - for (int i = 1; i < updates; i++) { - if (response.data[i]['prerelease']) { + for (final release in response.data) { + if (release['tag_name'] == currentVersion) { + if (buffer.isEmpty) { + buffer.writeln(release['body']); + } + break; + } + if (release['prerelease']) { continue; } - releases.update( - 'body', - (value) => - value + - '\n' + - '# ' + - response.data[i]['tag_name'] + - '\n' + - response.data[i]['body'], - ); + buffer.writeln(release['body']); } - return releases; + return buffer.toString(); } on Exception catch (e) { if (kDebugMode) { print(e); diff --git a/lib/ui/views/home/home_viewmodel.dart b/lib/ui/views/home/home_viewmodel.dart index 2d33ed26..8b7c1816 100644 --- a/lib/ui/views/home/home_viewmodel.dart +++ b/lib/ui/views/home/home_viewmodel.dart @@ -39,7 +39,8 @@ class HomeViewModel extends BaseViewModel { List patchedInstalledApps = []; String _currentManagerVersion = ''; String _currentPatchesVersion = ''; - String? _latestManagerVersion = ''; + String? latestManagerVersion; + String? latestPatchesVersion; File? downloadedApk; Future initialize(BuildContext context) async { @@ -50,7 +51,6 @@ class HomeViewModel extends BaseViewModel { await forceRefresh(context); return; } - _latestManagerVersion = await _managerAPI.getLatestManagerVersion(); _currentPatchesVersion = await _managerAPI.getCurrentPatchesVersion(); if (_managerAPI.showUpdateDialog() && await hasManagerUpdates()) { showUpdateDialog(context, false); @@ -131,21 +131,21 @@ class HomeViewModel extends BaseViewModel { if (!_managerAPI.releaseBuild) { return false; } - _latestManagerVersion = + latestManagerVersion = await _managerAPI.getLatestManagerVersion() ?? _currentManagerVersion; - if (_latestManagerVersion != _currentManagerVersion) { + if (latestManagerVersion != _currentManagerVersion) { return true; } return false; } Future hasPatchesUpdates() async { - final String? latestVersion = await _managerAPI.getLatestPatchesVersion(); - if (latestVersion != null) { + latestPatchesVersion = await _managerAPI.getLatestPatchesVersion(); + if (latestPatchesVersion != null) { try { final int latestVersionInt = - int.parse(latestVersion.replaceAll(RegExp('[^0-9]'), '')); + int.parse(latestPatchesVersion!.replaceAll(RegExp('[^0-9]'), '')); final int currentVersionInt = int.parse(_currentPatchesVersion.replaceAll(RegExp('[^0-9]'), '')); return latestVersionInt > currentVersionInt; @@ -475,12 +475,14 @@ class HomeViewModel extends BaseViewModel { ); } - Future?> getLatestManagerRelease() { - return _githubAPI.getLatestManagerRelease(_managerAPI.defaultManagerRepo); + Future getManagerChangelogs() { + return _githubAPI.getManagerChangelogs(); } - Future?> getLatestPatchesRelease() { - return _githubAPI.getLatestRelease(_managerAPI.defaultPatchesRepo); + Future getLatestPatchesChangelog() async { + final release = + await _githubAPI.getLatestRelease(_managerAPI.defaultPatchesRepo); + return release?['body']; } Future getLatestPatchesReleaseTime() { diff --git a/lib/ui/widgets/homeView/update_confirmation_sheet.dart b/lib/ui/widgets/homeView/update_confirmation_sheet.dart index 388c38aa..21f37c24 100644 --- a/lib/ui/widgets/homeView/update_confirmation_sheet.dart +++ b/lib/ui/widgets/homeView/update_confirmation_sheet.dart @@ -14,6 +14,7 @@ class UpdateConfirmationSheet extends StatelessWidget { final bool isPatches; final bool changelog; + @override Widget build(BuildContext context) { final HomeViewModel model = locator(); @@ -25,100 +26,99 @@ class UpdateConfirmationSheet extends StatelessWidget { builder: (_, scrollController) => SingleChildScrollView( controller: scrollController, child: SafeArea( - child: FutureBuilder?>( - future: !isPatches - ? model.getLatestManagerRelease() - : model.getLatestPatchesRelease(), - builder: (_, snapshot) { - if (!snapshot.hasData) { - return const SizedBox( - height: 300, - child: Center( - child: CircularProgressIndicator(), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + if (!changelog) + Padding( + padding: const EdgeInsets.only( + top: 40.0, + left: 24.0, + right: 24.0, + bottom: 20.0, ), - ); - } - - return Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - if (!changelog) - Padding( - padding: const EdgeInsets.only( - top: 40.0, - left: 24.0, - right: 24.0, - bottom: 20.0, - ), - child: Row( - children: [ - Expanded( - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, + child: Row( + children: [ + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + isPatches + ? t.homeView.updatePatchesSheetTitle + : t.homeView.updateSheetTitle, + style: const TextStyle( + fontSize: 24, + fontWeight: FontWeight.bold, + ), + ), + const SizedBox(height: 4.0), + Row( children: [ + Icon( + Icons.new_releases_outlined, + color: + Theme.of(context).colorScheme.secondary, + ), + const SizedBox(width: 8.0), Text( isPatches - ? t.homeView.updatePatchesSheetTitle - : t.homeView.updateSheetTitle, - style: const TextStyle( - fontSize: 24, - fontWeight: FontWeight.bold, + ? model.latestPatchesVersion ?? 'Unknown' + : model.latestManagerVersion ?? 'Unknown', + style: TextStyle( + fontSize: 20, + fontWeight: FontWeight.w500, + color: + Theme.of(context).colorScheme.secondary, ), ), - const SizedBox(height: 4.0), - Row( - children: [ - Icon( - Icons.new_releases_outlined, - color: Theme.of(context) - .colorScheme - .secondary, - ), - const SizedBox(width: 8.0), - Text( - snapshot.data!['tag_name'] ?? 'Unknown', - style: TextStyle( - fontSize: 20, - fontWeight: FontWeight.w500, - color: Theme.of(context) - .colorScheme - .secondary, - ), - ), - ], - ), ], ), - ), - FilledButton( - onPressed: () { - Navigator.of(context).pop(); - isPatches - ? model.updatePatches(context) - : model.updateManager(context); - }, - child: Text(t.updateButton), - ), - ], + ], + ), ), - ), - Padding( - padding: const EdgeInsets.only( - top: 12.0, - left: 24.0, - bottom: 12.0, - ), - child: Text( - t.homeView.updateChangelogTitle, - style: TextStyle( - fontSize: changelog ? 24 : 20, - fontWeight: FontWeight.w500, - color: - Theme.of(context).colorScheme.onSecondaryContainer, + FilledButton( + onPressed: () { + Navigator.of(context).pop(); + isPatches + ? model.updatePatches(context) + : model.updateManager(context); + }, + child: Text(t.updateButton), ), - ), + ], ), - Container( + ), + Padding( + padding: const EdgeInsets.only( + top: 12.0, + left: 24.0, + bottom: 12.0, + ), + child: Text( + t.homeView.updateChangelogTitle, + style: TextStyle( + fontSize: changelog ? 24 : 20, + fontWeight: FontWeight.w500, + color: Theme.of(context).colorScheme.onSecondaryContainer, + ), + ), + ), + FutureBuilder( + future: !isPatches + ? model.getManagerChangelogs() + : model.getLatestPatchesChangelog(), + builder: (_, snapshot) { + if (!snapshot.hasData) { + return Padding( + padding: EdgeInsets.only(top: changelog ? 96 : 24), + child: const Center( + child: CircularProgressIndicator(), + ), + ); + } + + return Container( margin: const EdgeInsets.symmetric(horizontal: 24.0), decoration: BoxDecoration( color: Theme.of(context).colorScheme.secondaryContainer, @@ -139,12 +139,12 @@ class UpdateConfirmationSheet extends StatelessWidget { shrinkWrap: true, physics: const NeverScrollableScrollPhysics(), padding: const EdgeInsets.all(20.0), - data: snapshot.data!['body'] ?? '', + data: snapshot.data ?? '', ), - ), - ], - ); - }, + ); + }, + ), + ], ), ), ),