This repository has been archived by the owner on Sep 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ios.js
145 lines (123 loc) · 4.03 KB
/
index.ios.js
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
import React, {
AppRegistry,
Component,
StyleSheet,
Text,
View,
Image,
Dimensions,
StatusBar,
ScrollView
} from 'react-native';
StatusBar.setBarStyle('light-content');
console.disableYellowBox = true; // This gets in the way!
// imageSize is used to statically size <Image/> instances
const windowDims = Dimensions.get('window'),
itemSize = (windowDims.width / 2) - 20;
// This is *the* placeholder image to be used in steps #3 and #5
const placeholder = require('./images/placeholder.png');
const styles = StyleSheet.create({
container : {
paddingTop : 30,
justifyContent : 'center',
alignItems : 'center',
backgroundColor : '#F5FCFF',
flexDirection : 'row',
flexWrap : 'wrap'
},
child : {
width : itemSize,
height : itemSize,
margin : 7
},
topBar : {
position : 'absolute',
top : 0,
height : 25,
width : windowDims.width,
backgroundColor : 'rgba(0,0,0,.8)'
}
});
class Placeholder extends Component {
// Initial state
state = {
data : [] // empty data array
};
// Step #4 (Self-bound function)
// We will update the state of the application so that images can render
onAfterLoad = (data) => {
this.setState({
data : data.data
});
};
// Step #1 & #2
// Here we’ll fetch JSON for images to be displayed
componentWillMount() {
// The URL below has an 'r' parameter that is used as a 'cache buster' and is only intended for demonstration purposes
let url = 'http://api.giphy.com/v1/gifs/search?q=javascript&api_key=dc6zaTOxFJmzC&limit=30&r=' + Math.random();
console.log('Loading data')
// Initiate query, parse, then update view via callback
fetch(url)
.then(function(r) {
return r.json();
})
.then(this.onAfterLoad) // Success callback registration
.catch(function(e) { // Failure callback registartion
alert('Failure fetching data');
console.log(e)
});
}
// This will be responsible for rendering the image components
buildImages(data) {
let images = [],
length = data.length,
i = 0,
randVal = '?r=' + Math.random(), // Cache busting for testing only can be removed
source,
item;
// Empty array?
if (data.length == 0) {
// This console.log() call can be removed.
console.log('Rendering placeholders');
// Fill the array with 10 undefines
data.length = length = 10;
}
else {
// This else branch is here just for debugging and can be removed.
console.log(`Got data. Rendering ${length} images.`);
}
for (; i < length; i++) {
item = data[i];
// For when we actually have data
if (item) {
source = {
uri : item.images.original_still.url + randVal,
width : itemSize,
height : itemSize
}
}
images.push(
<Image style={styles.child}
source={source}
defaultSource={placeholder}
key={'img' + i}/>
)
}
return images;
}
render() {
let state = this.state,
data = state.data,
images = this.buildImages(data);
return (
<View style={{flex:1}}>
<ScrollView contentContainerStyle={styles.container}
style={{backgroundColor: '#F5FCFF'}}>
{images}
</ScrollView>
<View style={styles.topBar}/>
</View>
);
}
}
AppRegistry.registerComponent('Placeholder', () => Placeholder);