Skip to content

An open-source cross-platform alternative to AirDrop

fix: clear ios cache when shared from another file (#946)

Tien Do Nam674b45923ca8
diff --git a/app/ios/Podfile.lock b/app/ios/Podfile.lock
index 263451d3..9037780d 100644
--- a/app/ios/Podfile.lock
+++ b/app/ios/Podfile.lock
@@ -1,4 +1,6 @@
PODS:
+ - app_group_directory (1.0.0):
+ - Flutter
- connectivity_plus (0.0.1):
- Flutter
- ReachabilitySwift
@@ -92,6 +94,7 @@ PODS:
- Flutter
DEPENDENCIES:
+ - app_group_directory (from `.symlinks/plugins/app_group_directory/ios`)
- connectivity_plus (from `.symlinks/plugins/connectivity_plus/ios`)
- device_info_plus (from `.symlinks/plugins/device_info_plus/ios`)
- file_picker (from `.symlinks/plugins/file_picker/ios`)
@@ -124,6 +127,8 @@ SPEC REPOS:
- SwiftyGif
EXTERNAL SOURCES:
+ app_group_directory:
+ :path: ".symlinks/plugins/app_group_directory/ios"
connectivity_plus:
:path: ".symlinks/plugins/connectivity_plus/ios"
device_info_plus:
@@ -170,6 +175,7 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/wakelock_plus/ios"
SPEC CHECKSUMS:
+ app_group_directory: 7bf9f8f9819ead554de29da7c25fb7a680d6a9a0
connectivity_plus: bf0076dd84a130856aa636df1c71ccaff908fa1d
device_info_plus: c6fb39579d0f423935b0c9ce7ee2f44b71b9fce6
DKImagePickerController: b512c28220a2b8ac7419f21c491fc8534b7601ac
diff --git a/app/lib/init.dart b/app/lib/init.dart
index a517a1f6..727fcf7b 100644
--- a/app/lib/init.dart
+++ b/app/lib/init.dart
@@ -170,7 +170,7 @@ Future<void> postInit(BuildContext context, Ref ref, bool appStart, void Functio
if (appStart && !hasInitialShare && (checkPlatformWithGallery() || checkPlatformCanReceiveShareIntent())) {
// Clear cache on every app start.
// If we received a share intent, then don't clear it, otherwise the shared file will be lost.
- ref.dispatch(ClearCacheAction());
+ ref.dispatchAsync(ClearCacheAction()); // ignore: unawaited_futures
}
diff --git a/app/lib/provider/selection/selected_sending_files_provider.dart b/app/lib/provider/selection/selected_sending_files_provider.dart
index 439af47a..461c0b21 100644
--- a/app/lib/provider/selection/selected_sending_files_provider.dart
+++ b/app/lib/provider/selection/selected_sending_files_provider.dart
@@ -188,7 +188,7 @@ class RemoveSelectedFileAction extends ReduxAction<SelectedSendingFilesNotifier,
@override
void after() {
if (state.isEmpty) {
- global.dispatch(ClearCacheAction());
+ global.dispatchAsync(ClearCacheAction()); // ignore: discarded_futures
}
}
}
@@ -202,6 +202,6 @@ class ClearSelectionAction extends ReduxAction<SelectedSendingFilesNotifier, Lis
@override
void after() {
- global.dispatch(ClearCacheAction());
+ global.dispatchAsync(ClearCacheAction()); // ignore: discarded_futures
}
}
diff --git a/app/lib/util/native/cache_helper.dart b/app/lib/util/native/cache_helper.dart
index 325eeb79..7e55d556 100644
--- a/app/lib/util/native/cache_helper.dart
+++ b/app/lib/util/native/cache_helper.dart
@@ -1,40 +1,71 @@
-// ignore_for_file: discarded_futures
+// ignore_for_file: discarded_futures, unawaited_futures
import 'dart:io';
+import 'dart:isolate';
+import 'package:app_group_directory/app_group_directory.dart';
import 'package:file_picker/file_picker.dart';
-import 'package:flutter/foundation.dart';
+import 'package:flutter/services.dart';
+import 'package:localsend_app/util/file_path_helper.dart';
+import 'package:localsend_app/util/logger.dart';
import 'package:localsend_app/util/native/platform_check.dart';
+import 'package:logging/logging.dart';
import 'package:path_provider/path_provider.dart';
import 'package:refena_flutter/refena_flutter.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
+final _logger = Logger('ClearCacheAction');
+
/// Clears the cache.
-/// It is written in a "fire-and-forget" way, so we don't need to wait until everything is cleared.
-class ClearCacheAction extends GlobalAction {
+/// It runs on a separate isolate to avoid blocking the UI.
+class ClearCacheAction extends AsyncGlobalAction {
@override
- void reduce() {}
+ Future<void> reduce() async {
+ // The token statement must be outside the lambda because it must be executed on the root isolate.
+ final token = RootIsolateToken.instance!;
+ await Isolate.run(() => _clear(token));
+ }
+}
- @override
- void after() {
- FilePicker.platform.clearTemporaryFiles().then((_) {}).catchError((error) {
- ref.message('Failed to clear file picker cache: $error');
- });
-
- PhotoManager.clearFileCache().catchError((error) => ref.message('Failed to clear photo manager cache: $error'));
-
- if (checkPlatform([TargetPlatform.iOS, TargetPlatform.android])) {
- getTemporaryDirectory().then((cacheDir) {
- cacheDir.list().listen((event) {
- if (event is File) {
- event.delete().then((_) {}).catchError((error) {
- ref.message('Failed to delete file: $error');
+Future<void> _clear(RootIsolateToken token) async {
+ initLogger(Level.ALL);
+ BackgroundIsolateBinaryMessenger.ensureInitialized(token);
+
+ final futures = (
+ FilePicker.platform.clearTemporaryFiles(),
+ PhotoManager.clearFileCache(),
+ checkPlatform([TargetPlatform.iOS, TargetPlatform.android])
+ ? getTemporaryDirectory().then((cacheDir) {
+ cacheDir.list().listen((event) {
+ if (event is File) {
+ event.delete().then((_) {}).catchError((error) {
+ _logger.warning('Failed to delete file: $error');
+ });
+ }
});
- }
- });
- }).catchError((error) {
- ref.message('Failed to get temporary directory: $error');
- });
- }
+ })
+ : Future.value(),
+ checkPlatform([TargetPlatform.iOS])
+ ? AppGroupDirectory.getAppGroupDirectory('group.org.localsend.localsendApp').then((directory) async {
+ if (directory == null) {
+ _logger.warning('Failed to get app group directory');
+ return;
+ }
+
+ // delete contents of the directory (only files, not directories)
+ await for (final entry in directory.list(recursive: false, followLinks: false)) {
+ if (entry is File && !entry.path.fileName.startsWith('.')) {
+ _logger.info('Deleting ${entry.path}');
+ entry.deleteSync();
+ }
+ }
+ })
+ : Future.value(),
+ ).wait;
+
+ try {
+ await futures;
+ } catch (e) {
+ _logger.warning('Failed to clear cache: $e');
}
}
diff --git a/app/pubspec.lock b/app/pubspec.lock
index e04286aa..bc97c52a 100644
--- a/app/pubspec.lock
+++ b/app/pubspec.lock
@@ -25,6 +25,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.2"
+ app_group_directory:
+ dependency: "direct main"
+ description:
+ name: app_group_directory
+ sha256: ad89800fd55133b46e1f6940ac6b974562f7fb6394c662c0f4422b90167f2416
+ url: "https://pub.dev"
+ source: hosted
+ version: "2.0.0"
archive:
dependency: transitive
description:
diff --git a/app/pubspec.yaml b/app/pubspec.yaml
index 259ad331..f6544b91 100644
--- a/app/pubspec.yaml
+++ b/app/pubspec.yaml
@@ -10,6 +10,7 @@ environment:
sdk: ">=3.2.0 <4.0.0"
dependencies:
+ app_group_directory: 2.0.0
basic_utils: 5.7.0
collection: ^1.18.0 # allow newer versions, so it can compile with newer Flutter versions
common: