|
| 1 | +import React from 'react' |
| 2 | + |
| 3 | +import { |
| 4 | + View, |
| 5 | + Text, |
| 6 | + Button, |
| 7 | + Image, |
| 8 | + ActivityIndicator, |
| 9 | + StyleSheet, |
| 10 | + Dimensions, |
| 11 | + ScrollView, |
| 12 | + CameraRoll, |
| 13 | + TouchableHighlight, |
| 14 | + Platform, |
| 15 | + Alert |
| 16 | +} from 'react-native' |
| 17 | + |
| 18 | +import RNFetchBlob from 'react-native-fetch-blob' |
| 19 | + |
| 20 | +const { width, height } = Dimensions.get('window') |
| 21 | +let styles |
| 22 | +class ImageBrowser extends React.Component { |
| 23 | + static navigationOptions = { |
| 24 | + title: 'Unsplash Images', |
| 25 | + } |
| 26 | + |
| 27 | + state = { |
| 28 | + images: [], |
| 29 | + loading: true, |
| 30 | + page: 1 |
| 31 | + } |
| 32 | + |
| 33 | + componentDidMount() { |
| 34 | + this.fetchPhotos() |
| 35 | + } |
| 36 | + |
| 37 | + fetchPhotos = () => { |
| 38 | + this.setState({ loading: true }) |
| 39 | + fetch(`https://api.unsplash.com/photos/?page=${this.state.page}&per_page=30&client_id=${YOURAPPID}`) |
| 40 | + .then(res => res.json()) |
| 41 | + .then(images => { |
| 42 | + this.state.images.push(...images) |
| 43 | + console.log('this.state.images: ', this.state.images) |
| 44 | + this.setState({ images: this.state.images, loading: false, page: this.state.page + 1 }) |
| 45 | + }) |
| 46 | + } |
| 47 | + |
| 48 | + saveToCameraRoll = (image) => { |
| 49 | + if (Platform.OS === 'android') { |
| 50 | + RNFetchBlob |
| 51 | + .config({ |
| 52 | + fileCache : true, |
| 53 | + appendExt : 'jpg' |
| 54 | + }) |
| 55 | + .fetch('GET', image.urls.small) |
| 56 | + .then((res) => { |
| 57 | + CameraRoll.saveToCameraRoll(res.path()) |
| 58 | + .then(Alert.alert('Success', 'Photo added to camera roll!')) |
| 59 | + .catch(err => console.log('err:', err)) |
| 60 | + }) |
| 61 | + } else { |
| 62 | + CameraRoll.saveToCameraRoll(image.urls.small) |
| 63 | + .then(Alert.alert('Success', 'Photo added to camera roll!')) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + render() { |
| 68 | + return ( |
| 69 | + <View style={{flex: 1}}> |
| 70 | + <Text style={styles.title}>Unsplash Images</Text> |
| 71 | + { |
| 72 | + this.state.loading ? ( |
| 73 | + <Text style={{ padding: 10, textAlign: 'center' }}>Loading...</Text> |
| 74 | + ) : ( |
| 75 | + <Button |
| 76 | + onPress={this.fetchPhotos} |
| 77 | + title='View More' |
| 78 | + /> |
| 79 | + ) |
| 80 | + } |
| 81 | + <ScrollView contentContainerStyle={styles.scrollContainer}> |
| 82 | + { |
| 83 | + this.state.images.map((image, i) => { |
| 84 | + return ( |
| 85 | + <TouchableHighlight |
| 86 | + key={i} |
| 87 | + onPress={() => this.saveToCameraRoll(image)} |
| 88 | + underlayColor='transparent' |
| 89 | + > |
| 90 | + <Image |
| 91 | + style={styles.image} |
| 92 | + source={{ uri: image.urls.small }} |
| 93 | + /> |
| 94 | + </TouchableHighlight> |
| 95 | + ) |
| 96 | + }) |
| 97 | + } |
| 98 | + </ScrollView> |
| 99 | + </View> |
| 100 | + ) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +styles = StyleSheet.create({ |
| 105 | + scrollContainer: { |
| 106 | + flexDirection: 'row', |
| 107 | + flexWrap: 'wrap' |
| 108 | + }, |
| 109 | + centerLoader: { |
| 110 | + height: height - 100, |
| 111 | + width, |
| 112 | + justifyContent: 'center', |
| 113 | + alignItems: 'center' |
| 114 | + }, |
| 115 | + image: { |
| 116 | + width: width / 2, height: width / 2 |
| 117 | + }, |
| 118 | + title: { |
| 119 | + textAlign: 'center', |
| 120 | + padding: 20 |
| 121 | + } |
| 122 | +}) |
| 123 | + |
| 124 | +export default ImageBrowser |
0 commit comments