From f4b0a695d6cd21ee210106121fefa539dfc593aa Mon Sep 17 00:00:00 2001 From: Benjamin <73490201+BenjaminHalko@users.noreply.github.com> Date: Fri, 15 Sep 2023 08:58:15 -0700 Subject: [PATCH] fix: improve app list loading speed (#1166) --- lib/services/patcher_api.dart | 70 ++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/lib/services/patcher_api.dart b/lib/services/patcher_api.dart index dfaaf3ec95..9935d9ea45 100644 --- a/lib/services/patcher_api.dart +++ b/lib/services/patcher_api.dart @@ -25,6 +25,8 @@ class PatcherAPI { late Directory _tmpDir; late File _keyStoreFile; List _patches = []; + List _universalPatches = []; + List _compatiblePackages = []; Map filteredPatches = >{}; File? _outFile; @@ -45,6 +47,24 @@ class PatcherAPI { } } + List getCompatiblePackages() { + final List compatiblePackages = []; + for (final Patch patch in _patches) { + for (final Package package in patch.compatiblePackages) { + if (!compatiblePackages.contains(package.name)) { + compatiblePackages.add(package.name); + } + } + } + return compatiblePackages; + } + + List getUniversalPatches() { + return _patches + .where((patch) => patch.compatiblePackages.isEmpty) + .toList(); + } + Future _loadPatches() async { try { if (_patches.isEmpty) { @@ -56,6 +76,9 @@ class PatcherAPI { } _patches = List.empty(); } + + _compatiblePackages = getCompatiblePackages(); + _universalPatches = getUniversalPatches(); } Future> getFilteredInstalledApps( @@ -63,41 +86,32 @@ class PatcherAPI { ) async { final List filteredApps = []; final bool allAppsIncluded = - _patches.any((patch) => patch.compatiblePackages.isEmpty) && + _universalPatches.isNotEmpty && showUniversalPatches; if (allAppsIncluded) { - final allPackages = await DeviceApps.getInstalledApplications( + final appList = await DeviceApps.getInstalledApplications( includeAppIcons: true, onlyAppsWithLaunchIntent: true, ); - for (final pkg in allPackages) { - if (!filteredApps.any((app) => app.packageName == pkg.packageName)) { - final appInfo = await DeviceApps.getApp( - pkg.packageName, + + for(final app in appList) { + filteredApps.add(app as ApplicationWithIcon); + } + } + for (final packageName in _compatiblePackages) { + try { + if (!filteredApps.any((app) => app.packageName == packageName)) { + final ApplicationWithIcon? app = await DeviceApps.getApp( + packageName, true, ) as ApplicationWithIcon?; - if (appInfo != null) { - filteredApps.add(appInfo); + if (app != null) { + filteredApps.add(app); } } - } - } - for (final Patch patch in _patches) { - for (final Package package in patch.compatiblePackages) { - try { - if (!filteredApps.any((app) => app.packageName == package.name)) { - final ApplicationWithIcon? app = await DeviceApps.getApp( - package.name, - true, - ) as ApplicationWithIcon?; - if (app != null) { - filteredApps.add(app); - } - } - } on Exception catch (e) { - if (kDebugMode) { - print(e); - } + } on Exception catch (e) { + if (kDebugMode) { + print(e); } } } @@ -105,6 +119,10 @@ class PatcherAPI { } List getFilteredPatches(String packageName) { + if (!_compatiblePackages.contains(packageName)) { + return _universalPatches; + } + final List patches = _patches .where( (patch) =>