Export an object instead of array for asset type

Summary: Exporting an object instead of an array so it can be used as an enum by other files.

Reviewed By: sahrens

Differential Revision: D5007488

fbshipit-source-id: 8253a424f0dbb51511b889b1da637f366c0f290a
This commit is contained in:
Fred Liu 2017-05-04 21:38:12 -07:00 committed by Facebook Github Bot
parent e6adb78de6
commit 331d248f81

View File

@ -8,41 +8,36 @@
* *
* @providesModule CameraRoll * @providesModule CameraRoll
* @flow * @flow
* @format
*/ */
'use strict'; 'use strict';
var {PropTypes, checkPropTypes} = require('React'); const {PropTypes, checkPropTypes} = require('React');
var RCTCameraRollManager = require('NativeModules').CameraRollManager; const RCTCameraRollManager = require('NativeModules').CameraRollManager;
var createStrictShapeTypeChecker = require('createStrictShapeTypeChecker'); const createStrictShapeTypeChecker = require('createStrictShapeTypeChecker');
var deepFreezeAndThrowOnMutationInDev = const invariant = require('fbjs/lib/invariant');
require('deepFreezeAndThrowOnMutationInDev');
var invariant = require('fbjs/lib/invariant');
var GROUP_TYPES_OPTIONS = [ const GROUP_TYPES_OPTIONS = {
'Album', Album: 'Album',
'All', All: 'All',
'Event', Event: 'Event',
'Faces', Faces: 'Faces',
'Library', Library: 'Library',
'PhotoStream', PhotoStream: 'PhotoStream',
'SavedPhotos', // default SavedPhotos: 'SavedPhotos', // default
]; };
var ASSET_TYPE_OPTIONS = [ const ASSET_TYPE_OPTIONS = {
'All', All: 'All',
'Videos', Videos: 'Videos',
'Photos', // default Photos: 'Photos',
]; };
// Flow treats Object and Array as disjoint types, currently.
deepFreezeAndThrowOnMutationInDev((GROUP_TYPES_OPTIONS: any));
deepFreezeAndThrowOnMutationInDev((ASSET_TYPE_OPTIONS: any));
/** /**
* Shape of the param arg for the `getPhotos` function. * Shape of the param arg for the `getPhotos` function.
*/ */
var getPhotosParamChecker = createStrictShapeTypeChecker({ const getPhotosParamChecker = createStrictShapeTypeChecker({
/** /**
* The number of photos wanted in reverse order of the photo application * The number of photos wanted in reverse order of the photo application
* (i.e. most recent first for SavedPhotos). * (i.e. most recent first for SavedPhotos).
@ -58,7 +53,7 @@ var getPhotosParamChecker = createStrictShapeTypeChecker({
/** /**
* Specifies which group types to filter the results to. * Specifies which group types to filter the results to.
*/ */
groupTypes: PropTypes.oneOf(GROUP_TYPES_OPTIONS), groupTypes: PropTypes.oneOf(Object.keys(GROUP_TYPES_OPTIONS)),
/** /**
* Specifies filter on group names, like 'Recent Photos' or custom album * Specifies filter on group names, like 'Recent Photos' or custom album
@ -69,7 +64,7 @@ var getPhotosParamChecker = createStrictShapeTypeChecker({
/** /**
* Specifies filter on asset type * Specifies filter on asset type
*/ */
assetType: PropTypes.oneOf(ASSET_TYPE_OPTIONS), assetType: PropTypes.oneOf(Object.keys(ASSET_TYPE_OPTIONS)),
/** /**
* Filter by mimetype (e.g. image/jpeg). * Filter by mimetype (e.g. image/jpeg).
@ -80,28 +75,30 @@ var getPhotosParamChecker = createStrictShapeTypeChecker({
/** /**
* Shape of the return value of the `getPhotos` function. * Shape of the return value of the `getPhotos` function.
*/ */
var getPhotosReturnChecker = createStrictShapeTypeChecker({ const getPhotosReturnChecker = createStrictShapeTypeChecker({
// $FlowFixMe(>=0.41.0) // $FlowFixMe(>=0.41.0)
edges: PropTypes.arrayOf(createStrictShapeTypeChecker({ edges: PropTypes.arrayOf(
node: createStrictShapeTypeChecker({ createStrictShapeTypeChecker({
type: PropTypes.string.isRequired, node: createStrictShapeTypeChecker({
group_name: PropTypes.string.isRequired, type: PropTypes.string.isRequired,
image: createStrictShapeTypeChecker({ group_name: PropTypes.string.isRequired,
uri: PropTypes.string.isRequired, image: createStrictShapeTypeChecker({
height: PropTypes.number.isRequired, uri: PropTypes.string.isRequired,
width: PropTypes.number.isRequired, height: PropTypes.number.isRequired,
isStored: PropTypes.bool, width: PropTypes.number.isRequired,
isStored: PropTypes.bool,
}).isRequired,
timestamp: PropTypes.number.isRequired,
location: createStrictShapeTypeChecker({
latitude: PropTypes.number,
longitude: PropTypes.number,
altitude: PropTypes.number,
heading: PropTypes.number,
speed: PropTypes.number,
}),
}).isRequired, }).isRequired,
timestamp: PropTypes.number.isRequired, }),
location: createStrictShapeTypeChecker({ ).isRequired,
latitude: PropTypes.number,
longitude: PropTypes.number,
altitude: PropTypes.number,
heading: PropTypes.number,
speed: PropTypes.number,
}),
}).isRequired,
})).isRequired,
page_info: createStrictShapeTypeChecker({ page_info: createStrictShapeTypeChecker({
has_next_page: PropTypes.bool.isRequired, has_next_page: PropTypes.bool.isRequired,
start_cursor: PropTypes.string, start_cursor: PropTypes.string,
@ -121,12 +118,13 @@ var getPhotosReturnChecker = createStrictShapeTypeChecker({
* *
*/ */
class CameraRoll { class CameraRoll {
static GroupTypesOptions: Object = GROUP_TYPES_OPTIONS;
static GroupTypesOptions: Array<string>; static AssetTypeOptions: Object = ASSET_TYPE_OPTIONS;
static AssetTypeOptions: Array<string>;
static saveImageWithTag(tag: string): Promise<Object> { static saveImageWithTag(tag: string): Promise<Object> {
console.warn('CameraRoll.saveImageWithTag is deprecated. Use CameraRoll.saveToCameraRoll instead'); console.warn(
'CameraRoll.saveImageWithTag is deprecated. Use CameraRoll.saveToCameraRoll instead',
);
return this.saveToCameraRoll(tag, 'photo'); return this.saveToCameraRoll(tag, 'photo');
} }
@ -144,16 +142,19 @@ class CameraRoll {
* *
* Returns a Promise which will resolve with the new URI. * Returns a Promise which will resolve with the new URI.
*/ */
static saveToCameraRoll(tag: string, type?: 'photo' | 'video'): Promise<Object> { static saveToCameraRoll(
tag: string,
type?: 'photo' | 'video',
): Promise<Object> {
invariant( invariant(
typeof tag === 'string', typeof tag === 'string',
'CameraRoll.saveToCameraRoll must be a valid string.' 'CameraRoll.saveToCameraRoll must be a valid string.',
); );
invariant( invariant(
type === 'photo' || type === 'video' || type === undefined, type === 'photo' || type === 'video' || type === undefined,
// $FlowFixMe(>=0.28.0) // $FlowFixMe(>=0.28.0)
`The second argument to saveToCameraRoll must be 'photo' or 'video'. You passed ${type}` `The second argument to saveToCameraRoll must be 'photo' or 'video'. You passed ${type}`,
); );
let mediaType = 'photo'; let mediaType = 'photo';
@ -214,32 +215,39 @@ class CameraRoll {
*/ */
static getPhotos(params) { static getPhotos(params) {
if (__DEV__) { if (__DEV__) {
checkPropTypes({params: getPhotosParamChecker}, {params}, 'params', 'CameraRoll.getPhotos'); checkPropTypes(
{params: getPhotosParamChecker},
{params},
'params',
'CameraRoll.getPhotos',
);
} }
if (arguments.length > 1) { if (arguments.length > 1) {
console.warn('CameraRoll.getPhotos(tag, success, error) is deprecated. Use the returned Promise instead'); console.warn(
'CameraRoll.getPhotos(tag, success, error) is deprecated. Use the returned Promise instead',
);
let successCallback = arguments[1]; let successCallback = arguments[1];
if (__DEV__) { if (__DEV__) {
const callback = arguments[1]; const callback = arguments[1];
successCallback = (response) => { successCallback = response => {
checkPropTypes( checkPropTypes(
{response: getPhotosReturnChecker}, {response: getPhotosReturnChecker},
{response}, {response},
'response', 'response',
'CameraRoll.getPhotos callback' 'CameraRoll.getPhotos callback',
); );
callback(response); callback(response);
}; };
} }
const errorCallback = arguments[2] || ( () => {} ); const errorCallback = arguments[2] || (() => {});
RCTCameraRollManager.getPhotos(params).then(successCallback, errorCallback); RCTCameraRollManager.getPhotos(params).then(
successCallback,
errorCallback,
);
} }
// TODO: Add the __DEV__ check back in to verify the Promise result // TODO: Add the __DEV__ check back in to verify the Promise result
return RCTCameraRollManager.getPhotos(params); return RCTCameraRollManager.getPhotos(params);
} }
} }
CameraRoll.GroupTypesOptions = GROUP_TYPES_OPTIONS;
CameraRoll.AssetTypeOptions = ASSET_TYPE_OPTIONS;
module.exports = CameraRoll; module.exports = CameraRoll;