Attempted fix for #549

APK stored in "ext storage" dir if cache dir not available
This commit is contained in:
Imran Remtulla
2023-05-22 13:02:18 -04:00
parent 03fc6a530f
commit d24f2b4e6d

View File

@@ -108,6 +108,7 @@ class AppsProvider with ChangeNotifier {
bool isForeground = true; bool isForeground = true;
late Stream<FGBGType>? foregroundStream; late Stream<FGBGType>? foregroundStream;
late StreamSubscription<FGBGType>? foregroundSubscription; late StreamSubscription<FGBGType>? foregroundSubscription;
late Directory APKDir;
Iterable<AppInMemory> getAppValues() => apps.values.map((a) => a.deepCopy()); Iterable<AppInMemory> getAppValues() => apps.values.map((a) => a.deepCopy());
@@ -119,13 +120,21 @@ class AppsProvider with ChangeNotifier {
if (isForeground) await refreshInstallStatuses(); if (isForeground) await refreshInstallStatuses();
}); });
() async { () async {
var cacheDirs = await getExternalCacheDirectories();
if (cacheDirs?.isNotEmpty ?? false) {
APKDir = cacheDirs!.first;
} else {
APKDir =
Directory('${(await getExternalStorageDirectory())!.path}/apks');
if (!APKDir.existsSync()) {
APKDir.createSync();
}
}
// Load Apps into memory (in background, this is done later instead of in the constructor) // Load Apps into memory (in background, this is done later instead of in the constructor)
await loadApps(); await loadApps();
// Delete any partial APKs // Delete any partial APKs
var cutoff = DateTime.now().subtract(const Duration(days: 7)); var cutoff = DateTime.now().subtract(const Duration(days: 7));
(await getExternalCacheDirectories()) APKDir.listSync()
?.first
.listSync()
.where((element) => .where((element) =>
element.path.endsWith('.part') || element.path.endsWith('.part') ||
element.statSync().modified.isBefore(cutoff)) element.statSync().modified.isBefore(cutoff))
@@ -138,7 +147,7 @@ class AppsProvider with ChangeNotifier {
Future<File> downloadFile( Future<File> downloadFile(
String url, String fileNameNoExt, Function? onProgress, String url, String fileNameNoExt, Function? onProgress,
{bool useExisting = true, Map<String, String>? headers}) async { {bool useExisting = true, Map<String, String>? headers}) async {
var destDir = (await getExternalCacheDirectories())!.first.path; var destDir = APKDir.path;
var req = Request('GET', Uri.parse(url)); var req = Request('GET', Uri.parse(url));
if (headers != null) { if (headers != null) {
req.headers.addAll(headers); req.headers.addAll(headers);
@@ -783,14 +792,14 @@ class AppsProvider with ChangeNotifier {
} }
Future<void> removeApps(List<String> appIds) async { Future<void> removeApps(List<String> appIds) async {
var apkFiles = (await getExternalCacheDirectories())?.first.listSync(); var apkFiles = APKDir.listSync();
for (var appId in appIds) { for (var appId in appIds) {
File file = File('${(await getAppsDir()).path}/$appId.json'); File file = File('${(await getAppsDir()).path}/$appId.json');
if (file.existsSync()) { if (file.existsSync()) {
file.deleteSync(recursive: true); file.deleteSync(recursive: true);
} }
apkFiles apkFiles
?.where( .where(
(element) => element.path.split('/').last.startsWith('$appId-')) (element) => element.path.split('/').last.startsWith('$appId-'))
.forEach((element) { .forEach((element) {
element.delete(recursive: true); element.delete(recursive: true);