Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 20 additions & 10 deletions lib/esys_flutter_share_plus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Share {

static Future<void>? _initialise;

static bool _useSeparateActivity = true;

/// Initializes the Share plugin.
///
/// This method should be called at app startup or before starting to work with sharing.
Expand All @@ -25,8 +27,12 @@ class Share {
/// a clean state before any sharing operations.
static Future<void> init({bool useSeparateActivity = true}) async {
await (_initialise ??= _init(useSeparateActivity));
await _clearTempShareDirectory();
}

/// Clearing the directory with temporary files.
static Future<void> clear() => _clearTempShareDirectory();

/// Sends a text to other apps.
static void text(String title, String text, String mimeType) {
Map argsMap = <String, String>{
Expand Down Expand Up @@ -93,7 +99,7 @@ class Share {
};

_channel.invokeMethod('file', argsMap).whenComplete(() {
file.delete();
if (!_useSeparateActivity || Platform.isIOS) file.delete();
});
}

Expand Down Expand Up @@ -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();
}
}
});
}
Expand Down Expand Up @@ -168,7 +176,7 @@ class Share {
};

_channel.invokeMethod('file', argsMap).whenComplete(() {
destFile.delete();
if (!_useSeparateActivity || Platform.isIOS) destFile.delete();
});
}

Expand Down Expand Up @@ -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<void> _init(bool useSeparateActivity) async {
_useSeparateActivity = useSeparateActivity;
if (Platform.isAndroid) {
await _initNativeAndroid(useSeparateActivity);
await _initNativeAndroid();
}
_clearTempShareDirectory();
}

static Future<void> _initNativeAndroid(bool useSeparateActivity) async {
_channel.invokeMethod('init', useSeparateActivity);
static Future<void> _initNativeAndroid() async {
_channel.invokeMethod('init', _useSeparateActivity);
}

static Future<Directory> _getDirectoryForShareFile() async {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down