import { CameraRoll } from '@react-native-camera-roll/camera-roll'; import * as React from 'react'; import { Button, Keyboard, StyleSheet, Switch, Text, TextInput, View, } from 'react-native'; import type { GetPhotosParams, Include, PhotoIdentifiersPage, } from '@react-native-camera-roll/camera-roll'; interface State { fetchingPhotos: boolean; timeTakenMillis: number | null; output: PhotoIdentifiersPage | null; include: Include[]; /** * `first` argument passed into `getPhotos`, but as a string. Validate it * with `this.first()` before using. */ firstStr: string; includeSharedAlbums: boolean; } const includeValues: Include[] = [ 'filename', 'fileSize', 'location', 'imageSize', 'playableDuration', ]; /** * 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', includeSharedAlbums: false, }; first = () => { const first = parseInt(this.state.firstStr, 10); if (first < 0 || !Number.isInteger(first)) { return null; } return first; }; startFetchingPhotos = async () => { const {include, includeSharedAlbums} = this.state; const first = this.first(); if (first === null) { return; } this.setState({fetchingPhotos: true}); Keyboard.dismiss(); const params: GetPhotosParams = { first, include, includeSharedAlbums, }; const startTime = Date.now(); const output: PhotoIdentifiersPage = await CameraRoll.getPhotos(params); const endTime = Date.now(); this.setState({ output, timeTakenMillis: endTime - startTime, fetchingPhotos: false, }); }; handleIncludeChange = (includeValue: 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, includeSharedAlbums, } = this.state; const first = this.first(); return ( {includeValues.map(includeValue => ( {includeValue} this.handleIncludeChange(includeValue, changedTo) } /> ))} includeSharedAlbums this.setState({includeSharedAlbums: changedTo}) } /> first {first === null && ( (enter a positive number) )} this.setState({firstStr: text})} style={[styles.textInput, first === null && styles.textInputError]} />