44#import " PMLogUtils.h"
55#import " PMManager.h"
66#import " PMNotificationManager.h"
7- #import " ResultHandler .h"
7+ #import " PMResultHandler .h"
88#import " PMThumbLoadOption.h"
99#import " PMProgressHandler.h"
1010#import " PMConverter.h"
@@ -19,6 +19,12 @@ @implementation PMPlugin {
1919 BOOL isDetach;
2020}
2121
22+
23+ + (void )registerWithRegistrar : (nonnull NSObject <FlutterPluginRegistrar> *)registrar {
24+ PMPlugin *plugin = [PMPlugin new ];
25+ [plugin registerPlugin: registrar];
26+ }
27+
2228- (void )registerPlugin : (NSObject <FlutterPluginRegistrar> *)registrar {
2329 privateRegistrar = registrar;
2430 [self initNotificationManager: registrar];
@@ -29,8 +35,9 @@ - (void)registerPlugin:(NSObject <FlutterPluginRegistrar> *)registrar {
2935 manager.converter = [PMConverter new ];
3036 [self setManager: manager];
3137
38+ __block PMPlugin *weakSelf = self; // avoid retain cycle
3239 [channel setMethodCallHandler: ^(FlutterMethodCall *call, FlutterResult result) {
33- [self onMethodCall: call result: result];
40+ [weakSelf onMethodCall: call result: result];
3441 }];
3542}
3643
@@ -119,7 +126,7 @@ - (void)onMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
119126 return ;
120127 }
121128
122- ResultHandler *handler = [ResultHandler handlerWithCall: call result: result];
129+ PMResultHandler *handler = [PMResultHandler handlerWithCall: call result: result];
123130
124131 if ([self isNotNeedPermissionMethod: call.method]) {
125132 [self handleNotNeedPermissionMethod: handler];
@@ -130,7 +137,7 @@ - (void)onMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
130137 }
131138}
132139
133- - (void )handleNotNeedPermissionMethod : (ResultHandler *)handler {
140+ - (void )handleNotNeedPermissionMethod : (PMResultHandler *)handler {
134141 FlutterMethodCall *call = handler.call ;
135142 NSString *method = call.method ;
136143 PMManager *manager = self.manager ;
@@ -154,9 +161,9 @@ - (void)handleNotNeedPermissionMethod:(ResultHandler *)handler {
154161 }
155162}
156163
157- - (void )getPermissionState : (ResultHandler *)handler {
164+ - (void )getPermissionState : (PMResultHandler *)handler {
158165 int requestAccessLevel = [handler.call.arguments[@" iosAccessLevel" ] intValue ];
159- #if __IPHONE_14_0
166+ #if TARGET_OS_IOS
160167 if (@available (iOS 14 , *)) {
161168 PHAuthorizationStatus result = [PHPhotoLibrary authorizationStatusForAccessLevel: requestAccessLevel];
162169 [handler reply: @(result)];
@@ -165,12 +172,17 @@ - (void)getPermissionState:(ResultHandler *)handler {
165172 [handler reply: @(status)];
166173 }
167174#else
168- PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus ];
169- [handler reply: @(status)];
175+ if (@available (macOS 11.0 , *)) {
176+ PHAuthorizationStatus result = [PHPhotoLibrary authorizationStatusForAccessLevel: requestAccessLevel];
177+ [handler reply: @(result)];
178+ } else {
179+ PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus ];
180+ [handler reply: @(status)];
181+ }
170182#endif
171183}
172184
173- - (void )handleAboutPermissionMethod : (ResultHandler *)handler {
185+ - (void )handleAboutPermissionMethod : (PMResultHandler *)handler {
174186 FlutterMethodCall *call = handler.call ;
175187 PMManager *manager = self.manager ;
176188
@@ -182,7 +194,7 @@ - (void)handleAboutPermissionMethod:(ResultHandler *)handler {
182194 }
183195}
184196
185- - (void )replyPermssionResult : (ResultHandler *)handler status : (PHAuthorizationStatus)status isOnlyAdd : (BOOL )isOnlyAdd {
197+ - (void )replyPermssionResult : (PMResultHandler *)handler status : (PHAuthorizationStatus)status isOnlyAdd : (BOOL )isOnlyAdd {
186198 [handler reply: @(status)];
187199}
188200
@@ -207,7 +219,7 @@ - (UIViewController *)getCurrentViewController {
207219#endif
208220
209221- (void )handlePermission : (PMManager *)manager
210- handler : (ResultHandler *)handler
222+ handler : (PMResultHandler *)handler
211223 requestAccessLevel : (int )requestAccessLevel {
212224#if __IPHONE_14_0
213225 if (@available (iOS 14 , *)) {
@@ -245,7 +257,7 @@ - (void)requestPermissionStatus:(int)requestAccessLevel
245257#endif
246258}
247259
248- - (void )presentLimited : (ResultHandler *)handler {
260+ - (void )presentLimited : (PMResultHandler *)handler {
249261#if __IPHONE_14_0
250262 if (@available (iOS 14 , *)) {
251263 UIViewController *controller = [self getCurrentViewController ];
@@ -282,7 +294,7 @@ - (void)presentLimited:(ResultHandler *)handler {
282294
283295#if TARGET_OS_OSX
284296- (void )handlePermission : (PMManager *)manager
285- handler : (ResultHandler *)handler
297+ handler : (PMResultHandler *)handler
286298 requestAccessLevel : (int )requestAccessLevel {
287299#if __MAC_11_0
288300 if (@available (macOS 11.0 , *)) {
@@ -320,21 +332,23 @@ - (void)requestPermissionStatus:(int)requestAccessLevel
320332#endif
321333}
322334
323- - (void )presentLimited : (ResultHandler *)handler {
335+ - (void )presentLimited : (PMResultHandler *)handler {
324336 [handler replyError: @" Not supported on macOS." ];
325337}
326338
327339#endif
328340
329- - (void )runInBackground : (dispatch_block_t )block withHandler : (ResultHandler *)handler {
341+ - (void )runInBackground : (dispatch_block_t )block withHandler : (PMResultHandler *)handler {
330342 dispatch_qos_class_t priority = [self getQosPriorityForMethod: handler.call.method];
331343 dispatch_async (dispatch_get_global_queue (priority, 0 ), block);
332344}
333345
334346- (dispatch_qos_class_t )getQosPriorityForMethod : (NSString *)method {
335347 if ([method isEqualToString: @" getThumb" ] ||
336348 [method isEqualToString: @" assetExists" ] ||
337- [method isEqualToString: @" isLocallyAvailable" ]) {
349+ [method isEqualToString: @" isLocallyAvailable" ] ||
350+ [method isEqualToString: @" cancelRequestWithCancelToken" ] ||
351+ [method isEqualToString: @" cancelAllRequest" ]) {
338352 return QOS_CLASS_USER_INTERACTIVE;
339353 }
340354
@@ -367,7 +381,7 @@ - (dispatch_qos_class_t)getQosPriorityForMethod:(NSString *)method {
367381 return QOS_CLASS_DEFAULT;
368382}
369383
370- - (void )onAuth : (ResultHandler *)handler {
384+ - (void )onAuth : (PMResultHandler *)handler {
371385 PMManager *manager = self.manager ;
372386 __block PMNotificationManager *notificationManager = self.notificationManager ;
373387
@@ -381,7 +395,7 @@ - (void)onAuth:(ResultHandler *)handler {
381395 } withHandler: handler];
382396}
383397
384- - (void )handleMethodResultHandler : (ResultHandler *)handler manager : (PMManager *)manager notificationManager : (PMNotificationManager *)notificationManager {
398+ - (void )handleMethodResultHandler : (PMResultHandler *)handler manager : (PMManager *)manager notificationManager : (PMNotificationManager *)notificationManager {
385399 FlutterMethodCall *call = handler.call ;
386400
387401 if ([call.method isEqualToString: @" getAssetPathList" ]) {
@@ -669,6 +683,13 @@ - (void)handleMethodResultHandler:(ResultHandler *)handler manager:(PMManager *)
669683 } else if ([@" cancelCacheRequests" isEqualToString: call.method]) {
670684 [manager cancelCacheRequests ];
671685 [handler reply: @YES ];
686+ } else if ([@" cancelRequestWithCancelToken" isEqualToString: call.method]) {
687+ NSString *cancelToken = call.arguments [@" cancelToken" ];
688+ [manager cancelRequestWithCancelToken: cancelToken];
689+ [handler reply: @YES ];
690+ } else if ([@" cancelAllRequest" isEqualToString: call.method]) {
691+ [manager cancelAllRequest ];
692+ [handler reply: @YES ];
672693 } else {
673694 [handler notImplemented ];
674695 }
@@ -692,7 +713,7 @@ - (PMProgressHandler *)getProgressHandlerFromDict:(NSDictionary *)dict {
692713 return handler;
693714}
694715
695- - (void )createFolder : (FlutterMethodCall *)call manager : (PMManager *)manager handler : (ResultHandler *)handler {
716+ - (void )createFolder : (FlutterMethodCall *)call manager : (PMManager *)manager handler : (PMResultHandler *)handler {
696717 NSString *name = call.arguments [@" name" ];
697718 BOOL isRoot = [call.arguments[@" isRoot" ] boolValue ];
698719 NSString *parentId = call.arguments [@" folderId" ];
@@ -709,7 +730,7 @@ - (void)createFolder:(FlutterMethodCall *)call manager:(PMManager *)manager hand
709730 }];
710731}
711732
712- - (void )createAlbum : (FlutterMethodCall *)call manager : (PMManager *)manager handler : (ResultHandler *)handler {
733+ - (void )createAlbum : (FlutterMethodCall *)call manager : (PMManager *)manager handler : (PMResultHandler *)handler {
713734 NSString *name = call.arguments [@" name" ];
714735 BOOL isRoot = [call.arguments[@" isRoot" ] boolValue ];
715736 NSString *parentId = call.arguments [@" folderId" ];
0 commit comments