Skip to content

Commit

Permalink
🥅 Expose more errors from each calls
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Sep 18, 2024
1 parent 1e9964d commit fcd0a05
Show file tree
Hide file tree
Showing 3 changed files with 293 additions and 291 deletions.
108 changes: 53 additions & 55 deletions ios/Classes/PMPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ - (void)initNotificationManager:(NSObject <FlutterPluginRegistrar> *)registrar {
self.notificationManager = [PMNotificationManager managerWithRegistrar:registrar];
}

- (void) requestOnlyAddPermission:(void(^)(PHAuthorizationStatus status))handler {
- (void)requestOnlyAddPermission:(void(^)(PHAuthorizationStatus status))handler {
#if TARGET_OS_OSX
if (@available(macOS 11.0, *)) {
[PHPhotoLibrary requestAuthorizationForAccessLevel:PHAccessLevelAddOnly handler:handler];
Expand Down Expand Up @@ -428,41 +428,41 @@ - (void)handleMethodResultHandler:(ResultHandler *)handler manager:(PMManager *)
[manager saveImage:data
filename:filename
desc:desc
block:^(PMAssetEntity *asset) {
if (!asset) {
[handler reply:nil];
return;
}
[handler reply:[PMConvertUtils convertPMAssetToMap:asset needTitle:NO]];
}];
block:^(PMAssetEntity *asset, NSObject *error) {
if (asset) {
[handler reply:[PMConvertUtils convertPMAssetToMap:asset needTitle:NO]];
return;
}
[handler replyError:error];
}];
} else if ([call.method isEqualToString:@"saveImageWithPath"]) {
NSString *path = call.arguments[@"path"];
NSString *filename = call.arguments[@"title"];
NSString *desc = call.arguments[@"desc"];
[manager saveImageWithPath:path
filename:filename
desc:desc
block:^(PMAssetEntity *asset) {
if (!asset) {
[handler reply:nil];
return;
}
[handler reply:[PMConvertUtils convertPMAssetToMap:asset needTitle:NO]];
}];
block:^(PMAssetEntity *asset, NSObject *error) {
if (asset) {
[handler reply:[PMConvertUtils convertPMAssetToMap:asset needTitle:NO]];
return;
}
[handler replyError:error];
}];
} else if ([call.method isEqualToString:@"saveVideo"]) {
NSString *videoPath = call.arguments[@"path"];
NSString *filename = call.arguments[@"title"];
NSString *desc = call.arguments[@"desc"];
[manager saveVideo:videoPath
filename:filename
desc:desc
block:^(PMAssetEntity *asset) {
if (!asset) {
[handler reply:nil];
return;
}
[handler reply:[PMConvertUtils convertPMAssetToMap:asset needTitle:NO]];
}];
block:^(PMAssetEntity *asset, NSObject *error) {
if (asset) {
[handler reply:[PMConvertUtils convertPMAssetToMap:asset needTitle:NO]];
return;
}
[handler replyError:error];
}];
} else if ([call.method isEqualToString:@"saveLivePhoto"]) {
NSString *videoPath = call.arguments[@"videoPath"];
NSString *imagePath = call.arguments[@"imagePath"];
Expand All @@ -472,13 +472,13 @@ - (void)handleMethodResultHandler:(ResultHandler *)handler manager:(PMManager *)
videoPath:videoPath
filename:filename
desc:desc
block:^(PMAssetEntity *asset) {
if (!asset) {
[handler reply:nil];
return;
}
[handler reply:[PMConvertUtils convertPMAssetToMap:asset needTitle:NO]];
}];
block:^(PMAssetEntity *asset, NSObject *error) {
if (asset) {
[handler reply:[PMConvertUtils convertPMAssetToMap:asset needTitle:NO]];
return;
}
[handler replyError:error];
}];
} else if ([call.method isEqualToString:@"assetExists"]) {
NSString *assetId = call.arguments[@"id"];
BOOL exists = [manager existsWithId:assetId];
Expand Down Expand Up @@ -522,13 +522,12 @@ - (void)handleMethodResultHandler:(ResultHandler *)handler manager:(PMManager *)
} else if ([@"copyAsset" isEqualToString:call.method]) {
NSString *assetId = call.arguments[@"assetId"];
NSString *galleryId = call.arguments[@"galleryId"];
[manager copyAssetWithId:assetId toGallery:galleryId block:^(PMAssetEntity *entity, NSString *msg) {
if (msg) {
NSLog(@"copy asset error, cause by : %@", msg);
[handler reply:nil];
} else {
[handler reply:[PMConvertUtils convertPMAssetToMap:entity needTitle:NO]];
}
[manager copyAssetWithId:assetId toGallery:galleryId block:^(PMAssetEntity *entity, NSObject *error) {
if (error) {
[handler replyError:error];
} else {
[handler reply:[PMConvertUtils convertPMAssetToMap:entity needTitle:NO]];
}
}];
} else if ([@"createFolder" isEqualToString:call.method]) {
[self createFolder:call manager:manager handler:handler];
Expand All @@ -538,9 +537,9 @@ - (void)handleMethodResultHandler:(ResultHandler *)handler manager:(PMManager *)
NSArray *assetId = call.arguments[@"assetId"];
NSString *pathId = call.arguments[@"pathId"];

[manager removeInAlbumWithAssetId:assetId albumId:pathId block:^(NSString *msg) {
if (msg) {
[handler reply:@{@"msg": msg}];
[manager removeInAlbumWithAssetId:assetId albumId:pathId block:^(NSObject *error) {
if (error) {
[handler replyError:error];
} else {
[handler reply:@{@"success": @YES}];
}
Expand All @@ -563,9 +562,9 @@ - (void)handleMethodResultHandler:(ResultHandler *)handler manager:(PMManager *)
} else if ([@"deleteAlbum" isEqualToString:call.method]) {
NSString *id = call.arguments[@"id"];
int type = [call.arguments[@"type"] intValue];
[manager removeCollectionWithId:id type:type block:^(NSString *msg) {
if (msg) {
[handler reply:@{@"errorMsg": msg}];
[manager removeCollectionWithId:id type:type block:^(NSObject *error) {
if (error) {
[handler replyError:error];
} else {
[handler reply:@{@"result": @YES}];
}
Expand All @@ -588,16 +587,9 @@ - (void)handleMethodResultHandler:(ResultHandler *)handler manager:(PMManager *)
}
}

- (NSDictionary *)convertToResult:(NSString *)id errorMsg:(NSString *)errorMsg {
- (NSDictionary *)convertToResult:(NSString *)id {
NSMutableDictionary *mutableDictionary = [NSMutableDictionary new];
if (errorMsg) {
mutableDictionary[@"errorMsg"] = errorMsg;
}

if (id) {
mutableDictionary[@"id"] = id;
}

mutableDictionary[@"id"] = id;
return mutableDictionary;
}

Expand All @@ -622,8 +614,11 @@ - (void)createFolder:(FlutterMethodCall *)call manager:(PMManager *)manager hand
parentId = nil;
}

[manager createFolderWithName:name parentId:parentId block:^(NSString *id, NSString *errorMsg) {
[handler reply:[self convertToResult:id errorMsg:errorMsg]];
[manager createFolderWithName:name parentId:parentId block:^(NSString *newId, NSObject *error) {
if (error) {
[handler replyError:error];
}
[handler reply:[self convertToResult:newId]];
}];
}

Expand All @@ -636,8 +631,11 @@ - (void)createAlbum:(FlutterMethodCall *)call manager:(PMManager *)manager handl
parentId = nil;
}

[manager createAlbumWithName:name parentId:parentId block:^(NSString *id, NSString *errorMsg) {
[handler reply:[self convertToResult:id errorMsg:errorMsg]];
[manager createAlbumWithName:name parentId:parentId block:^(NSString *newId, NSObject *error) {
if (error) {
[handler replyError:error];
}
[handler reply:[self convertToResult:newId]];
}];
}

Expand Down
20 changes: 10 additions & 10 deletions ios/Classes/core/PMManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ typedef void (^ChangeIds)(NSArray<NSString *> *);
#define PM_IMAGE_CACHE_PATH @".image"
#define PM_FULL_IMAGE_CACHE_PATH @"flutter-images"

typedef void (^AssetResult)(PMAssetEntity *);
typedef void (^AssetBlockResult)(PMAssetEntity *, NSObject *);


@interface PMManager : NSObject
Expand Down Expand Up @@ -55,23 +55,23 @@ typedef void (^AssetResult)(PMAssetEntity *);
- (void)saveImage:(NSData *)data
filename:(NSString *)filename
desc:(NSString *)desc
block:(AssetResult)block;
block:(AssetBlockResult)block;

- (void)saveImageWithPath:(NSString *)path
filename:(NSString *)filename
desc:(NSString *)desc
block:(void (^)(PMAssetEntity *))block;
block:(AssetBlockResult)block;

- (void)saveVideo:(NSString *)path
filename:(NSString *)filename
desc:(NSString *)desc
block:(AssetResult)block;
block:(AssetBlockResult)block;

- (void)saveLivePhoto:(NSString *)imagePath
videoPath:(NSString *)videoPath
filename:(NSString *)filename
desc:(NSString *)desc
block:(AssetResult)block;
block:(AssetBlockResult)block;

- (BOOL)existsWithId:(NSString *)assetId;

Expand All @@ -85,15 +85,15 @@ typedef void (^AssetResult)(PMAssetEntity *);

- (NSArray<PMAssetPathEntity *> *)getSubPathWithId:(NSString *)id type:(int)type albumType:(int)albumType option:(NSObject<PMBaseFilter> *)option;

- (void)copyAssetWithId:(NSString *)id toGallery:(NSString *)gallery block:(void (^)(PMAssetEntity *entity, NSString *msg))block;
- (void)copyAssetWithId:(NSString *)id toGallery:(NSString *)gallery block:(void (^)(PMAssetEntity *entity, NSObject *msg))block;

- (void)createFolderWithName:(NSString *)name parentId:(NSString *)id block:(void (^)(NSString *, NSString *))block;
- (void)createFolderWithName:(NSString *)name parentId:(NSString *)id block:(void (^)(NSString *newId, NSObject *error))block;

- (void)createAlbumWithName:(NSString *)name parentId:(NSString *)id block:(void (^)(NSString *, NSString *))block;
- (void)createAlbumWithName:(NSString *)name parentId:(NSString *)id block:(void (^)(NSString *newId, NSObject *error))block;

- (void)removeInAlbumWithAssetId:(NSArray *)id albumId:(NSString *)albumId block:(void (^)(NSString *))block;
- (void)removeInAlbumWithAssetId:(NSArray *)id albumId:(NSString *)albumId block:(void (^)(NSObject *error))block;

- (void)removeCollectionWithId:(NSString *)id type:(int)type block:(void (^)(NSString *))block;
- (void)removeCollectionWithId:(NSString *)id type:(int)type block:(void (^)(NSObject *))block;

- (BOOL)favoriteWithId:(NSString *)id favorite:(BOOL)favorite;

Expand Down
Loading

0 comments on commit fcd0a05

Please sign in to comment.