More efficient loading (addresses #110)

This commit is contained in:
Imran Remtulla
2022-11-14 12:56:52 -05:00
parent 0202224fa6
commit 9576a99a4e

View File

@@ -424,22 +424,28 @@ class AppsProvider with ChangeNotifier {
} }
loadingApps = true; loadingApps = true;
notifyListeners(); notifyListeners();
List<FileSystemEntity> appFiles = (await getAppsDir()) List<App> newApps = (await getAppsDir())
.listSync() .listSync()
.where((item) => item.path.toLowerCase().endsWith('.json')) .where((item) => item.path.toLowerCase().endsWith('.json'))
.map((e) => App.fromJson(jsonDecode(File(e.path).readAsStringSync())))
.toList(); .toList();
apps.clear(); var idsToDelete = apps.values
.map((e) => e.app.id)
.toSet()
.difference(newApps.map((e) => e.id).toSet());
for (var id in idsToDelete) {
apps.remove(id);
}
var sp = SourceProvider(); var sp = SourceProvider();
List<List<String>> errors = []; List<List<String>> errors = [];
for (int i = 0; i < appFiles.length; i++) { for (int i = 0; i < newApps.length; i++) {
App app = var info = await getInstalledInfo(newApps[i].id);
App.fromJson(jsonDecode(File(appFiles[i].path).readAsStringSync()));
var info = await getInstalledInfo(app.id);
try { try {
sp.getSource(app.url); sp.getSource(newApps[i].url);
apps.putIfAbsent(app.id, () => AppInMemory(app, null, info)); apps.putIfAbsent(
newApps[i].id, () => AppInMemory(newApps[i], null, info));
} catch (e) { } catch (e) {
errors.add([app.id, app.name, e.toString()]); errors.add([newApps[i].id, newApps[i].name, e.toString()]);
} }
} }
if (errors.isNotEmpty) { if (errors.isNotEmpty) {