diff --git a/lib/src/internal/constants.dart b/lib/src/internal/constants.dart index fc847230..ebc1411e 100644 --- a/lib/src/internal/constants.dart +++ b/lib/src/internal/constants.dart @@ -78,4 +78,5 @@ class PMConstants { 'ohos.permission.WRITE_IMAGEVIDEO', ]; + static const cancelTokenKey = 'cancelToken'; } diff --git a/lib/src/internal/plugin.dart b/lib/src/internal/plugin.dart index 806548be..71db838f 100644 --- a/lib/src/internal/plugin.dart +++ b/lib/src/internal/plugin.dart @@ -48,13 +48,29 @@ class PMCancelToken { } mixin BasePlugin { - MethodChannel _channel = const MethodChannel(PMConstants.channelPrefix); + MethodChannel _channel = const PMMethodChannel(PMConstants.channelPrefix); final Map onlyAddPermission = { 'onlyAddPermission': true, }; } +class PMMethodChannel extends MethodChannel { + const PMMethodChannel(String name) : super(name); + + @override + Future invokeMethod(String method, [dynamic arguments]) { + if (arguments is! Map) { + return super.invokeMethod(method, arguments); + } + final cancelToken = arguments[PMConstants.cancelTokenKey]; + if (cancelToken == null) { + arguments[PMConstants.cancelTokenKey] = PMCancelToken().key; + } + return super.invokeMethod(method, arguments); + } +} + class VerboseLogMethodChannel extends MethodChannel { VerboseLogMethodChannel({ required String name, @@ -154,7 +170,7 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin, OhosPlugin { logFilePath: logPath, ); } else { - _channel = const MethodChannel(PMConstants.channelPrefix); + _channel = const PMMethodChannel(PMConstants.channelPrefix); } } @@ -265,17 +281,26 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin, OhosPlugin { } } + void _setCancelToken( + Map params, PMCancelToken? cancelToken) { + if (cancelToken != null) { + params[PMConstants.cancelTokenKey] = cancelToken.key; + } + } + /// Get thumbnail of asset id. Future getThumbnail({ required String id, required ThumbnailOption option, PMProgressHandler? progressHandler, + PMCancelToken? cancelToken, }) { final Map params = { 'id': id, 'option': option.toMap(), }; _injectProgressHandlerParams(params, progressHandler); + _setCancelToken(params, cancelToken); return _channel.invokeMethod(PMConstants.mGetThumb, params); } @@ -283,12 +308,14 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin, OhosPlugin { String id, { PMProgressHandler? progressHandler, PMDarwinAVFileType? darwinFileType, + PMCancelToken? cancelToken, }) { final Map params = { 'id': id, 'darwinFileType': darwinFileType, }; _injectProgressHandlerParams(params, progressHandler); + _setCancelToken(params, cancelToken); return _channel.invokeMethod(PMConstants.mGetOriginBytes, params); } @@ -305,6 +332,7 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin, OhosPlugin { PMProgressHandler? progressHandler, int subtype = 0, PMDarwinAVFileType? darwinFileType, + PMCancelToken? cancelToken, }) async { final params = { 'id': id, @@ -313,6 +341,7 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin, OhosPlugin { 'darwinFileType': darwinFileType?.value ?? 0, }; _injectProgressHandlerParams(params, progressHandler); + _setCancelToken(params, cancelToken); return _channel.invokeMethod(PMConstants.mGetFullFile, params); } @@ -512,6 +541,7 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin, OhosPlugin { Future getMediaUrl( AssetEntity entity, { PMProgressHandler? progressHandler, + PMCancelToken? cancelToken, }) async { if (PlatformUtils.isOhos) { return entity.id; @@ -521,6 +551,7 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin, OhosPlugin { 'type': entity.typeInt, }; _injectProgressHandlerParams(params, progressHandler); + _setCancelToken(params, cancelToken); return _channel.invokeMethod(PMConstants.mGetMediaUrl, params); } diff --git a/lib/src/types/entity.dart b/lib/src/types/entity.dart index 8162bcb4..34f76450 100644 --- a/lib/src/types/entity.dart +++ b/lib/src/types/entity.dart @@ -583,6 +583,7 @@ class AssetEntity { bool isOrigin = true, bool withSubtype = false, PMProgressHandler? progressHandler, + PMCancelToken? cancelToken, PMDarwinAVFileType? darwinFileType, }) { return _getFile( @@ -590,6 +591,7 @@ class AssetEntity { subtype: withSubtype ? subtype : 0, progressHandler: progressHandler, darwinFileType: darwinFileType, + cancelToken: cancelToken, ); } @@ -625,6 +627,7 @@ class AssetEntity { ThumbnailFormat format = ThumbnailFormat.jpeg, int quality = 100, PMProgressHandler? progressHandler, + PMCancelToken? cancelToken, int frame = 0, }) { assert(() { @@ -655,7 +658,11 @@ class AssetEntity { return true; }()); - return thumbnailDataWithOption(option, progressHandler: progressHandler); + return thumbnailDataWithOption( + option, + progressHandler: progressHandler, + cancelToken: cancelToken, + ); } /// Obtain the thumbnail data with the given customized [ThumbnailOption]. @@ -666,6 +673,7 @@ class AssetEntity { Future thumbnailDataWithOption( ThumbnailOption option, { PMProgressHandler? progressHandler, + PMCancelToken? cancelToken, }) { assert(() { _checkThumbnailAssertion(); @@ -683,6 +691,7 @@ class AssetEntity { id: id, option: option, progressHandler: progressHandler, + cancelToken: cancelToken, ); } @@ -736,10 +745,12 @@ class AssetEntity { /// * https://developer.apple.com/documentation/avfoundation/avurlasset Future getMediaUrl({ PMProgressHandler? progressHandler, + PMCancelToken? cancelToken, }) { return plugin.getMediaUrl( this, progressHandler: progressHandler, + cancelToken: cancelToken, ); } @@ -754,6 +765,7 @@ class AssetEntity { PMProgressHandler? progressHandler, int subtype = 0, PMDarwinAVFileType? darwinFileType, + PMCancelToken? cancelToken, }) async { assert( _platformMatched, @@ -768,6 +780,7 @@ class AssetEntity { progressHandler: progressHandler, subtype: subtype, darwinFileType: darwinFileType, + cancelToken: cancelToken, ); if (path == null) { return null; @@ -777,6 +790,7 @@ class AssetEntity { Future _getOriginBytes({ PMProgressHandler? progressHandler, + PMCancelToken? cancelToken, }) async { assert( _platformMatched, @@ -788,11 +802,19 @@ class AssetEntity { if (Platform.isAndroid) { final sdkInt = int.parse(await plugin.getSystemVersion()); if (sdkInt > 29) { - return plugin.getOriginBytes(id, progressHandler: progressHandler); + return plugin.getOriginBytes( + id, + progressHandler: progressHandler, + cancelToken: cancelToken, + ); } } if (PlatformUtils.isOhos) { - return plugin.getOriginBytes(id, progressHandler: progressHandler); + return plugin.getOriginBytes( + id, + progressHandler: progressHandler, + cancelToken: cancelToken, + ); } final File? file = await originFile; return file?.readAsBytes();