Skip to content

Commit

Permalink
fix: improve app list loading speed (ReVanced#1166)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminHalko committed Sep 15, 2023
1 parent b525ea1 commit f4b0a69
Showing 1 changed file with 44 additions and 26 deletions.
70 changes: 44 additions & 26 deletions lib/services/patcher_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class PatcherAPI {
late Directory _tmpDir;
late File _keyStoreFile;
List<Patch> _patches = [];
List<Patch> _universalPatches = [];
List<String> _compatiblePackages = [];
Map filteredPatches = <String, List<Patch>>{};
File? _outFile;

Expand All @@ -45,6 +47,24 @@ class PatcherAPI {
}
}

List<String> getCompatiblePackages() {
final List<String> 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<Patch> getUniversalPatches() {
return _patches
.where((patch) => patch.compatiblePackages.isEmpty)
.toList();
}

Future<void> _loadPatches() async {
try {
if (_patches.isEmpty) {
Expand All @@ -56,55 +76,53 @@ class PatcherAPI {
}
_patches = List.empty();
}

_compatiblePackages = getCompatiblePackages();
_universalPatches = getUniversalPatches();
}

Future<List<ApplicationWithIcon>> getFilteredInstalledApps(
bool showUniversalPatches,
) async {
final List<ApplicationWithIcon> 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);
}
}
}
return filteredApps;
}

List<Patch> getFilteredPatches(String packageName) {
if (!_compatiblePackages.contains(packageName)) {
return _universalPatches;
}

final List<Patch> patches = _patches
.where(
(patch) =>
Expand Down

0 comments on commit f4b0a69

Please sign in to comment.