-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
225 lines (200 loc) · 6.07 KB
/
app.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
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
import _ from 'underscore'
import { FlatList, Dimensions } from 'react-native'
import Modal from 'react-native-modal'
import React from 'react';
import { Alert, Button, Clipboard, StyleSheet, Text, View } from 'react-native';
import {Image, TextInput, TouchableHighlight, ScrollView} from 'react-native'
import debounce from 'debounce'
import Config from './config'
import StatusBarBackground from './status-bar-background'
import rower from './rower'
// https://stackoverflow.com/questions/41056761/detect-scrollview-has-reached-the-end/41058382
const isCloseToBottom = ({layoutMeasurement, contentOffset, contentSize}) => {
const paddingToBottom = 400;
return layoutMeasurement.height + contentOffset.y >=
contentSize.height - paddingToBottom;
};
export default class App extends React.Component {
constructor(props) {
super(props)
this.state = {
query: '',
results: [],
rows: [],
modal_visible: false,
loaded_offsets: {},
layout_measurement: null,
content_offset: {x: 0, y: 0},
content_size: null,
}
//this.fetch = debounce(this.fetch, 500)
}
render_row(row) {
const {index, entries} = row
const height = parseInt(entries[0].images.fixed_height.height)
//console.log(JSON.stringify([height,index,this.state.content_offset]))
if (height * index - this.state.content_offset.y < -2000 ||
height * index - this.state.content_offset.y > 2000)
{
return <View style={{height:height}}>
<Text>Loading...</Text>
</View>
}
return <View style={styles.images}>
{entries.map((result, result_index) => (
result.images ?
<View key={result_index} style={{
width: parseInt(result.images.fixed_height.width) + 6,
height: parseInt(result.images.fixed_height.height) + 6,
}}>
<TouchableHighlight
onLongPress={this.image_did_long_press.bind(this, result)}
>
<Image
source={{uri: result.images.fixed_height.url}}
style={{
alignSelf: 'center',
width: parseInt(result.images.fixed_height.width),
height: parseInt(result.images.fixed_height.height),
}}
/>
</TouchableHighlight>
</View>
: null
))}
</View>
}
render() {
return (
<View style={styles.container} onLayout={this.layout_did_change.bind(this)}>
<StatusBarBackground style={{backgroundColor:'#191970'}}/>
<Text style={styles.header}>
Giphy Search
</Text>
<TextInput style={styles.query}
value={this.state.query}
placeholder='Search...'
onChangeText={this.query_did_change.bind(this)}
/>
<ScrollView style={{width: '100%'}}
onScroll={this.did_scroll.bind(this)}
scrollEventThrottle={200}
>
{this.state.rows.map(row => this.render_row(row))}
</ScrollView>
<Modal isVisible={this.state.modal_visible}
onBackdropPress={this.backdrop_did_press.bind(this)}
>
<View style={{ backgroundColor: '#fff' }}>
<Button
title='Copy URL'
onPress={this.copy_did_press.bind(this)}
/>
</View>
</Modal>
</View>
);
}
layout_did_change() {
const rows = rower({
results: _.filter(this.state.results, result => result.images),
image_fn: result => result.images.fixed_height,
// https://stackoverflow.com/questions/30203154/get-size-of-a-view-in-react-native
container_width: Dimensions.get('window').width,
padding: 6,
})
this.setState({rows: rows})
}
query_did_change(text) {
this.setState({query: text})
this.fetch(text)
}
fetch(query, offset) {
offset = offset || 0
if (offset && this.state.loaded_offsets[offset]) {
return
}
this.state.loaded_offsets[offset] = true
fetch(`https://api.giphy.com/v1/gifs/search?q=${query}&api_key=${Config.api_key}&offset=${offset}`)
.then(response => response.json())
.then(payload => {
let results
if (offset == 0) {
results = payload.data
this.setState({loaded_offsets: {}})
} else {
results = [...this.state.results, ...payload.data]
}
const rows = rower({
results: _.filter(results, result => result.images),
image_fn: result => result.images.fixed_height,
container_width: Dimensions.get('window').width,
padding: 6,
})
this.setState({
results: results,
rows: rows,
query: query,
offset: offset,
})
})
.catch(response => {
this.setState({
error: response.message,
})
})
this.setState({
results: [...this.state.results, query],
})
}
image_did_long_press(gif) {
this.setState({modal_visible: true, gif: gif})
}
async copy_did_press() {
await Clipboard.setString(this.state.gif.images.fixed_height.url)
this.setState({modal_visible: false})
}
backdrop_did_press() {
this.setState({modal_visible: false})
}
did_scroll({nativeEvent}) {
if (isCloseToBottom(nativeEvent)) {
this.fetch(this.state.query, this.state.results.length)
}
const {layoutMeasurement, contentOffset, contentSize} = nativeEvent
this.setState({
content_offset: contentOffset,
content_size: contentSize,
layout_measurement: layoutMeasurement,
})
}
componentDidMount() {
this.query_did_change('yay')
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
images: {
backgroundColor: '#000',
flex: 1, flexDirection: 'row', flexWrap: 'wrap',
justifyContent: 'center',
},
header: {
fontSize: 24,
},
query: {
height: 50,
backgroundColor: '#eee',
borderColor: '#444',
borderWidth: 1,
margin: 3,
padding: 4,
fontSize: 20,
width: '50%',
},
});