-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ios.js
188 lines (167 loc) · 4.4 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
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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
'use strict';
var React = require('react-native');
// ***
// The following lines from...
// https://github.com/nolanlawson/pouchdb-async-storage
// Throw an exception: Requiring unknown module "request" and suggests restarting the packager, which doesn't help.
// ****
//
// var PouchDB = require('pouchdb');
// require('pouchdb-async-storage');
// var db = new PouchDB('mydb', {adapter: 'asyncstorage'});
var API_KEY = '7waqfqbprs7pajbz28mqf6vz';
var API_URL = 'http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json';
var PAGE_SIZE = 25;
var PARAMS = '?apikey=' + API_KEY + '&page_limit=' + PAGE_SIZE;
var REQUEST_URL = API_URL + PARAMS;
var {
AppRegistry,
StyleSheet,
Image,
ListView,
Text,
View,
AsyncStorage,
TextInput,
ActivityIndicatorIOS,
} = React;
var offlineMovies = React.createClass({
getInitialState: function() {
// AsyncStorage.removeItem('movies');
return {
dataSource: new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2,
}),
loaded: false,
cached: false, // tracks whether the view was built from cached data
};
},
componentDidMount: function() {
this.fetchData();
},
fetchData: function() {
fetch(REQUEST_URL)
.then((response) => response.json())
.then((responseData) => {
console.log('SUCCESS: Live data retrieved and cached');
this.setState({
dataSource: this.state.dataSource.cloneWithRows(responseData.movies),
loaded: true,
});
AsyncStorage.setItem('movies', JSON.stringify(responseData.movies));
})
.catch((warning) => {
console.log('WARN: unable to retrieve live data, so attempting to grab cache instead', warning);
AsyncStorage.getItem('movies')
.then((response) => {
console.log('SUCCESS: cached data retrieved'),
this.setState({
dataSource: this.state.dataSource.cloneWithRows(JSON.parse(response)),
loaded: true,
cached: true,
});
})
.catch((error) => {
console.log('ERROR: unable to retrieve live nor cached data')
});
}).done();
},
render: function() {
if(!this.state.loaded) {
return this.renderLoadingView();
}
return (
<ListView
dataSource={this.state.dataSource}
renderRow= {this.renderMovie}
renderHeader = {this.searchView}
style={styles.listView}
/>
)
},
searchView: function() {
return (
<View style={styles.search}>
{ this.renderCachedDataWarning() }
<TextInput
style={{height: 40, borderColor: 'gray', borderWidth: 1}}
clearButtonMode='while-editing'
placeholder='Find a movie'
/>
</View>
)
},
renderLoadingView: function() {
return (
<View style={styles.container}>
<ActivityIndicatorIOS
color="#cc5500"
size="large"
/>
</View>
)
},
renderCachedDataWarning: function() {
if(!this.state.cached) { return false}
return (
<View style={styles.cacheWarning}>
<Text style={{color: '#ffffff', fontSize: 16, textAlign: 'center'}}>
Cached Data Below
</Text>
</View>
)
},
renderMovie: function(movie) {
return (
<View style={styles.container}>
<Image
source={{ uri: movie.posters.thumbnail }}
style={styles.thumbnail}
/>
<View style={styles.containerRight}>
<Text style={styles.title}>{movie.title}</Text>
<Text style={styles.year}>{movie.year}</Text>
</View>
</View>
);
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
borderBottomWidth: 1,
borderBottomColor: '#DDDDDD',
},
containerRight: {
flex: 1,
},
thumbnail: {
width: 53,
height: 81,
},
title: {
fontSize: 20,
marginBottom: 8,
textAlign: 'center',
},
year: {
textAlign: 'center',
},
listView: {
paddingTop: 20,
backgroundColor: '#F5FCFF',
},
cacheWarning: {
padding:20,
backgroundColor: '#cc5500',
}
});
AppRegistry.registerComponent('offlineMovies', () => offlineMovies);