import * as React from 'react'; import { StyleSheet, View, Button, Text, Switch, TextInput, Keyboard, } from 'react-native'; // @ts-ignore: CameraRollExample has no typings in same folder import CameraRoll from '../../js/CameraRoll'; interface State { fetchingPhotos: boolean; timeTakenMillis: number | null; output: CameraRoll.PhotoIdentifiersPage | null; include: CameraRoll.Include[]; /** * `first` argument passed into `getPhotos`, but as a string. Validate it * with `this.first()` before using. */ firstStr: string; /** `after` passed into `getPhotos`. Not passed if empty */ after: string; } const includeValues: CameraRoll.Include[] = [ 'filename', 'fileSize', 'location', ]; /** * Example for testing performance differences between `getPhotos` and * `getPhotosFast` */ export default class GetPhotosPerformanceExample extends React.PureComponent< {}, State > { state: State = { fetchingPhotos: false, timeTakenMillis: null, output: null, include: [], firstStr: '1000', after: '', }; first = () => { const first = parseInt(this.state.firstStr, 10); if (first < 0 || !Number.isInteger(first)) { return null; } return first; }; startFetchingPhotos = async () => { const {include} = this.state; const first = this.first(); if (first === null) { return; } this.setState({fetchingPhotos: true}); Keyboard.dismiss(); const params: CameraRoll.GetPhotosParams = {first, include}; const startTime = Date.now(); const output: CameraRoll.PhotoIdentifiersPage = await CameraRoll.getPhotos( params, ); const endTime = Date.now(); this.setState({ output, timeTakenMillis: endTime - startTime, fetchingPhotos: false, }); }; handleIncludeChange = ( includeValue: CameraRoll.Include, changedTo: boolean, ) => { if (changedTo === false) { const include = this.state.include.filter( value => value !== includeValue, ); this.setState({include}); } else { const include = [...this.state.include, includeValue]; this.setState({include}); } }; render() { const { fetchingPhotos, timeTakenMillis, output, include, firstStr, } = this.state; const first = this.first(); return ( {includeValues.map(includeValue => ( {includeValue} this.handleIncludeChange(includeValue, changedTo) } /> ))} first {first === null && ( (enter a positive number) )} this.setState({firstStr: text})} style={[styles.textInput, first === null && styles.textInputError]} />