Skip to content

Commit

Permalink
feat: add cancel token for iOS
Browse files Browse the repository at this point in the history
Signed-off-by: Caijinglong <cjl_spy@163.com>
  • Loading branch information
CaiJingLong committed Nov 28, 2024
1 parent c49c400 commit f237252
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/src/internal/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ class PMConstants {
'ohos.permission.WRITE_IMAGEVIDEO',
];

static const cancelTokenKey = 'cancelToken';
}
35 changes: 33 additions & 2 deletions lib/src/internal/plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,29 @@ class PMCancelToken {
}

mixin BasePlugin {
MethodChannel _channel = const MethodChannel(PMConstants.channelPrefix);
MethodChannel _channel = const PMMethodChannel(PMConstants.channelPrefix);

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

class PMMethodChannel extends MethodChannel {
const PMMethodChannel(String name) : super(name);

@override
Future<T?> invokeMethod<T>(String method, [dynamic arguments]) {
if (arguments is! Map) {
return super.invokeMethod<T>(method, arguments);
}
final cancelToken = arguments[PMConstants.cancelTokenKey];
if (cancelToken == null) {
arguments[PMConstants.cancelTokenKey] = PMCancelToken().key;
}
return super.invokeMethod<T>(method, arguments);
}
}

class VerboseLogMethodChannel extends MethodChannel {
VerboseLogMethodChannel({
required String name,
Expand Down Expand Up @@ -154,7 +170,7 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin, OhosPlugin {
logFilePath: logPath,
);
} else {
_channel = const MethodChannel(PMConstants.channelPrefix);
_channel = const PMMethodChannel(PMConstants.channelPrefix);
}
}

Expand Down Expand Up @@ -265,30 +281,41 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin, OhosPlugin {
}
}

void _setCancelToken(
Map<String, dynamic> params, PMCancelToken? cancelToken) {
if (cancelToken != null) {
params[PMConstants.cancelTokenKey] = cancelToken.key;
}
}

/// Get thumbnail of asset id.
Future<typed_data.Uint8List?> getThumbnail({
required String id,
required ThumbnailOption option,
PMProgressHandler? progressHandler,
PMCancelToken? cancelToken,
}) {
final Map<String, dynamic> params = <String, dynamic>{
'id': id,
'option': option.toMap(),
};
_injectProgressHandlerParams(params, progressHandler);
_setCancelToken(params, cancelToken);
return _channel.invokeMethod(PMConstants.mGetThumb, params);
}

Future<typed_data.Uint8List?> getOriginBytes(
String id, {
PMProgressHandler? progressHandler,
PMDarwinAVFileType? darwinFileType,
PMCancelToken? cancelToken,
}) {
final Map<String, dynamic> params = <String, dynamic>{
'id': id,
'darwinFileType': darwinFileType,
};
_injectProgressHandlerParams(params, progressHandler);
_setCancelToken(params, cancelToken);
return _channel.invokeMethod(PMConstants.mGetOriginBytes, params);
}

Expand All @@ -305,6 +332,7 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin, OhosPlugin {
PMProgressHandler? progressHandler,
int subtype = 0,
PMDarwinAVFileType? darwinFileType,
PMCancelToken? cancelToken,
}) async {
final params = <String, dynamic>{
'id': id,
Expand All @@ -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);
}

Expand Down Expand Up @@ -512,6 +541,7 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin, OhosPlugin {
Future<String?> getMediaUrl(
AssetEntity entity, {
PMProgressHandler? progressHandler,
PMCancelToken? cancelToken,
}) async {
if (PlatformUtils.isOhos) {
return entity.id;
Expand All @@ -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);
}

Expand Down
28 changes: 25 additions & 3 deletions lib/src/types/entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -583,13 +583,15 @@ class AssetEntity {
bool isOrigin = true,
bool withSubtype = false,
PMProgressHandler? progressHandler,
PMCancelToken? cancelToken,
PMDarwinAVFileType? darwinFileType,
}) {
return _getFile(
isOrigin: isOrigin,
subtype: withSubtype ? subtype : 0,
progressHandler: progressHandler,
darwinFileType: darwinFileType,
cancelToken: cancelToken,
);
}

Expand Down Expand Up @@ -625,6 +627,7 @@ class AssetEntity {
ThumbnailFormat format = ThumbnailFormat.jpeg,
int quality = 100,
PMProgressHandler? progressHandler,
PMCancelToken? cancelToken,
int frame = 0,
}) {
assert(() {
Expand Down Expand Up @@ -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].
Expand All @@ -666,6 +673,7 @@ class AssetEntity {
Future<typed_data.Uint8List?> thumbnailDataWithOption(
ThumbnailOption option, {
PMProgressHandler? progressHandler,
PMCancelToken? cancelToken,
}) {
assert(() {
_checkThumbnailAssertion();
Expand All @@ -683,6 +691,7 @@ class AssetEntity {
id: id,
option: option,
progressHandler: progressHandler,
cancelToken: cancelToken,
);
}

Expand Down Expand Up @@ -736,10 +745,12 @@ class AssetEntity {
/// * https://developer.apple.com/documentation/avfoundation/avurlasset
Future<String?> getMediaUrl({
PMProgressHandler? progressHandler,
PMCancelToken? cancelToken,
}) {
return plugin.getMediaUrl(
this,
progressHandler: progressHandler,
cancelToken: cancelToken,
);
}

Expand All @@ -754,6 +765,7 @@ class AssetEntity {
PMProgressHandler? progressHandler,
int subtype = 0,
PMDarwinAVFileType? darwinFileType,
PMCancelToken? cancelToken,
}) async {
assert(
_platformMatched,
Expand All @@ -768,6 +780,7 @@ class AssetEntity {
progressHandler: progressHandler,
subtype: subtype,
darwinFileType: darwinFileType,
cancelToken: cancelToken,
);
if (path == null) {
return null;
Expand All @@ -777,6 +790,7 @@ class AssetEntity {

Future<typed_data.Uint8List?> _getOriginBytes({
PMProgressHandler? progressHandler,
PMCancelToken? cancelToken,
}) async {
assert(
_platformMatched,
Expand All @@ -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();
Expand Down

0 comments on commit f237252

Please sign in to comment.