Skip to content

Commit

Permalink
icons
Browse files Browse the repository at this point in the history
  • Loading branch information
nkitsaini committed Mar 29, 2024
1 parent d85e064 commit 47feb79
Show file tree
Hide file tree
Showing 7 changed files with 35,998 additions and 62 deletions.
1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@
<data android:mimeType="text/plain"/>
</intent>
</queries>
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
</manifest>
34,910 changes: 34,910 additions & 0 deletions lib/app_icon.dart

Large diffs are not rendered by default.

123 changes: 123 additions & 0 deletions lib/app_list.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import 'package:device_apps/device_apps.dart';
import 'package:cache_manager/cache_manager.dart';
// import 'package:json_serializable/builder.dart';
import 'dart:convert';

import 'package:flutter/material.dart';
// import 'package:json_serializable/json_serializable.dart';

class CacheData {
final List<GridApp> grid;
List<App> apps;

CacheData(this.grid, this.apps);

factory CacheData.fromJson(Map<String, dynamic> json) => CacheData(
(json['grid'] as List).map((x) => gridAppFromJson(x)).toList(),
(json['apps'] as List).map((x) => App.fromJson(x)).toList());

static gridAppToJson(GridApp gridApp) {
return gridApp.toJson();
}

static GridApp gridAppFromJson(Map<String, dynamic> json) {
return GridApp.fromJson(json);
}

Map<String, dynamic> toJson() {
return {
'grid': grid.map((x) => gridAppToJson(x)).toList(),
'apps': apps.map((x) => x.toJson()).toList()
};
}
}

class GridApp {
final App app;
int? iconCodePoint;

GridApp(this.app, this.iconCodePoint);

factory GridApp.fromJson(Map<String, dynamic> json) {
return GridApp(App.fromJson(json['app']), json['iconCodePoint']);
}

Map<String, dynamic> toJson() {
return {'app': app.toJson(), 'iconCodePoint': iconCodePoint};
}
}

class App {
final String appName, packageName;

App(this.appName, this.packageName);

factory App.fromJson(Map<String, dynamic> json) =>
App(json['appName'], json['packageName']);

factory App.fromApplication(Application application) =>
App(application.appName, application.packageName);

Map<String, dynamic> toJson() {
return {'appName': appName, 'packageName': packageName};
}
}

class AppListCacher extends ChangeNotifier {
String cacheKey = 'app_list_cache_v3';
CacheData? data;

Future readCache() async {
dynamic value = await ReadCache.getString(key: cacheKey);
if (value != null) {
print("Reading from cache");
data = CacheData.fromJson(jsonDecode(value));
notifyListeners();
} else {
print("Cache read miss");
}
}

Future<List<App>> getAppList() async {
if (data == null) {
await readCache();
}
// Do ~blocking get if there was nothing in cache
if (data == null) {
await updateCache();
} else {
updateCache();
}

return data!.apps;
}

Future<List<Application>> _readApps() async {
List<Application> apps = (await DeviceApps.getInstalledApplications(
includeSystemApps: true,
onlyAppsWithLaunchIntent: true,
));
return apps;
}

flushChanges() {
assert(data != null);
WriteCache.setString(key: cacheKey, value: jsonEncode(data!.toJson()));
print("cache updated");
notifyListeners();
}

Future updateCache() async {
print("updating cache");
List<App> apps = (await _readApps())
.where((x) => x.enabled)
.map((x) => App.fromApplication(x))
.toList();
if (data == null) {
data = CacheData([], apps);
} else {
data!.apps = apps;
}
flushChanges();
}
}
Loading

0 comments on commit 47feb79

Please sign in to comment.