Enable icon caching (#1837)

This commit is contained in:
Imran Remtulla
2024-09-27 19:01:57 -04:00
parent cad14dd6a4
commit 77bebc48bc
3 changed files with 27 additions and 4 deletions

View File

@@ -248,7 +248,8 @@ class _AppPageState extends State<AppPage> {
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(

View File

@@ -416,6 +416,8 @@ class AppsPageState extends State<AppsPage> {
? Image.memory(
listedApps[appIndex].icon!,
gaplessPlayback: true,
opacity: AlwaysStoppedAnimation(
listedApps[appIndex].installedInfo == null ? 0.6 : 1),
)
: Row(
mainAxisSize: MainAxisSize.min,

View File

@@ -375,6 +375,7 @@ class AppsProvider with ChangeNotifier {
late Stream<FGBGType>? foregroundStream;
late StreamSubscription<FGBGType>? foregroundSubscription;
late Directory APKDir;
late Directory iconsCacheDir;
late SettingsProvider settingsProvider = SettingsProvider();
Iterable<AppInMemory> 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<void> updateAppIcon(String? appId) async {
Future<void> 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-'))