diff --git a/CHANGELOG.md b/CHANGELOG.md index 5aedd85..18a28b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 2.5.1 + +* **Fixed** Android: an issue where files were deleted before being shared to another activity. +* **Added** public method `clean()` to trigger the cleanup of the temporary files directory. + ## 2.5.0 * Update Android `compileSdkVersion` to 35 diff --git a/README.md b/README.md index dc07e35..2f19637 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,15 @@ end Instead, if you have already a non-swift project, you can check this issue to solve the problem: [Friction adding swift plugin to objective-c project](https://github.com/flutter/flutter/issues/16049). +## IMPORTANT Note for Android +On Android, the Share function launches by default in a separate system task and activity. This maintains the cleanliness of your application's navigation stack. However, in this mode, the application cannot wait for the process to complete or reliably receive its result. + +Therefore, we cannot determine precisely whether the user has finished sharing the file to safely delete the temporary files. As a result, the temporary file directory is cleared each time a new sharing operation is initiated. + +You can also force a cleanup when you are certain the user has finished with the Share function (for example, when leaving the screen of your app from which sharing was launched). To do this, use the `clean()` method. + +Alternative option: You can disable launching in a separate system activity by setting the parameter `useSeparateActivity: false` in the `init()` method. In this case, the result of the Share operation will be returned to your application, and the cleanup of temporary files will happen automatically. + ## Usage Import: diff --git a/lib/esys_flutter_share_plus.dart b/lib/esys_flutter_share_plus.dart index 542da1b..ad28b4f 100644 --- a/lib/esys_flutter_share_plus.dart +++ b/lib/esys_flutter_share_plus.dart @@ -13,6 +13,8 @@ class Share { static Future? _initialise; + static bool _useSeparateActivity = true; + /// Initializes the Share plugin. /// /// This method should be called at app startup or before starting to work with sharing. @@ -25,8 +27,12 @@ class Share { /// a clean state before any sharing operations. static Future init({bool useSeparateActivity = true}) async { await (_initialise ??= _init(useSeparateActivity)); + await _clearTempShareDirectory(); } + /// Clearing the directory with temporary files. + static Future clear() => _clearTempShareDirectory(); + /// Sends a text to other apps. static void text(String title, String text, String mimeType) { Map argsMap = { @@ -93,7 +99,7 @@ class Share { }; _channel.invokeMethod('file', argsMap).whenComplete(() { - file.delete(); + if (!_useSeparateActivity || Platform.isIOS) file.delete(); }); } @@ -137,8 +143,10 @@ class Share { 'filePaths': filePaths, }; _channel.invokeMethod('files', argsMap).whenComplete(() { - for (final file in tempFilesList) { - file.delete(); + if (!_useSeparateActivity || Platform.isIOS) { + for (final file in tempFilesList) { + file.delete(); + } } }); } @@ -168,7 +176,7 @@ class Share { }; _channel.invokeMethod('file', argsMap).whenComplete(() { - destFile.delete(); + if (!_useSeparateActivity || Platform.isIOS) destFile.delete(); }); } @@ -212,22 +220,24 @@ class Share { }; _channel.invokeMethod('files', argsMap).whenComplete(() { - for (final file in tempFilesList) { - file.delete(); + if (!_useSeparateActivity || Platform.isIOS) { + for (final file in tempFilesList) { + file.delete(); + } } }); ; } static Future _init(bool useSeparateActivity) async { + _useSeparateActivity = useSeparateActivity; if (Platform.isAndroid) { - await _initNativeAndroid(useSeparateActivity); + await _initNativeAndroid(); } - _clearTempShareDirectory(); } - static Future _initNativeAndroid(bool useSeparateActivity) async { - _channel.invokeMethod('init', useSeparateActivity); + static Future _initNativeAndroid() async { + _channel.invokeMethod('init', _useSeparateActivity); } static Future _getDirectoryForShareFile() async { diff --git a/pubspec.yaml b/pubspec.yaml index a13e95c..05ae997 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: esys_flutter_share_plus description: A Flutter plugin for sharing images & text with other applications. Fork by Innim -version: 2.5.0 +version: 2.5.1 homepage: https://github.com/Innim/esys-flutter-share repository: https://github.com/Innim/esys-flutter-share issue_tracker: https://github.com/Innim/esys-flutter-share/issues