From 8ce266bc94fb3add1fe9f2787734f121664e8df4 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Fri, 29 Sep 2023 18:39:07 +0200 Subject: [PATCH] perf: Reduce amount of network requests --- assets/i18n/en_US.json | 4 +- lib/models/patch.dart | 1 - lib/models/patched_application.dart | 4 - lib/services/github_api.dart | 59 +------ lib/services/manager_api.dart | 68 +------- lib/services/revanced_api.dart | 34 ++-- lib/ui/views/home/home_viewmodel.dart | 9 +- .../navigation/navigation_viewmodel.dart | 4 +- .../widgets/homeView/installed_apps_card.dart | 2 - .../settings_advanced_section.dart | 2 +- lib/ui/widgets/shared/application_item.dart | 162 +++++------------- pubspec.yaml | 1 + 12 files changed, 78 insertions(+), 272 deletions(-) diff --git a/assets/i18n/en_US.json b/assets/i18n/en_US.json index 8ff7d9bc..571888cf 100644 --- a/assets/i18n/en_US.json +++ b/assets/i18n/en_US.json @@ -56,9 +56,7 @@ "updatesDisabled": "Updating a patched app is currently disabled. Repatch the app again." }, "applicationItem": { - "patchButton": "Patch", - "infoButton": "Info", - "changelogLabel": "Changelog" + "infoButton": "Info" }, "latestCommitCard": { "loadingLabel": "Loading...", diff --git a/lib/models/patch.dart b/lib/models/patch.dart index b3d99e2a..fe2b3c33 100644 --- a/lib/models/patch.dart +++ b/lib/models/patch.dart @@ -1,5 +1,4 @@ import 'package:json_annotation/json_annotation.dart'; -import 'package:revanced_manager/utils/string.dart'; part 'patch.g.dart'; diff --git a/lib/models/patched_application.dart b/lib/models/patched_application.dart index 90bfb9a3..8f54dd72 100644 --- a/lib/models/patched_application.dart +++ b/lib/models/patched_application.dart @@ -16,9 +16,7 @@ class PatchedApplication { required this.patchDate, this.isRooted = false, this.isFromStorage = false, - this.hasUpdates = false, this.appliedPatches = const [], - this.changelog = const [], }); factory PatchedApplication.fromJson(Map json) => @@ -36,9 +34,7 @@ class PatchedApplication { DateTime patchDate; bool isRooted; bool isFromStorage; - bool hasUpdates; List appliedPatches; - List changelog; Map toJson() => _$PatchedApplicationToJson(this); diff --git a/lib/services/github_api.dart b/lib/services/github_api.dart index 0949f1b9..5f393daf 100644 --- a/lib/services/github_api.dart +++ b/lib/services/github_api.dart @@ -51,6 +51,7 @@ class GithubAPI { Future clearAllCache() async { try { await _cacheOptions.store!.clean(); + await DefaultCacheManager().emptyCache(); } on Exception catch (e) { if (kDebugMode) { print(e); @@ -142,38 +143,6 @@ class GithubAPI { } } - Future> getCommits( - String packageName, - String repoName, - DateTime since, - ) async { - final String path = - 'src/main/kotlin/app/revanced/patches/${repoAppPath[packageName]}'; - try { - final response = await _dio.get( - '/repos/$repoName/commits', - queryParameters: { - 'path': path, - 'since': since.toIso8601String(), - }, - ); - final List commits = response.data; - return commits - .map( - (commit) => commit['commit']['message'].split('\n')[0] + - ' - ' + - commit['commit']['author']['name'] + - '\n' as String, - ) - .toList(); - } on Exception catch (e) { - if (kDebugMode) { - print(e); - } - } - return []; - } - Future getLatestReleaseFile( String extension, String repoName, @@ -237,30 +206,4 @@ class GithubAPI { } return null; } - - Future> getPatches( - String repoName, - String version, - String url, - ) async { - List patches = []; - try { - final File? f = await getPatchesReleaseFile( - '.json', - repoName, - version, - url, - ); - if (f != null) { - final List list = jsonDecode(f.readAsStringSync()); - patches = list.map((patch) => Patch.fromJson(patch)).toList(); - } - } on Exception catch (e) { - if (kDebugMode) { - print(e); - } - } - - return patches; - } } diff --git a/lib/services/manager_api.dart b/lib/services/manager_api.dart index 7d42f6d7..5882f810 100644 --- a/lib/services/manager_api.dart +++ b/lib/services/manager_api.dart @@ -616,39 +616,6 @@ class ManagerAPI { ); } - Future reAssessSavedApps() async { - final List patchedApps = getPatchedApps(); - final List unsavedApps = - await getUnsavedApps(patchedApps); - patchedApps.addAll(unsavedApps); - final List toRemove = - await getAppsToRemove(patchedApps); - patchedApps.removeWhere((a) => toRemove.contains(a)); - for (final PatchedApplication app in patchedApps) { - app.hasUpdates = - await hasAppUpdates(app.originalPackageName, app.patchDate); - app.changelog = - await getAppChangelog(app.originalPackageName, app.patchDate); - if (!app.hasUpdates) { - final String? currentInstalledVersion = - (await DeviceApps.getApp(app.packageName))?.versionName; - if (currentInstalledVersion != null) { - final String currentSavedVersion = app.version; - final int currentInstalledVersionInt = int.parse( - currentInstalledVersion.replaceAll(RegExp('[^0-9]'), ''), - ); - final int currentSavedVersionInt = int.parse( - currentSavedVersion.replaceAll(RegExp('[^0-9]'), ''), - ); - if (currentInstalledVersionInt > currentSavedVersionInt) { - app.hasUpdates = true; - } - } - } - } - await setPatchedApps(patchedApps); - } - Future isAppUninstalled(PatchedApplication app) async { bool existsRoot = false; final bool existsNonRoot = await DeviceApps.isAppInstalled(app.packageName); @@ -662,37 +629,6 @@ class ManagerAPI { return !existsNonRoot; } - Future hasAppUpdates( - String packageName, - DateTime patchDate, - ) async { - final List commits = await _githubAPI.getCommits( - packageName, - getPatchesRepo(), - patchDate, - ); - return commits.isNotEmpty; - } - - Future> getAppChangelog( - String packageName, - DateTime patchDate, - ) async { - List newCommits = await _githubAPI.getCommits( - packageName, - getPatchesRepo(), - patchDate, - ); - if (newCommits.isEmpty) { - newCommits = await _githubAPI.getCommits( - packageName, - getPatchesRepo(), - patchDate, - ); - } - return newCommits; - } - Future isSplitApk(PatchedApplication patchedApp) async { Application? app; if (patchedApp.isFromStorage) { @@ -760,6 +696,8 @@ class ManagerAPI { Future resetLastSelectedPatches() async { final File selectedPatchesFile = File(storedPatchesFile); - selectedPatchesFile.deleteSync(); + if (selectedPatchesFile.existsSync()) { + selectedPatchesFile.deleteSync(); + } } } diff --git a/lib/services/revanced_api.dart b/lib/services/revanced_api.dart index dde23cee..f21e6b71 100644 --- a/lib/services/revanced_api.dart +++ b/lib/services/revanced_api.dart @@ -7,12 +7,15 @@ import 'package:dio_cache_interceptor/dio_cache_interceptor.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:injectable/injectable.dart'; +import 'package:synchronized/synchronized.dart'; import 'package:timeago/timeago.dart'; @lazySingleton class RevancedAPI { late Dio _dio = Dio(); + final Lock getToolsLock = Lock(); + final _cacheOptions = CacheOptions( store: MemCacheStore(), maxStale: const Duration(days: 1), @@ -38,6 +41,7 @@ class RevancedAPI { Future clearAllCache() async { try { await _cacheOptions.store!.clean(); + await DefaultCacheManager().emptyCache(); } on Exception catch (e) { if (kDebugMode) { print(e); @@ -66,21 +70,23 @@ class RevancedAPI { Future?> _getLatestRelease( String extension, String repoName, - ) async { - try { - final response = await _dio.get('/tools'); - final List tools = response.data['tools']; - return tools.firstWhereOrNull( - (t) => - t['repository'] == repoName && - (t['name'] as String).endsWith(extension), - ); - } on Exception catch (e) { - if (kDebugMode) { - print(e); + ) { + return getToolsLock.synchronized(() async { + try { + final response = await _dio.get('/tools'); + final List tools = response.data['tools']; + return tools.firstWhereOrNull( + (t) => + t['repository'] == repoName && + (t['name'] as String).endsWith(extension), + ); + } on Exception catch (e) { + if (kDebugMode) { + print(e); + } + return null; } - return null; - } + }); } Future getLatestReleaseVersion( diff --git a/lib/ui/views/home/home_viewmodel.dart b/lib/ui/views/home/home_viewmodel.dart index cfc99e66..7ff6d1bb 100644 --- a/lib/ui/views/home/home_viewmodel.dart +++ b/lib/ui/views/home/home_viewmodel.dart @@ -37,15 +37,15 @@ class HomeViewModel extends BaseViewModel { DateTime? _lastUpdate; bool showUpdatableApps = false; List patchedInstalledApps = []; - List patchedUpdatableApps = []; String? _latestManagerVersion = ''; File? downloadedApk; Future initialize(BuildContext context) async { - _latestManagerVersion = await _managerAPI.getLatestManagerVersion(); if (!_managerAPI.getPatchesConsent()) { await showPatchesConsent(context); } + + _latestManagerVersion = await _managerAPI.getLatestManagerVersion(); await _patcherAPI.initialize(); await flutterLocalNotificationsPlugin.initialize( const InitializationSettings( @@ -83,7 +83,6 @@ class HomeViewModel extends BaseViewModel { } } _getPatchedApps(); - _managerAPI.reAssessSavedApps().then((_) => _getPatchedApps()); } void navigateToAppInfo(PatchedApplication app) { @@ -108,10 +107,6 @@ class HomeViewModel extends BaseViewModel { void _getPatchedApps() { patchedInstalledApps = _managerAPI.getPatchedApps().toList(); - patchedUpdatableApps = _managerAPI - .getPatchedApps() - .where((app) => app.hasUpdates == true) - .toList(); notifyListeners(); } diff --git a/lib/ui/views/navigation/navigation_viewmodel.dart b/lib/ui/views/navigation/navigation_viewmodel.dart index a2a0e4bb..a911f070 100644 --- a/lib/ui/views/navigation/navigation_viewmodel.dart +++ b/lib/ui/views/navigation/navigation_viewmodel.dart @@ -39,9 +39,9 @@ class NavigationViewModel extends IndexTrackingViewModel { // Force disable Material You on Android 11 and below if (dynamicTheme.themeId.isOdd) { - const int ANDROID_12_SDK_VERSION = 31; + const int android12SdkVersion = 31; final AndroidDeviceInfo info = await DeviceInfoPlugin().androidInfo; - if (info.version.sdkInt < ANDROID_12_SDK_VERSION) { + if (info.version.sdkInt < android12SdkVersion) { await prefs.setInt('themeMode', 0); await prefs.setBool('useDynamicTheme', false); await dynamicTheme.setTheme(0); diff --git a/lib/ui/widgets/homeView/installed_apps_card.dart b/lib/ui/widgets/homeView/installed_apps_card.dart index 7a989856..ec825340 100644 --- a/lib/ui/widgets/homeView/installed_apps_card.dart +++ b/lib/ui/widgets/homeView/installed_apps_card.dart @@ -79,8 +79,6 @@ class InstalledAppsCard extends StatelessWidget { icon: app.icon, name: app.name, patchDate: app.patchDate, - changelog: app.changelog, - isUpdatableApp: false, onPressed: () => locator().navigateToAppInfo(app), ), diff --git a/lib/ui/widgets/settingsView/settings_advanced_section.dart b/lib/ui/widgets/settingsView/settings_advanced_section.dart index 2d9be3fc..f3e838a6 100644 --- a/lib/ui/widgets/settingsView/settings_advanced_section.dart +++ b/lib/ui/widgets/settingsView/settings_advanced_section.dart @@ -5,8 +5,8 @@ import 'package:flutter_i18n/widgets/I18nText.dart'; import 'package:revanced_manager/ui/views/settings/settingsFragment/settings_manage_api_url.dart'; import 'package:revanced_manager/ui/views/settings/settingsFragment/settings_manage_sources.dart'; import 'package:revanced_manager/ui/views/settings/settings_viewmodel.dart'; -import 'package:revanced_manager/ui/widgets/settingsView/settings_enable_patches_selection.dart'; import 'package:revanced_manager/ui/widgets/settingsView/settings_auto_update_patches.dart'; +import 'package:revanced_manager/ui/widgets/settingsView/settings_enable_patches_selection.dart'; import 'package:revanced_manager/ui/widgets/settingsView/settings_experimental_patches.dart'; import 'package:revanced_manager/ui/widgets/settingsView/settings_experimental_universal_patches.dart'; import 'package:revanced_manager/ui/widgets/settingsView/settings_section.dart'; diff --git a/lib/ui/widgets/shared/application_item.dart b/lib/ui/widgets/shared/application_item.dart index 42eee351..b7336435 100644 --- a/lib/ui/widgets/shared/application_item.dart +++ b/lib/ui/widgets/shared/application_item.dart @@ -1,6 +1,5 @@ import 'dart:typed_data'; -import 'package:expandable/expandable.dart'; import 'package:flutter/material.dart'; import 'package:flutter_i18n/flutter_i18n.dart'; import 'package:revanced_manager/ui/widgets/shared/custom_card.dart'; @@ -13,151 +12,84 @@ class ApplicationItem extends StatefulWidget { required this.icon, required this.name, required this.patchDate, - required this.changelog, - required this.isUpdatableApp, required this.onPressed, }) : super(key: key); final Uint8List icon; final String name; final DateTime patchDate; - final List changelog; - final bool isUpdatableApp; final Function() onPressed; @override State createState() => _ApplicationItemState(); } -class _ApplicationItemState extends State - with TickerProviderStateMixin { - late AnimationController _animationController; +class _ApplicationItemState extends State { @override void initState() { super.initState(); - _animationController = AnimationController( - vsync: this, - duration: const Duration(milliseconds: 300), - ); - } - - @override - void dispose() { - _animationController.dispose(); - super.dispose(); } @override Widget build(BuildContext context) { - final ExpandableController expController = ExpandableController(); return Container( margin: const EdgeInsets.only(bottom: 16.0), child: CustomCard( - onTap: () { - expController.toggle(); - _animationController.isCompleted - ? _animationController.reverse() - : _animationController.forward(); - }, - child: ExpandablePanel( - controller: expController, - theme: const ExpandableThemeData( - inkWellBorderRadius: BorderRadius.all(Radius.circular(16)), - tapBodyToCollapse: false, - tapBodyToExpand: false, - tapHeaderToExpand: false, - hasIcon: false, - animationDuration: Duration(milliseconds: 450), - ), - header: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - Flexible( - child: Row( - children: [ - SizedBox( - width: 40, - child: Image.memory(widget.icon, height: 40, width: 40), - ), - const SizedBox(width: 19), - Expanded( - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - widget.name, - maxLines: 1, - overflow: TextOverflow.ellipsis, - style: const TextStyle( - fontSize: 16, - fontWeight: FontWeight.w500, - ), - ), - Text( - format(widget.patchDate), - maxLines: 1, - overflow: TextOverflow.ellipsis, - style: const TextStyle( - fontSize: 16, - fontWeight: FontWeight.w500, - ), - ), - ], - ), - ), - ], - ), - ), - Row( + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Flexible( + child: Row( children: [ - RotationTransition( - turns: Tween(begin: 0.0, end: 0.50) - .animate(_animationController), - child: const Padding( - padding: EdgeInsets.all(8.0), - child: Icon(Icons.arrow_drop_down), - ), + SizedBox( + width: 40, + child: Image.memory(widget.icon, height: 40, width: 40), ), - const SizedBox(width: 8), - Column( - mainAxisAlignment: MainAxisAlignment.center, - crossAxisAlignment: CrossAxisAlignment.end, - children: [ - CustomMaterialButton( - label: widget.isUpdatableApp - ? I18nText('applicationItem.patchButton') - : I18nText('applicationItem.infoButton'), - onPressed: widget.onPressed, - ), - ], + const SizedBox(width: 19), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + widget.name, + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: const TextStyle( + fontSize: 16, + fontWeight: FontWeight.w500, + ), + ), + Text( + format(widget.patchDate), + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: const TextStyle( + fontSize: 16, + fontWeight: FontWeight.w500, + ), + ), + ], + ), ), ], ), - ], - ), - collapsed: const SizedBox(), - expanded: Padding( - padding: const EdgeInsets.only( - top: 16.0, - left: 4.0, - right: 4.0, - bottom: 4.0, ), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - I18nText( - 'applicationItem.changelogLabel', - child: const Text( - '', - style: TextStyle(fontWeight: FontWeight.w700), - ), + Row( + children: [ + const SizedBox(width: 8), + Column( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.end, + children: [ + CustomMaterialButton( + label: I18nText('applicationItem.infoButton'), + onPressed: widget.onPressed, + ), + ], ), - const SizedBox(height: 4), - Text('\u2022 ${widget.changelog.join('\n\u2022 ')}'), ], ), - ), + ], ), ), ); diff --git a/pubspec.yaml b/pubspec.yaml index 98d16d32..297c8ada 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -75,6 +75,7 @@ dependencies: flutter_markdown: ^0.6.14 dio_cache_interceptor: ^3.4.0 install_plugin: ^2.1.0 + synchronized: ^3.1.0 dev_dependencies: json_serializable: ^6.6.1