Skip to content

Commit f43fed1

Browse files
CopilotAlexV525
andauthored
Fix iOS/macOS ignoring RequestType when filterOption is nil (#1354)
On iOS/macOS, using `RequestType.video` without an explicit `filterOption` returns all media types instead of just videos. Regression from #1313 which made `filterOptionGroup` nullable. When `filterOption` is `nil`, `[nil getFetchOptions:type]` returns `nil` in Objective-C, resulting in no predicate on `PHFetchOptions`. ### Changes - **`PMManager.m: getAssetOptions:filterOption:`** — Create basic `PHFetchOptions` with media type predicate when `filterOption` is nil - **`PMManager.m: getAssetCountWithType:option:`** — Route through `getAssetOptions:filterOption:` instead of calling `getFetchOptions:` directly - **`PMManager.m: getAssetsWithType:option:start:end:`** — Same fix, plus nil-safe `needTitle` access --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: AlexV525 <[email protected]> Co-authored-by: Alex Li <[email protected]>
1 parent 02dbed4 commit f43fed1

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ To know more about breaking changes, see the [Migration Guide][].
88

99
## Unreleased
1010

11-
*None.*
11+
**Fixes**
12+
13+
- Fix iOS/macOS returning all media types when `filterOption` is not specified instead of respecting the requested type.
1214

1315
## 3.8.2
1416

darwin/photo_manager/Sources/photo_manager/core/PMManager.m

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#import "PMManager.h"
1212
#import "PMMD5Utils.h"
1313
#import "PMPathFilterOption.h"
14+
#import "PMRequestTypeUtils.h"
1415
#import "PMResultHandler.h"
1516

1617
@implementation PMManager {
@@ -130,13 +131,13 @@ - (void)logCollections:(PHFetchResult *)collections option:(PHFetchOptions *)opt
130131
}
131132

132133
- (NSUInteger)getAssetCountWithType:(int)type option:(NSObject<PMBaseFilter> *)filter {
133-
PHFetchOptions *options = [filter getFetchOptions:type];
134+
PHFetchOptions *options = [self getAssetOptions:type filterOption:filter];
134135
PHFetchResult<PHAsset *> *result = [PHAsset fetchAssetsWithOptions:options];
135136
return result.count;
136137
}
137138

138139
- (NSArray<PMAssetEntity *> *)getAssetsWithType:(int)type option:(NSObject<PMBaseFilter> *)option start:(int)start end:(int)end {
139-
PHFetchOptions *options = [option getFetchOptions:type];
140+
PHFetchOptions *options = [self getAssetOptions:type filterOption:option];
140141
PHFetchResult<PHAsset *> *result = [PHAsset fetchAssetsWithOptions:options];
141142

142143
NSUInteger endOffset = end;
@@ -151,7 +152,8 @@ - (NSUInteger)getAssetCountWithType:(int)type option:(NSObject<PMBaseFilter> *)f
151152
break;
152153
}
153154
PHAsset *asset = result[i];
154-
PMAssetEntity *pmAsset = [self convertPHAssetToAssetEntity:asset needTitle:[option needTitle]];
155+
BOOL needTitle = option ? [option needTitle] : NO;
156+
PMAssetEntity *pmAsset = [self convertPHAssetToAssetEntity:asset needTitle:needTitle];
155157
[array addObject: pmAsset];
156158
}
157159

@@ -1212,7 +1214,11 @@ - (PMAssetPathEntity *)fetchPathProperties:(NSString *)id type:(int)type filterO
12121214
}
12131215

12141216
- (PHFetchOptions *)getAssetOptions:(int)type filterOption:(NSObject<PMBaseFilter> *)optionGroup {
1215-
return [optionGroup getFetchOptions:type];
1217+
if (optionGroup) {
1218+
return [optionGroup getFetchOptions:type];
1219+
}
1220+
1221+
return [PMRequestTypeUtils getFetchOptionsByType:type];
12161222
}
12171223

12181224
#pragma clang diagnostic push

darwin/photo_manager/Sources/photo_manager/core/PMRequestTypeUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#import <Foundation/Foundation.h>
2+
#import <Photos/Photos.h>
23

34
@interface PMRequestTypeUtils : NSObject
45

@@ -8,4 +9,6 @@
89

910
+ (BOOL)containsAudio:(int)type;
1011

12+
+ (PHFetchOptions *)getFetchOptionsByType:(int)type;
13+
1114
@end

darwin/photo_manager/Sources/photo_manager/core/PMRequestTypeUtils.m

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
#import "NSString+PM_COMMON.h"
12
#import "PMRequestTypeUtils.h"
23

4+
#import <Photos/Photos.h>
5+
36
#define PM_TYPE_IMAGE 1
47
#define PM_TYPE_VIDEO 1<<1
58
#define PM_TYPE_AUDIO 1<<2
@@ -24,4 +27,41 @@ + (BOOL)containsAudio:(int)type {
2427
return [self checkContainsType:type targetType:PM_TYPE_AUDIO];
2528
}
2629

30+
+ (PHFetchOptions *)getFetchOptionsByType:(int)type {
31+
// When filterOption is nil, we still need to filter by media type
32+
PHFetchOptions *options = [PHFetchOptions new];
33+
34+
BOOL containsImage = [PMRequestTypeUtils containsImage:type];
35+
BOOL containsVideo = [PMRequestTypeUtils containsVideo:type];
36+
BOOL containsAudio = [PMRequestTypeUtils containsAudio:type];
37+
38+
NSMutableString *typeWhere = [NSMutableString new];
39+
NSMutableArray *args = [NSMutableArray new];
40+
41+
if (containsImage) {
42+
[typeWhere appendString:@"mediaType == %d"];
43+
[args addObject:@(PHAssetMediaTypeImage)];
44+
}
45+
if (containsVideo) {
46+
if (![typeWhere isEmpty]) {
47+
[typeWhere appendString:@" OR "];
48+
}
49+
[typeWhere appendString:@"mediaType == %d"];
50+
[args addObject:@(PHAssetMediaTypeVideo)];
51+
}
52+
if (containsAudio) {
53+
if (![typeWhere isEmpty]) {
54+
[typeWhere appendString:@" OR "];
55+
}
56+
[typeWhere appendString:@"mediaType == %d"];
57+
[args addObject:@(PHAssetMediaTypeAudio)];
58+
}
59+
60+
if (![typeWhere isEmpty]) {
61+
options.predicate = [NSPredicate predicateWithFormat:typeWhere argumentArray:args];
62+
}
63+
64+
return options;
65+
}
66+
2767
@end

0 commit comments

Comments
 (0)