Skip to content

Commit

Permalink
feat: add method move to trash (#1005)
Browse files Browse the repository at this point in the history
Signed-off-by: CaiJingLong <cjl_spy@163.com>
  • Loading branch information
CaiJingLong authored Nov 1, 2023
1 parent 3a69fa8 commit bd7a276
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ User must follow the below methods to ensure permissions were granted:
2. `PhotoManager.setIgnorePermissionCheck(true)`, ignoring permission checks,
handle permission with other mechanisms.

Behavior changes to the method of the method of `PhotoManager.editor.deleteWithIds`:

- The behavior changes to delete instead of moving to Trash on Android 30+. (#959)
- Provides a new method to move resources to the Trash. (`PhotoManager.editor.android.moveToTrash`), the method only support Android 30+. (#1005)

### Fixes

- Correct the key when fetching video info with MMR on Android. (#997)
Expand Down
8 changes: 8 additions & 0 deletions README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,14 @@ await PhotoManager.editor.android.moveAssetToAnother(

(对于 Android 30+,由于系统限制,此功能当前被屏蔽。)

##### 将资源移动到废纸篓

```dart
await PhotoManager.editor.android.moveToTrash(list);
```

这个方法用于将资源移动到废纸篓,它仅支持安卓 API 30+,低于 30 的 API 会抛出异常。

##### 移除所有不存在的资源

这将删除所有本地不存在的相册条目。
Expand Down
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -798,10 +798,10 @@ no matter what language has been set to devices.
To change the default language, see the following steps:

- Open your iOS project (Runner.xcworkspace) using Xcode.
![Edit localizations in Xcode 1](https://raw.githubusercontent.com/CaiJingLong/some_asset/master/iosFlutterProjectEditinginXcode.png)
![Edit localizations in Xcode 1](https://raw.githubusercontent.com/CaiJingLong/some_asset/master/iosFlutterProjectEditinginXcode.png)

- Select the project "Runner" and in the localizations table, click on the + icon.
![Edit localizations in Xcode 2](https://raw.githubusercontent.com/CaiJingLong/some_asset/master/iosFlutterAddLocalization.png)
![Edit localizations in Xcode 2](https://raw.githubusercontent.com/CaiJingLong/some_asset/master/iosFlutterAddLocalization.png)

- Select the adequate language(s) you want to retrieve localized strings.
- Validate the popup screen without any modification.
Expand Down Expand Up @@ -918,6 +918,15 @@ for each entities' deletion, we have no way to avoid them.
Make sure you're using the correct method,
and your customers accept repeatedly confirmations.

##### Move entities to trash

```dart
await PhotoManager.editor.android.moveToTrash(list);
```

The method support android 30 or higher. It will move the entities to trash.
If the method is called on android 29 or lower, it will throw an exception.

#### Features for iOS or macOS

##### Create a folder
Expand Down Expand Up @@ -990,4 +999,4 @@ PhotoManager.editor.darwin.deletePath();

[`LocallyAvailableBuilder`]: https://github.com/fluttercandies/flutter_wechat_assets_picker/blob/2055adfa74370339d10e6f09adef72f2130d2380/lib/src/widget/builder/locally_available_builder.dart

[flutter/flutter#20522]: https://github.com/flutter/flutter/issues/20522
[flutter/flutter#20522]: https://github.com/flutter/flutter/issues/20522
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ class Methods {

const val getLatLng = "getLatLngAndroidQ"
const val notify = "notify"

const val deleteWithIds = "deleteWithIds"
const val moveToTrash = "moveToTrash"

const val saveImage = "saveImage"
const val saveImageWithPath = "saveImageWithPath"
const val saveVideo = "saveVideo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class PhotoManagerDeleteManager(val context: Context, private var activity: Acti
ids.toTypedArray()
)
}
//
// enum class Action {
// Delete,
// Trash,
// }

private var androidRHandler: ResultHandler? = null

Expand All @@ -67,4 +72,18 @@ class PhotoManagerDeleteManager(val context: Context, private var activity: Acti
0
)
}

@RequiresApi(Build.VERSION_CODES.R)
fun moveToTrashInApi30(uris: List<Uri?>, resultHandler: ResultHandler) {
this.androidRHandler = resultHandler
val pendingIntent = MediaStore.createTrashRequest(cr, uris.mapNotNull { it }, true)
activity?.startIntentSenderForResult(
pendingIntent.intentSender,
androidRDeleteRequestCode,
null,
0,
0,
0
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,26 @@ class PhotoManagerPlugin(
}
}

Methods.moveToTrash -> {
try {
val ids = call.argument<List<String>>("ids")!!
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val uris = ids.map { photoManager.getUri(it) }.toList()
deleteManager.moveToTrashInApi30(uris, resultHandler)
} else {
LogUtils.error("The API 29 or lower have not the IS_TRASHED row in MediaStore.")
resultHandler.replyError(
"The api not support 29 or lower.",
"",
UnsupportedOperationException("The api cannot be used in 29 or lower.")
)
}
} catch (e: Exception) {
LogUtils.error("deleteWithIds failed", e)
resultHandler.replyError("deleteWithIds failed")
}
}

Methods.removeNoExistsAssets -> {
photoManager.removeAllExistsAssets(resultHandler)
}
Expand Down
1 change: 1 addition & 0 deletions lib/src/internal/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class PMConstants {
static const String mNotify = 'notify';
static const String mForceOldApi = 'forceOldApi';
static const String mDeleteWithIds = 'deleteWithIds';
static const String mMoveToTrash = 'moveToTrash';
static const String mSaveImage = 'saveImage';
static const String mSaveImageWithPath = 'saveImageWithPath';
static const String mSaveVideo = 'saveVideo';
Expand Down
5 changes: 5 additions & 0 deletions lib/src/internal/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,9 @@ class AndroidEditor {
Future<bool> removeAllNoExistsAsset() {
return plugin.androidRemoveNoExistsAssets();
}

/// Move to trash
Future<List<String>> moveToTrash(List<AssetEntity> list) {
return plugin.moveToTrash(list);
}
}
7 changes: 7 additions & 0 deletions lib/src/internal/plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin {
return deleted.cast<String>();
}

Future<List<String>> moveToTrash(List<AssetEntity> list) {
return _channel.invokeMethod(
PMConstants.mMoveToTrash,
<String, dynamic>{'ids': list.map((e) => e.id).toList()},
).then((value) => value.cast<String>());
}

final Map<String, dynamic> onlyAddPermission = <String, dynamic>{
'onlyAddPermission': true,
};
Expand Down

0 comments on commit bd7a276

Please sign in to comment.