-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGallery.js
More file actions
164 lines (150 loc) · 3.7 KB
/
Gallery.js
File metadata and controls
164 lines (150 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict';
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Image,
ListView,
TouchableHighlight,
} from 'react-native'
import Dimensions from 'Dimensions';
// import FullPageView module
var FullPageView = require('./FullPageView')
////////////// CONSTANTS and PARAMETERS //////////////
// setting size of pictures loaded for tumbnails. see for reference:
// https://github.com/500px/api-documentation/blob/master/basics/formats_and_terms.md#image-urls-and-image-sizes
const IMAGE_SIZE = 31
// set width and height for tumbnail pictures
var _height = (Dimensions.get('window').height)/2 - 100;
var _width = (Dimensions.get('window').width)/2-10;
////////////// HELPERS FUNCTIONS ///////////////////
// get url based on page number
var URL = function(page, imageSize = IMAGE_SIZE){
return 'https://api.500px.com/v1/photos?feature=popular&image_size[]=' +
imageSize+'&consumer_key=wB4ozJxTijCwNuggJvPGtBGCRqaZVcF6jsrzUadF&page='+
page;
}
////////////// MAIN COMPONENT //////////////
class Gallery extends Component{
constructor(props) {
super(props);
this.state = {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
}),
loaded: false, // for tracking initial load
loadingMore: false, // for tracking load more
};
}
componentDidMount(){
this._photos = []
this.page = 1 // set initial page that will be loaded
this.fetchPhotos(this.page);
}
fetchPhotos(page) {
fetch(URL(page))
.then((response) => response.json())
.then((responseData) => {
// load new bunch of photos
var newPhotos = responseData.photos
// add new photos to existing list
this._photos = this._photos.concat(newPhotos)
// increment page for next load
this.page +=1;
this.setState({
dataSource:
this.state.dataSource.cloneWithRows(this._photos),
loaded:
true,
});
})
.catch(error =>
this.setState({
message: 'Something went wrong ' + error
}))
.done();
}
render(){
// while image is loading show a message
if (!this.state.loaded) {
return this.renderLoadingView();
}
return (
<ListView
contentContainerStyle={styles.list}
initialListSize={20}
dataSource = {this.state.dataSource}
renderRow = {this.renderPhoto.bind(this)}
scrollRenderAheadDistance={1500}
onEndReached = {this._onEndReached.bind(this)}
onEndReachedThreshold = {300}
style = {styles.listView} />
);
}
_onEndReached() {
console.log('onEndReached');
if (this.state.loadingMore) {
// if we already loading - do nothing
return;
}
this.setState({
loadingMore: false
});
// load more photos
this.fetchPhotos(this.page);
}
renderLoadingView() {
return (
<View>
<Text style = {styles.loadingText}>
Photos are being loaded...
</Text>
</View>
);
}
renderPhoto(photo) {
return (
// when pressed render foolscreen photo
<TouchableHighlight onPress ={() => this.imagePressed(photo.id)}>
<View>
<Image
source = {{uri: photo.image_url[0]}}
style = {styles.tumbnail} />
</View>
</TouchableHighlight>
)
}
imagePressed(photoId){
this.props.navigator.push({
title: "FullPageView",
id: 'fullScreen_' + photoId, // pass id of an image to navigator
});
}
}
////////////// STYLES //////////
const styles = StyleSheet.create({
list : {
justifyContent: 'space-around',
flexDirection: 'row',
flexWrap: 'wrap'
},
tumbnail : {
width: _width,
height: _height,
margin: 3
},
listView: {
flex: 2,
paddingTop: 20,
backgroundColor: '#F5FCFF'
},
loadingText: {
marginTop: 100,
fontSize: 20,
textAlign: 'center',
},
});
// export module
module.exports = Gallery;