From 77bebc48bcd878787e743ea9f9ca15eea2f9da3a Mon Sep 17 00:00:00 2001 From: Imran Remtulla Date: Fri, 27 Sep 2024 19:01:57 -0400 Subject: [PATCH] Enable icon caching (#1837) --- lib/pages/app.dart | 3 ++- lib/pages/apps.dart | 2 ++ lib/providers/apps_provider.dart | 26 +++++++++++++++++++++++--- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/pages/app.dart b/lib/pages/app.dart index cda1d8d..00a4b2e 100644 --- a/lib/pages/app.dart +++ b/lib/pages/app.dart @@ -248,7 +248,8 @@ class _AppPageState extends State { children: [ const SizedBox(height: 20), FutureBuilder( - future: appsProvider.updateAppIcon(app?.app.id), + future: + appsProvider.updateAppIcon(app?.app.id, ignoreCache: true), builder: (ctx, val) { return app?.icon != null ? Row( diff --git a/lib/pages/apps.dart b/lib/pages/apps.dart index d2086c9..505a98d 100644 --- a/lib/pages/apps.dart +++ b/lib/pages/apps.dart @@ -416,6 +416,8 @@ class AppsPageState extends State { ? Image.memory( listedApps[appIndex].icon!, gaplessPlayback: true, + opacity: AlwaysStoppedAnimation( + listedApps[appIndex].installedInfo == null ? 0.6 : 1), ) : Row( mainAxisSize: MainAxisSize.min, diff --git a/lib/providers/apps_provider.dart b/lib/providers/apps_provider.dart index 2f17835..a444ab0 100644 --- a/lib/providers/apps_provider.dart +++ b/lib/providers/apps_provider.dart @@ -375,6 +375,7 @@ class AppsProvider with ChangeNotifier { late Stream? foregroundStream; late StreamSubscription? foregroundSubscription; late Directory APKDir; + late Directory iconsCacheDir; late SettingsProvider settingsProvider = SettingsProvider(); Iterable getAppValues() => apps.values.map((a) => a.deepCopy()); @@ -393,12 +394,21 @@ class AppsProvider with ChangeNotifier { var cacheDirs = await getExternalCacheDirectories(); if (cacheDirs?.isNotEmpty ?? false) { APKDir = cacheDirs!.first; + iconsCacheDir = Directory('${cacheDirs.first.path}/icons'); + if (!iconsCacheDir.existsSync()) { + iconsCacheDir.createSync(); + } } else { APKDir = Directory('${(await getExternalStorageDirectory())!.path}/apks'); if (!APKDir.existsSync()) { APKDir.createSync(); } + iconsCacheDir = + Directory('${(await getExternalStorageDirectory())!.path}/icons'); + if (!iconsCacheDir.existsSync()) { + iconsCacheDir.createSync(); + } } if (!isBg) { // Load Apps into memory (in background processes, this is done later instead of in the constructor) @@ -1297,10 +1307,16 @@ class AppsProvider with ChangeNotifier { notifyListeners(); } - Future updateAppIcon(String? appId) async { + Future updateAppIcon(String? appId, {bool ignoreCache = false}) async { if (apps[appId]?.icon == null) { - var icon = - (await apps[appId]?.installedInfo?.applicationInfo?.getAppIcon()); + var cachedIcon = File('${iconsCacheDir.path}/$appId.png'); + var alreadyCached = cachedIcon.existsSync() && !ignoreCache; + var icon = alreadyCached + ? (await cachedIcon.readAsBytes()) + : (await apps[appId]?.installedInfo?.applicationInfo?.getAppIcon()); + if (icon != null && !alreadyCached) { + cachedIcon.writeAsBytes(icon.toList()); + } if (icon != null) { apps.update( apps[appId]!.app.id, @@ -1351,6 +1367,10 @@ class AppsProvider with ChangeNotifier { if (file.existsSync()) { file.deleteSync(recursive: true); } + File iconFile = File('$iconsCacheDir/$appId.png'); + if (iconFile.existsSync()) { + iconFile.deleteSync(recursive: true); + } apkFiles .where( (element) => element.path.split('/').last.startsWith('$appId-'))