feat: Support filter media items between dates (#140)
* Update RNCCameraRollManager.m * Update CameraRollModule.java * Update README.md * Update RNCCameraRollManager.m * Update CameraRoll.d.ts * Update RNCCameraRollManager.h * Update CameraRollModule.java * Update RNCCameraRollManager.m * update code
This commit is contained in:
parent
24f141cada
commit
75b4208474
|
@ -147,6 +147,8 @@ Returns a Promise with photo identifier objects from the local camera roll of th
|
||||||
* `Videos`
|
* `Videos`
|
||||||
* `Photos` // default
|
* `Photos` // default
|
||||||
* `mimeTypes` : {Array} : Filter by mimetype (e.g. image/jpeg).
|
* `mimeTypes` : {Array} : Filter by mimetype (e.g. image/jpeg).
|
||||||
|
* `fromTime` : {timestamp} : Filter from date added.
|
||||||
|
* `toTime` : {timestamp} : Filter to date added.
|
||||||
|
|
||||||
Returns a Promise which when resolved will be of the following shape:
|
Returns a Promise which when resolved will be of the following shape:
|
||||||
|
|
||||||
|
|
|
@ -234,6 +234,8 @@ public class CameraRollModule extends ReactContextBaseJavaModule {
|
||||||
String after = params.hasKey("after") ? params.getString("after") : null;
|
String after = params.hasKey("after") ? params.getString("after") : null;
|
||||||
String groupName = params.hasKey("groupName") ? params.getString("groupName") : null;
|
String groupName = params.hasKey("groupName") ? params.getString("groupName") : null;
|
||||||
String assetType = params.hasKey("assetType") ? params.getString("assetType") : ASSET_TYPE_PHOTOS;
|
String assetType = params.hasKey("assetType") ? params.getString("assetType") : ASSET_TYPE_PHOTOS;
|
||||||
|
long fromTime = params.hasKey("fromTime") ? (long) params.getDouble("fromTime") : 0;
|
||||||
|
long toTime = params.hasKey("toTime") ? (long) params.getDouble("toTime") : 0;
|
||||||
ReadableArray mimeTypes = params.hasKey("mimeTypes")
|
ReadableArray mimeTypes = params.hasKey("mimeTypes")
|
||||||
? params.getArray("mimeTypes")
|
? params.getArray("mimeTypes")
|
||||||
: null;
|
: null;
|
||||||
|
@ -245,6 +247,8 @@ public class CameraRollModule extends ReactContextBaseJavaModule {
|
||||||
groupName,
|
groupName,
|
||||||
mimeTypes,
|
mimeTypes,
|
||||||
assetType,
|
assetType,
|
||||||
|
fromTime,
|
||||||
|
toTime,
|
||||||
promise)
|
promise)
|
||||||
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||||
}
|
}
|
||||||
|
@ -257,6 +261,8 @@ public class CameraRollModule extends ReactContextBaseJavaModule {
|
||||||
private final @Nullable ReadableArray mMimeTypes;
|
private final @Nullable ReadableArray mMimeTypes;
|
||||||
private final Promise mPromise;
|
private final Promise mPromise;
|
||||||
private final String mAssetType;
|
private final String mAssetType;
|
||||||
|
private final long mFromTime;
|
||||||
|
private final long mToTime;
|
||||||
|
|
||||||
private GetMediaTask(
|
private GetMediaTask(
|
||||||
ReactContext context,
|
ReactContext context,
|
||||||
|
@ -265,6 +271,8 @@ public class CameraRollModule extends ReactContextBaseJavaModule {
|
||||||
@Nullable String groupName,
|
@Nullable String groupName,
|
||||||
@Nullable ReadableArray mimeTypes,
|
@Nullable ReadableArray mimeTypes,
|
||||||
String assetType,
|
String assetType,
|
||||||
|
long fromTime,
|
||||||
|
long toTime,
|
||||||
Promise promise) {
|
Promise promise) {
|
||||||
super(context);
|
super(context);
|
||||||
mContext = context;
|
mContext = context;
|
||||||
|
@ -274,6 +282,8 @@ public class CameraRollModule extends ReactContextBaseJavaModule {
|
||||||
mMimeTypes = mimeTypes;
|
mMimeTypes = mimeTypes;
|
||||||
mPromise = promise;
|
mPromise = promise;
|
||||||
mAssetType = assetType;
|
mAssetType = assetType;
|
||||||
|
mFromTime = fromTime;
|
||||||
|
mToTime = toTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -314,6 +324,15 @@ public class CameraRollModule extends ReactContextBaseJavaModule {
|
||||||
selection.replace(selection.length() - 1, selection.length(), ")");
|
selection.replace(selection.length() - 1, selection.length(), ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mFromTime > 0) {
|
||||||
|
selection.append(" AND " + Images.Media.DATE_TAKEN + " > ?");
|
||||||
|
selectionArgs.add(mFromTime + "");
|
||||||
|
}
|
||||||
|
if (mToTime > 0) {
|
||||||
|
selection.append(" AND " + Images.Media.DATE_TAKEN + " <= ?");
|
||||||
|
selectionArgs.add(mToTime + "");
|
||||||
|
}
|
||||||
|
|
||||||
WritableMap response = new WritableNativeMap();
|
WritableMap response = new WritableNativeMap();
|
||||||
ContentResolver resolver = mContext.getContentResolver();
|
ContentResolver resolver = mContext.getContentResolver();
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,9 @@
|
||||||
|
|
||||||
@interface RCTConvert (PHFetchOptions)
|
@interface RCTConvert (PHFetchOptions)
|
||||||
|
|
||||||
+ (PHFetchOptions *)PHFetchOptionsFromMediaType:(NSString *)mediaType;
|
+ (PHFetchOptions *)PHFetchOptionsFromMediaType:(NSString *)mediaType
|
||||||
|
fromTime:(NSUInteger)fromTime
|
||||||
|
toTime:(NSUInteger)toTime;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|
|
@ -43,27 +43,44 @@ RCT_ENUM_CONVERTER(PHAssetCollectionSubtype, (@{
|
||||||
@implementation RCTConvert (PHFetchOptions)
|
@implementation RCTConvert (PHFetchOptions)
|
||||||
|
|
||||||
+ (PHFetchOptions *)PHFetchOptionsFromMediaType:(NSString *)mediaType
|
+ (PHFetchOptions *)PHFetchOptionsFromMediaType:(NSString *)mediaType
|
||||||
|
fromTime:(NSUInteger)fromTime
|
||||||
|
toTime:(NSUInteger)toTime
|
||||||
{
|
{
|
||||||
// This is not exhaustive in terms of supported media type predicates; more can be added in the future
|
// This is not exhaustive in terms of supported media type predicates; more can be added in the future
|
||||||
NSString *const lowercase = [mediaType lowercaseString];
|
NSString *const lowercase = [mediaType lowercaseString];
|
||||||
|
NSMutableArray *format = [NSMutableArray new];
|
||||||
|
NSMutableArray *arguments = [NSMutableArray new];
|
||||||
|
|
||||||
if ([lowercase isEqualToString:@"photos"]) {
|
if ([lowercase isEqualToString:@"photos"]) {
|
||||||
PHFetchOptions *const options = [PHFetchOptions new];
|
[format addObject:@"mediaType = %d"];
|
||||||
options.predicate = [NSPredicate predicateWithFormat:@"mediaType = %d", PHAssetMediaTypeImage];
|
[arguments addObject:@(PHAssetMediaTypeImage)];
|
||||||
return options;
|
|
||||||
} else if ([lowercase isEqualToString:@"videos"]) {
|
} else if ([lowercase isEqualToString:@"videos"]) {
|
||||||
PHFetchOptions *const options = [PHFetchOptions new];
|
[format addObject:@"mediaType = %d"];
|
||||||
options.predicate = [NSPredicate predicateWithFormat:@"mediaType = %d", PHAssetMediaTypeVideo];
|
[arguments addObject:@(PHAssetMediaTypeVideo)];
|
||||||
return options;
|
|
||||||
} else {
|
} else {
|
||||||
if (![lowercase isEqualToString:@"all"]) {
|
if (![lowercase isEqualToString:@"all"]) {
|
||||||
RCTLogError(@"Invalid filter option: '%@'. Expected one of 'photos',"
|
RCTLogError(@"Invalid filter option: '%@'. Expected one of 'photos',"
|
||||||
"'videos' or 'all'.", mediaType);
|
"'videos' or 'all'.", mediaType);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fromTime > 0) {
|
||||||
|
NSDate* fromDate = [NSDate dateWithTimeIntervalSince1970:fromTime/1000];
|
||||||
|
[format addObject:@"creationDate > %@"];
|
||||||
|
[arguments addObject:fromDate];
|
||||||
|
}
|
||||||
|
if (toTime > 0) {
|
||||||
|
NSDate* toDate = [NSDate dateWithTimeIntervalSince1970:toTime/1000];
|
||||||
|
[format addObject:@"creationDate < %@"];
|
||||||
|
[arguments addObject:toDate];
|
||||||
|
}
|
||||||
|
|
||||||
// This case includes the "all" mediatype
|
// This case includes the "all" mediatype
|
||||||
PHFetchOptions *const options = [PHFetchOptions new];
|
PHFetchOptions *const options = [PHFetchOptions new];
|
||||||
return options;
|
if ([format count] > 0) {
|
||||||
|
options.predicate = [NSPredicate predicateWithFormat:[format componentsJoinedByString:@" AND "] argumentArray:arguments];
|
||||||
}
|
}
|
||||||
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
@ -225,6 +242,8 @@ RCT_EXPORT_METHOD(getPhotos:(NSDictionary *)params
|
||||||
NSString *const groupName = [RCTConvert NSString:params[@"groupName"]];
|
NSString *const groupName = [RCTConvert NSString:params[@"groupName"]];
|
||||||
NSString *const groupTypes = [[RCTConvert NSString:params[@"groupTypes"]] lowercaseString];
|
NSString *const groupTypes = [[RCTConvert NSString:params[@"groupTypes"]] lowercaseString];
|
||||||
NSString *const mediaType = [RCTConvert NSString:params[@"assetType"]];
|
NSString *const mediaType = [RCTConvert NSString:params[@"assetType"]];
|
||||||
|
NSUInteger const fromTime = [RCTConvert NSInteger:params[@"fromTime"]];
|
||||||
|
NSUInteger const toTime = [RCTConvert NSInteger:params[@"toTime"]];
|
||||||
NSArray<NSString *> *const mimeTypes = [RCTConvert NSStringArray:params[@"mimeTypes"]];
|
NSArray<NSString *> *const mimeTypes = [RCTConvert NSStringArray:params[@"mimeTypes"]];
|
||||||
|
|
||||||
// If groupTypes is "all", we want to fetch the SmartAlbum "all photos". Otherwise, all
|
// If groupTypes is "all", we want to fetch the SmartAlbum "all photos". Otherwise, all
|
||||||
|
@ -235,7 +254,7 @@ RCT_EXPORT_METHOD(getPhotos:(NSDictionary *)params
|
||||||
PHAssetCollectionSubtype const collectionSubtype = [RCTConvert PHAssetCollectionSubtype:groupTypes];
|
PHAssetCollectionSubtype const collectionSubtype = [RCTConvert PHAssetCollectionSubtype:groupTypes];
|
||||||
|
|
||||||
// Predicate for fetching assets within a collection
|
// Predicate for fetching assets within a collection
|
||||||
PHFetchOptions *const assetFetchOptions = [RCTConvert PHFetchOptionsFromMediaType:mediaType];
|
PHFetchOptions *const assetFetchOptions = [RCTConvert PHFetchOptionsFromMediaType:mediaType fromTime:fromTime toTime:toTime];
|
||||||
assetFetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
|
assetFetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
|
||||||
|
|
||||||
BOOL __block foundAfter = NO;
|
BOOL __block foundAfter = NO;
|
||||||
|
|
|
@ -26,6 +26,8 @@ declare namespace CameraRoll {
|
||||||
groupName?: string;
|
groupName?: string;
|
||||||
assetType?: AssetType;
|
assetType?: AssetType;
|
||||||
mimeTypes?: Array<string>;
|
mimeTypes?: Array<string>;
|
||||||
|
fromTime?: number;
|
||||||
|
toTime?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface PhotoIdentifier {
|
interface PhotoIdentifier {
|
||||||
|
|
Loading…
Reference in New Issue