Skip to content

Commit

Permalink
🔥 Get rid of try catch when toggling favorite on Darwin (#1209)
Browse files Browse the repository at this point in the history
To avoid crashes on specific Xcode versions, typically Xcode 15.0.x.

Fixes
#1208
  • Loading branch information
AlexV525 authored Oct 22, 2024
1 parent bd48e50 commit a8d92ad
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 18 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion example_ohos/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
13 changes: 7 additions & 6 deletions ios/Classes/PMPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"]];
Expand Down
2 changes: 1 addition & 1 deletion ios/Classes/core/PMManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
19 changes: 11 additions & 8 deletions ios/Classes/core/PMManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit a8d92ad

Please sign in to comment.