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 83a58f6c4f
commit 71e84e6ee8
2 changed files with 95 additions and 82 deletions

View File

@ -20,6 +20,7 @@
*
* @flow
* @providesModule CameraRollExample
* @format
*/
'use strict';
@ -33,7 +34,7 @@ const {
Switch,
Text,
View,
TouchableOpacity
TouchableOpacity,
} = ReactNative;
const invariant = require('fbjs/lib/invariant');
@ -63,7 +64,9 @@ class CameraRollExample extends React.Component {
/>
<Text>{'Group Type: ' + this.state.groupTypes}</Text>
<CameraRollView
ref={(ref) => { this._cameraRollView = ref; }}
ref={ref => {
this._cameraRollView = ref;
}}
batchSize={20}
groupTypes={this.state.groupTypes}
renderImage={this._renderImage}
@ -72,29 +75,28 @@ class CameraRollExample extends React.Component {
);
}
loadAsset = (asset) => {
loadAsset(asset) {
if (this.props.navigator) {
this.props.navigator.push({
title: 'Camera Roll Image',
component: AssetScaledImageExampleView,
backButtonTitle: 'Back',
passProps: { asset: asset },
passProps: {asset: asset},
});
}
};
}
_renderImage = (asset) => {
_renderImage = asset => {
const imageSize = this.state.bigImages ? 150 : 75;
const imageStyle = [styles.image, {width: imageSize, height: imageSize}];
const {location} = asset.node;
const locationStr = location ? JSON.stringify(location) : 'Unknown location';
const locationStr = location
? JSON.stringify(location)
: 'Unknown location';
return (
<TouchableOpacity key={asset} onPress={ this.loadAsset.bind( this, asset ) }>
<TouchableOpacity key={asset} onPress={this.loadAsset.bind(this, asset)}>
<View style={styles.row}>
<Image
source={asset.node.image}
style={imageStyle}
/>
<Image source={asset.node.image} style={imageStyle} />
<View style={styles.info}>
<Text style={styles.url}>{asset.node.image.uri}</Text>
<Text>{locationStr}</Text>
@ -106,8 +108,8 @@ class CameraRollExample extends React.Component {
);
};
_onSliderChange = (value) => {
const options = CameraRoll.GroupTypesOptions;
_onSliderChange = value => {
const options = Object.keys(CameraRoll.GroupTypesOptions);
const index = Math.floor(value * options.length * 0.99);
const groupTypes = options[index];
if (groupTypes !== this.state.groupTypes) {
@ -115,10 +117,10 @@ class CameraRollExample extends React.Component {
}
};
_onSwitchChange = (value) => {
_onSwitchChange = value => {
invariant(this._cameraRollView, 'ref should be set');
this._cameraRollView.rendererChanged();
this.setState({ bigImages: value });
this.setState({bigImages: value});
};
}
@ -140,10 +142,13 @@ const styles = StyleSheet.create({
});
exports.title = 'Camera Roll';
exports.description = 'Example component that uses CameraRoll to list user\'s photos';
exports.description =
"Example component that uses CameraRoll to list user's photos";
exports.examples = [
{
title: 'Photos',
render(): React.Element<any> { return <CameraRollExample />; }
}
render(): React.Element<any> {
return <CameraRollExample />;
},
},
];

View File

@ -8,41 +8,36 @@
*
* @providesModule CameraRoll
* @flow
* @format
*/
'use strict';
var {PropTypes, checkPropTypes} = require('React');
var RCTCameraRollManager = require('NativeModules').CameraRollManager;
const {PropTypes, checkPropTypes} = require('React');
const RCTCameraRollManager = require('NativeModules').CameraRollManager;
var createStrictShapeTypeChecker = require('createStrictShapeTypeChecker');
var deepFreezeAndThrowOnMutationInDev =
require('deepFreezeAndThrowOnMutationInDev');
var invariant = require('fbjs/lib/invariant');
const createStrictShapeTypeChecker = require('createStrictShapeTypeChecker');
const invariant = require('fbjs/lib/invariant');
var GROUP_TYPES_OPTIONS = [
'Album',
'All',
'Event',
'Faces',
'Library',
'PhotoStream',
'SavedPhotos', // default
];
const GROUP_TYPES_OPTIONS = {
Album: 'Album',
All: 'All',
Event: 'Event',
Faces: 'Faces',
Library: 'Library',
PhotoStream: 'PhotoStream',
SavedPhotos: 'SavedPhotos', // default
};
var ASSET_TYPE_OPTIONS = [
'All',
'Videos',
'Photos', // default
];
// Flow treats Object and Array as disjoint types, currently.
deepFreezeAndThrowOnMutationInDev((GROUP_TYPES_OPTIONS: any));
deepFreezeAndThrowOnMutationInDev((ASSET_TYPE_OPTIONS: any));
const ASSET_TYPE_OPTIONS = {
All: 'All',
Videos: 'Videos',
Photos: 'Photos',
};
/**
* 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
* (i.e. most recent first for SavedPhotos).
@ -58,7 +53,7 @@ var getPhotosParamChecker = createStrictShapeTypeChecker({
/**
* 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
@ -69,7 +64,7 @@ var getPhotosParamChecker = createStrictShapeTypeChecker({
/**
* 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).
@ -80,28 +75,30 @@ var getPhotosParamChecker = createStrictShapeTypeChecker({
/**
* Shape of the return value of the `getPhotos` function.
*/
var getPhotosReturnChecker = createStrictShapeTypeChecker({
const getPhotosReturnChecker = createStrictShapeTypeChecker({
// $FlowFixMe(>=0.41.0)
edges: PropTypes.arrayOf(createStrictShapeTypeChecker({
node: createStrictShapeTypeChecker({
type: PropTypes.string.isRequired,
group_name: PropTypes.string.isRequired,
image: createStrictShapeTypeChecker({
uri: PropTypes.string.isRequired,
height: PropTypes.number.isRequired,
width: PropTypes.number.isRequired,
isStored: PropTypes.bool,
edges: PropTypes.arrayOf(
createStrictShapeTypeChecker({
node: createStrictShapeTypeChecker({
type: PropTypes.string.isRequired,
group_name: PropTypes.string.isRequired,
image: createStrictShapeTypeChecker({
uri: PropTypes.string.isRequired,
height: PropTypes.number.isRequired,
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,
timestamp: PropTypes.number.isRequired,
location: createStrictShapeTypeChecker({
latitude: PropTypes.number,
longitude: PropTypes.number,
altitude: PropTypes.number,
heading: PropTypes.number,
speed: PropTypes.number,
}),
}).isRequired,
})).isRequired,
}),
).isRequired,
page_info: createStrictShapeTypeChecker({
has_next_page: PropTypes.bool.isRequired,
start_cursor: PropTypes.string,
@ -121,12 +118,13 @@ var getPhotosReturnChecker = createStrictShapeTypeChecker({
*
*/
class CameraRoll {
static GroupTypesOptions: Array<string>;
static AssetTypeOptions: Array<string>;
static GroupTypesOptions: Object = GROUP_TYPES_OPTIONS;
static AssetTypeOptions: Object = ASSET_TYPE_OPTIONS;
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');
}
@ -144,16 +142,19 @@ class CameraRoll {
*
* 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(
typeof tag === 'string',
'CameraRoll.saveToCameraRoll must be a valid string.'
'CameraRoll.saveToCameraRoll must be a valid string.',
);
invariant(
type === 'photo' || type === 'video' || type === undefined,
// $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';
@ -214,32 +215,39 @@ class CameraRoll {
*/
static getPhotos(params) {
if (__DEV__) {
checkPropTypes({params: getPhotosParamChecker}, {params}, 'params', 'CameraRoll.getPhotos');
checkPropTypes(
{params: getPhotosParamChecker},
{params},
'params',
'CameraRoll.getPhotos',
);
}
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];
if (__DEV__) {
const callback = arguments[1];
successCallback = (response) => {
successCallback = response => {
checkPropTypes(
{response: getPhotosReturnChecker},
{response},
'response',
'CameraRoll.getPhotos callback'
'CameraRoll.getPhotos callback',
);
callback(response);
};
}
const errorCallback = arguments[2] || ( () => {} );
RCTCameraRollManager.getPhotos(params).then(successCallback, errorCallback);
const errorCallback = arguments[2] || (() => {});
RCTCameraRollManager.getPhotos(params).then(
successCallback,
errorCallback,
);
}
// TODO: Add the __DEV__ check back in to verify the Promise result
return RCTCameraRollManager.getPhotos(params);
}
}
CameraRoll.GroupTypesOptions = GROUP_TYPES_OPTIONS;
CameraRoll.AssetTypeOptions = ASSET_TYPE_OPTIONS;
module.exports = CameraRoll;