From a8d92ad5c00389a85e02d41aa1b5889a22bb3973 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Tue, 22 Oct 2024 22:04:08 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20Get=20rid=20of=20try=20catch=20w?= =?UTF-8?q?hen=20toggling=20favorite=20on=20Darwin=20(#1209)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To avoid crashes on specific Xcode versions, typically Xcode 15.0.x. Fixes https://github.com/fluttercandies/flutter_photo_manager/issues/1208 --- CHANGELOG.md | 6 ++++++ example/pubspec.yaml | 2 +- example_ohos/pubspec.yaml | 2 +- ios/Classes/PMPlugin.m | 13 +++++++------ ios/Classes/core/PMManager.h | 2 +- ios/Classes/core/PMManager.m | 19 +++++++++++-------- pubspec.yaml | 2 +- 7 files changed, 28 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bd67d71..339c57cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,12 @@ To know more about breaking changes, see the [Migration Guide][]. *None.* +## 3.5.2 + +### Improvements + +- Get rid of `@try` `@catch` when toggling favorite on Darwin. + ## 3.5.1 ### Improvements diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 4951b7a4..b261e003 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -1,6 +1,6 @@ name: photo_manager_example description: Demonstrates how to use the photo_manager plugin. -version: 3.5.1+34 +version: 3.5.2+35 publish_to: none environment: diff --git a/example_ohos/pubspec.yaml b/example_ohos/pubspec.yaml index d0d5416e..15a573c2 100644 --- a/example_ohos/pubspec.yaml +++ b/example_ohos/pubspec.yaml @@ -1,6 +1,6 @@ name: photo_manager_example_ohos description: Demonstrates how to use the photo_manager plugin. -version: 3.5.1+34 +version: 3.5.2+35 publish_to: none environment: diff --git a/ios/Classes/PMPlugin.m b/ios/Classes/PMPlugin.m index 13568aba..67f1f9b3 100644 --- a/ios/Classes/PMPlugin.m +++ b/ios/Classes/PMPlugin.m @@ -613,12 +613,13 @@ - (void)handleMethodResultHandler:(ResultHandler *)handler manager:(PMManager *) } else if ([@"favoriteAsset" isEqualToString:call.method]) { NSString *id = call.arguments[@"id"]; BOOL favorite = [call.arguments[@"favorite"] boolValue]; - @try { - BOOL favoriteResult = [manager favoriteWithId:id favorite:favorite]; - [handler reply:@(favoriteResult)]; - } @catch (NSObject *error) { - [handler replyError:error]; - } + [manager favoriteWithId:id favorite:favorite block:^(BOOL result, NSObject *error) { + if (error) { + [handler replyError:error]; + } else { + [handler reply:@(result)]; + } + }]; } else if ([@"requestCacheAssetsThumb" isEqualToString:call.method]) { NSArray *ids = call.arguments[@"ids"]; PMThumbLoadOption *option = [PMThumbLoadOption optionDict:call.arguments[@"option"]]; diff --git a/ios/Classes/core/PMManager.h b/ios/Classes/core/PMManager.h index b31a2173..d5f20ceb 100644 --- a/ios/Classes/core/PMManager.h +++ b/ios/Classes/core/PMManager.h @@ -111,7 +111,7 @@ typedef void (^AssetBlockResult)(PMAssetEntity *, NSObject *); - (void)removeCollectionWithId:(NSString *)id type:(int)type block:(void (^)(NSObject *))block; -- (BOOL)favoriteWithId:(NSString *)id favorite:(BOOL)favorite; +- (void)favoriteWithId:(NSString *)id favorite:(BOOL)favorite block:(void (^)(BOOL result, NSObject *))block; - (void)clearFileCache; diff --git a/ios/Classes/core/PMManager.m b/ios/Classes/core/PMManager.m index 2a45e86a..fe392cc4 100644 --- a/ios/Classes/core/PMManager.m +++ b/ios/Classes/core/PMManager.m @@ -1625,30 +1625,33 @@ - (void)removeCollectionWithId:(NSString *)id type:(int)type block:(void (^)(NSO } } -- (BOOL)favoriteWithId:(NSString *)id favorite:(BOOL)favorite { +- (void)favoriteWithId:(NSString *)id favorite:(BOOL)favorite block:(void (^)(BOOL result, NSObject *error))block { PHFetchResult *fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[id] options:nil]; PHAsset *asset = [self getFirstObjFromFetchResult:fetchResult]; if (!asset) { - @throw [NSString stringWithFormat:@"Asset %@ not found.", id]; + block(NO, [NSString stringWithFormat:@"Asset %@ not found.", id]); + return; } if (![asset canPerformEditOperation:PHAssetEditOperationProperties]) { - @throw [NSString stringWithFormat:@"The asset %@ cannot perform edit operation.", id]; + block(NO, [NSString stringWithFormat:@"The asset %@ cannot perform edit operation.", id]); + return; } NSError *error; - BOOL succeed = [PHPhotoLibrary.sharedPhotoLibrary - performChangesAndWait:^{ + BOOL succeed = [PHPhotoLibrary.sharedPhotoLibrary performChangesAndWait:^{ PHAssetChangeRequest *request = [PHAssetChangeRequest changeRequestForAsset:asset]; request.favorite = favorite; } error:&error]; if (!succeed) { - @throw [NSString stringWithFormat:@"Favouring asset %@ failed: Request not succeed.", id]; + block(NO, [NSString stringWithFormat:@"Favouring asset %@ failed: Request not succeed.", id]); + return; } if (error) { - @throw error; + block(NO, error); + return; } - return YES; + block(YES, nil); } - (NSString *)getCachePath:(NSString *)type { diff --git a/pubspec.yaml b/pubspec.yaml index 7c4c0001..b7f6c002 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: photo_manager description: A Flutter plugin that provides album assets abstraction management APIs on Android, iOS, macOS, and OpenHarmony. repository: https://github.com/fluttercandies/flutter_photo_manager -version: 3.5.1 +version: 3.5.2 environment: sdk: ">=2.13.0 <4.0.0"