general cleanup

Added typescript binding
Fixed flow type issues
renamed NativeModule to RNCCameraRoll
Added basic jest tests
prettier/lint issues fixed
This commit is contained in:
Bartol Karuza 2019-03-03 14:39:57 +08:00
parent 9f176640d8
commit 0c453b6118
18 changed files with 272 additions and 111 deletions

View File

@ -15,6 +15,9 @@ node_modules/react-native/\.buckd/
; Ignore unexpected extra "@providesModule"
.*/node_modules/.*/node_modules/fbjs/.*
; Ignore extra CameraRoll
.*/node_modules/react-native/Libraries/CameraRoll/CameraRoll.js
; Ignore duplicate module providers
; For RN Apps installed via npm, "Libraries" folder is inside
; "node_modules/react-native" but in the source repo it is in the root

21
LICENCE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2015-present, Facebook, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -1,5 +1,6 @@
# `@react-native-community/cameraroll`
# react-native-cameraroll
[![CircleCI Status](https://img.shields.io/circleci/project/github/react-native-community/react-native-netinfo/master.svg)](https://circleci.com/gh/react-native-community/workflows/react-native-cameraroll/tree/master) ![Supports Android and iOS](https://img.shields.io/badge/platforms-android%20|%20ios-lightgrey.svg) ![MIT License](https://img.shields.io/npm/l/@react-native-community/cameraroll.svg)
## Getting started
@ -34,12 +35,20 @@
compile project(':@react-native-community/cameraroll')
```
## Migrating from the core `react-native` module
This module was created when the CameraRoll was split out from the core of React Native. To migrate to this module you need to follow the installation instructions above and then change you imports from:
```javascript
import { CameraRoll } from "react-native";
```
to:
```javascript
import CameraRoll from "@react-native-community/cameraroll";
```
## Usage
---
id: cameraroll
title: CameraRoll
---
`CameraRoll` provides access to the local camera roll or photo library.

View File

@ -57,7 +57,7 @@ import javax.annotation.Nullable;
@ReactModule(name = CameraRollModule.NAME)
public class CameraRollModule extends ReactContextBaseJavaModule {
public static final String NAME = "CameraRollManager";
public static final String NAME = "RNCCameraRoll";
private static final String ERROR_UNABLE_TO_LOAD = "E_UNABLE_TO_LOAD";
private static final String ERROR_UNABLE_TO_LOAD_PERMISSION = "E_UNABLE_TO_LOAD_PERMISSION";

View File

@ -1,23 +1,18 @@
/**
* Sample React Native App
* https://github.com/facebook/react-native
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
* @lint-ignore-every XPLATJSCOPYRIGHT1
*/
import React, {Component} from 'react';
import {StyleSheet, Text, View} from 'react-native';
import CameraRoll from '@react-native-community/cameraroll';
import {StyleSheet, View} from 'react-native';
import CameraRollExample from './js/CameraRollExample';
type Props = {};
export default class App extends Component<Props> {
componentDidMount = async () => {
const photos = await CameraRoll.getPhotos({first: 0, assetType: 'Photos'});
};
render() {
return (
<View style={styles.container}>
@ -34,14 +29,4 @@ const styles = StyleSheet.create({
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});

View File

@ -1,15 +0,0 @@
/**
* @format
* @lint-ignore-every XPLATJSCOPYRIGHT1
*/
import 'react-native';
import React from 'react';
import App from '../App';
// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';
it('renders correctly', () => {
renderer.create(<App />);
});

View File

@ -1,15 +1,26 @@
const {device, expect, element, by, waitFor} = require('detox');
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
/* eslint-env jest */
const {device, expect, element, by} = require('detox');
describe('CameraRoll', () => {
beforeEach(async () => {
await device.launchApp({permissions: {
await device.launchApp({
permissions: {
photos: 'YES',
// camera: 'YES',
}});
},
});
});
it('should load example app with no errors and show all the examples by default', async () => {
await expect(element(by.text('Big Images'))).toExist();
});
});

View File

@ -1,8 +1,12 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @lint-ignore-every XPLATJSCOPYRIGHT1
* @flow
*/
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';

View File

@ -22,7 +22,7 @@ const {
TouchableOpacity,
} = ReactNative;
const invariant = require('invariant');
const invariant = require('fbjs/lib/invariant');
const CameraRollView = require('./CameraRollView');
@ -80,7 +80,7 @@ export default class CameraRollExample extends React.Component<Props, State> {
);
}
loadAsset(asset) {
loadAsset(asset: PhotoIdentifier) {
if (this.props.navigator) {
this.props.navigator.push({
title: 'Camera Roll Image',

View File

@ -69,7 +69,7 @@ RCT_ENUM_CONVERTER(PHAssetCollectionSubtype, (@{
@implementation RNCCameraRollManager
RCT_EXPORT_MODULE(CameraRollManager)
RCT_EXPORT_MODULE(RNCCameraRoll)
@synthesize bridge = _bridge;

View File

@ -11,10 +11,9 @@
const PropTypes = require('prop-types');
const {checkPropTypes} = PropTypes;
const RCTCameraRollManager = require('NativeModules').CameraRollManager;
const RNCCameraRoll = require('nativeInterface');
const deprecatedCreateStrictShapeTypeChecker = require('deprecatedCreateStrictShapeTypeChecker');
const invariant = require('invariant');
const invariant = require('fbjs/lib/invariant');
const GROUP_TYPES_OPTIONS = {
Album: 'Album',
@ -170,13 +169,12 @@ class CameraRoll {
}
static deletePhotos(photos: Array<string>) {
return RCTCameraRollManager.deletePhotos(photos);
return RNCCameraRoll.deletePhotos(photos);
}
/**
* Saves the photo or video to the camera roll or photo library.
*
* See https://facebook.github.io/react-native/docs/cameraroll.html#savetocameraroll
*/
static saveToCameraRoll(
tag: string,
@ -200,7 +198,7 @@ class CameraRoll {
mediaType = 'video';
}
return RCTCameraRollManager.saveToCameraRoll(tag, mediaType);
return RNCCameraRoll.saveToCameraRoll(tag, mediaType);
}
/**
@ -236,13 +234,10 @@ class CameraRoll {
};
}
const errorCallback = arguments[2] || (() => {});
RCTCameraRollManager.getPhotos(params).then(
successCallback,
errorCallback,
);
RNCCameraRoll.getPhotos(params).then(successCallback, errorCallback);
}
// TODO: Add the __DEV__ check back in to verify the Promise result
return RCTCameraRollManager.getPhotos(params);
return RNCCameraRoll.getPhotos(params);
}
}

View File

@ -0,0 +1,5 @@
module.exports = {
deletePhotos: jest.fn(),
saveToCameraRoll: jest.fn(),
getPhotos: jest.fn(),
};

View File

@ -0,0 +1,31 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
/* eslint-env jest */
import CameraRoll from '../CameraRoll';
const NativeModule = require('nativeInterface');
describe('CameraRoll', () => {
it('Should call deletePhotos', () => {
CameraRoll.deletePhotos(['a uri']);
expect(NativeModule.deletePhotos.mock.calls).toMatchSnapshot();
});
it('Should call saveToCameraRoll', async () => {
await CameraRoll.saveToCameraRoll('a tag', 'photo');
expect(NativeModule.saveToCameraRoll.mock.calls).toMatchSnapshot();
});
it('Should call getPhotos', async () => {
await CameraRoll.getPhotos({first: 0});
expect(NativeModule.getPhotos.mock.calls).toMatchSnapshot();
});
});

View File

@ -0,0 +1,30 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`CameraRoll Should call deletePhotos 1`] = `
Array [
Array [
Array [
"a uri",
],
],
]
`;
exports[`CameraRoll Should call getPhotos 1`] = `
Array [
Array [
Object {
"first": 0,
},
],
]
`;
exports[`CameraRoll Should call saveToCameraRoll 1`] = `
Array [
Array [
"a tag",
"photo",
],
]
`;

2
js/nativeInterface.js Normal file
View File

@ -0,0 +1,2 @@
const RNCCameraRoll = require('NativeModules').RNCCameraRoll;
export default RNCCameraRoll;

25
tsconfig.json Normal file
View File

@ -0,0 +1,25 @@
{
"include": ["typings/**/*.d.ts"],
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"importHelpers": true,
"jsx": "react",
"sourceMap": true,
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"skipLibCheck": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"lib": ["es2015", "es2016", "esnext", "dom"]
},
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}

87
typings/CameraRoll.d.ts vendored Normal file
View File

@ -0,0 +1,87 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
export type GroupType =
'Album' |
'All' |
'Event' |
'Faces' |
'Library' |
'PhotoStream' |
'SavedPhotos';
export type AssetType =
'All' |
'Videos' |
'Photos';
export interface GetPhotosParams {
first: number,
after?: string,
groupTypes?: GroupType,
groupName?: string,
assetType?: AssetType
mimeTypes?: Array<string>,
};
export interface PhotoIdentifier {
node: {
type: string,
group_name: string,
image: {
filename: string,
uri: string,
height: number,
width: number,
isStored?: boolean,
playableDuration: number,
},
timestamp: number,
location?: {
latitude?: number,
longitude?: number,
altitude?: number,
heading?: number,
speed?: number,
},
},
}
export interface PhotoIdentifiersPage {
edges: Array<PhotoIdentifier>,
page_info: {
has_next_page: boolean,
start_cursor?: string,
end_cursor?: string,
},
}
export interface CameraRollStatic {
/**
* `CameraRoll.saveImageWithTag()` is deprecated. Use `CameraRoll.saveToCameraRoll()` instead.
*/
saveImageWithTag: (tag: string) => Promise<string>;
/**
* Delete a photo from the camera roll or media library. photos is an array of photo uri's.
*/
deletePhotos: (photos: Array<string>) => void,
/**
* Saves the photo or video to the camera roll or photo library.
*/
saveToCameraRoll: (tag: string, type?: 'photo' | 'video') => Promise<string>;
/**
* Returns a Promise with photo identifier objects from the local camera
* roll of the device matching shape defined by `getPhotosReturnChecker`.
*/
getPhotos: (params: GetPhotosParams) => Promise<PhotoIdentifiersPage>;
}

View File

@ -2184,7 +2184,7 @@ debug@^4.0.0, debug@^4.0.1, debug@^4.1.0, debug@^4.1.1:
dependencies:
ms "^2.1.1"
debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
@ -3754,7 +3754,7 @@ import-local@^2.0.0:
pkg-dir "^3.0.0"
resolve-cwd "^2.0.0"
imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
@ -4979,11 +4979,6 @@ lockfile@^1.0.4:
dependencies:
signal-exit "^3.0.2"
lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=
lodash._baseuniq@~4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
@ -4992,33 +4987,11 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"
lodash._bindcallback@*:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=
lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=
lodash._createcache@*:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
dependencies:
lodash._getnative "^3.0.0"
lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=
lodash._getnative@*, lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=
lodash._root@~3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"
@ -5069,11 +5042,6 @@ lodash.padstart@^4.1.0:
resolved "https://registry.yarnpkg.com/lodash.padstart/-/lodash.padstart-4.6.1.tgz#d2e3eebff0d9d39ad50f5cbd1b52a7bce6bb611b"
integrity sha1-0uPuv/DZ05rVD1y9G1KnvOa7YRs=
lodash.restparam@*:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=
lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
@ -7297,7 +7265,7 @@ readable-stream@~1.1.10:
isarray "0.0.1"
string_decoder "~0.10.x"
readdir-scoped-modules@*, readdir-scoped-modules@^1.0.0:
readdir-scoped-modules@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/readdir-scoped-modules/-/readdir-scoped-modules-1.0.2.tgz#9fafa37d286be5d92cbaebdee030dc9b5f406747"
integrity sha1-n6+jfShr5dksuuve4DDcm19AZ0c=