-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.android.js
More file actions
342 lines (307 loc) · 10 KB
/
Copy pathindex.android.js
File metadata and controls
342 lines (307 loc) · 10 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
'use strict';
//an unique value merger
function uniqBy(a, key) {
var seen = {};
return a.filter(function (item) {
var k = key(item);
return seen.hasOwnProperty(k) ? false : (seen[k] = true);
})
}
var React = require('react-native');
var {
AppRegistry,
Image,
StyleSheet,
Text,
View,
TouchableOpacity,
Navigator,
Component,
BackAndroid,
ListView,
} = React;
var GridView = require('react-native-grid-view');
var INSTAGRAM_API_URL = 'https://api.instagram.com/v1/';
var INSTAGRAM_API_RECENT_URL = INSTAGRAM_API_URL + 'tags/cannabis/media/recent?client_id=d68357d3c1c343cc984f34d45ef502c8';
//TODO: calculate how many columns will be available on the screen
var THUMBNAILS_PER_ROW = 3;
var UPDATE_RECENTS_DELAY = 5000;//in milliseconds
//default back behaviour for android, does not work on emulator :(
BackAndroid.addEventListener('hardwareBackPress', () => {
console.log("back pressed standalone");
if (navigator && navigator.getCurrentRoutes().length > 1) {
navigator.pop();
return true;
}
return false;
});
var ThumbnailView = React.createClass({
render() {
return (
<View >
<TouchableOpacity onPress={this.onClickThumbnail}>
<Image
source={{uri: this.props.data.images.thumbnail.url}}
style={styles.thumbnail}>
<Text
style={styles.title}
numberOfLines={3}>{this.props.data.caption.text}></Text>
</Image>
</TouchableOpacity>
</View>
);
},
onClickThumbnail(event) {
this.viewInstagramPostDetails();
},
viewInstagramPostDetails() {
this.props.navigator.push({
title: "Details",
id: "details_route",
data: this.props.data,
});
},
});
var HomeScreen = React.createClass({
getInitialState() {
return {
dataSource: null,
loaded: false,
};
},
// componentDidMount is called by react when the component
// has been rendered on the page. We can set the interval here:
componentDidMount() {
//pre fetch
this.fetchInstagramRecents();
//schedule fetching from instagram every n milliseconds
this.timer = setInterval(this.fetchInstagramRecents, UPDATE_RECENTS_DELAY);
},
// This method is called immediately before the component is removed
// from the page and destroyed. We can clear the interval here:
componentWillUnmount: function () {
//remove the data fetching scheduling
clearInterval(this.timer);
},
render() {
if (!this.state.loaded) {
return this.renderLoadingView();
}
return (
<GridView
items={this.state.dataSource}
itemsPerRow={THUMBNAILS_PER_ROW}
renderItem={this.renderItem}
contentContainerStyle={styles.gridView}/>
)
},
renderItem(item) {
return <ThumbnailView data={item} navigator={this.props.navigator}/>
},
renderLoadingView() {
//TODO: create spinner
return (
<View style={styles.loadingContainer}>
<Text>
Loading grams...
</Text>
</View>
);
},
fetchInstagramRecents() {
fetch(INSTAGRAM_API_RECENT_URL)
.then((response) => response.json())
.then((responseData) => {
//if we actually have something
if (responseData.data) {
//save the data to the state
this.setState({
dataSource: (
this.state.dataSource == null
?
responseData.data
:
uniqBy(
//add the new response
this.state.dataSource.concat(responseData.data),
//identify unique by their link URI's
function (item) {
return item.link;
})
),
loaded: true,
});
}
})
.catch(console.warn)
.done();
},
});
var DetailsScreen = React.createClass({
getInitialState: function () {
return {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
}),
};
},
renderCommentItem(comment){
var commentDate = new Date(parseInt(comment.created_time) * 1000);
var parsedDate = (commentDate.getMonth() + 1) + "/" + commentDate.getDate() + "/" + commentDate.getFullYear();
return (
<View>
<View style={{flexDirection: 'row'}}>
<Image
source={{uri: comment.from.profile_picture}}
style={styles.commentProfileImage}
/>
<Text style={styles.commentTime}>{parsedDate}</Text>
</View>
<View >
<Text style={{color: "blue"}}>{comment.from.username} <Text
style={styles.commentText}>{comment.text}</Text></Text>
</View>
</View>
);
},
render() {
console.log("Details screen render() his.props.data.comments.count = " + this.props.data.comments.count);
if (this.props.data.comments.count > 0) {
}
return ( <View style={styles.flexOne}>
<TouchableOpacity onPress={this.props.navigator.pop} style={styles.flexOne}>
<View style={styles.detailsHeaderContainer}>
<View style={{flexDirection: 'row',flex : 1}}>
<View style={styles.profileImageContainer}>
<Image
source={{uri: this.props.data.caption.from.profile_picture}}
resizeMode={Image.resizeMode.cover}
style={styles.profileImage}
/>
</View>
<View style={styles.detailsTextContainer}>
<Text style={{color: "blue"}}>{this.props.data.caption.from.username} <Text
style={{color: "grey"}}>{this.props.data.caption.text}</Text></Text>
</View>
</View>
</View>
<View style={styles.detailsImageContainer}>
<Image
resizeMode={Image.resizeMode.cover}
//hack for modifying the url to get a bigger picture, currently the api does not return the link to the 1080x1080 picture
source={{uri: this.props.data.images.standard_resolution.url.replace("s640x640","s1080x1080")}}
style={styles.myImage}>
</Image>
</View>
<View style={styles.flexOne}>
<Text>
Comments:
</Text>
<ListView
renderRow={this.renderCommentItem}
dataSource={this.state.dataSource.cloneWithRows(this.props.data.comments.data)}
loadData={this.fetchInstagramPost}/>
</View>
</TouchableOpacity>
</View>);
},
});
//the main app class
var InstagramReader = React.createClass({
renderScene(route, navigator) {
console.log("route data=" + route.data);
switch (route.id) {
case 'home_route':
return <HomeScreen navigator={navigator}/>;
case 'details_route':
return <DetailsScreen navigator={navigator} data={route.data}/>;
}
},
render() {
return (
<Navigator
ref={(navigator) => { this.navigator = navigator; }}
initialRoute={{ title: "Details", id: "home_route"}}
renderScene={this.renderScene}
/>
);
},
});
var styles = StyleSheet.create({
loadingContainer: {
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
alignItems: 'center',
justifyContent: 'center',
},
title: {
opacity: 0.7,
paddingHorizontal: 40,
fontSize: 10,
backgroundColor: 'grey',
color: 'white'
},
thumbnail: {
width: 120,
height: 120,
},
flexOne: {
flex: 1,
},
myImage: {
alignItems: 'stretch',
flex: 1,
//width: 120,
//height: 120,
},
flexZero: {
flex: 0,
},
commentTime: {
textAlign: 'right',
fontSize: 10,
},
commentText: {
color: "grey",
fontSize: 10,
},
commentProfileImage: {
width: 40,
height: 40,
},
profileImage: {
width: 80,
height: 80,
},
profileImageContainer: {
padding: 4,
},
detailsImageContainer: {
flex: 1,
paddingTop: 10,
},
detailsHeaderContainer: {
flex: 0,
padding: 5,
},
detailsTextContainer: {
padding: 5,
flex:1,
},
gridView: {
//For aligning the children, we need to modify lib file
//in ./node_modules/react-native-grid-view/index.js
//change the styles to this:
//var styles = StyleSheet.create({
// group: {
// flexDirection: 'row',
// alignItems: 'flex-start',
// justifyContent: 'flex-start',
// }
//});
},
});
AppRegistry.registerComponent('InstagramReader', () => InstagramReader);