-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
159 lines (147 loc) · 4.18 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
import React, {useState, useEffect} from 'react';
import {
Dimensions,
StyleSheet,
SafeAreaView,
View,
Image,
FlatList,
Button,
Text,
TouchableOpacity,
Alert
} from 'react-native';
import SearchInput from './SearchInput';
import Modal from "react-native-modal";
import CameraRoll from "@react-native-community/cameraroll";
import RNFetchBlob from 'react-native-fetch-blob'
export default function App() {
const [allGifResults, setAllGifResults] = useState([]);
const [isModalVisible, setModalVisible] = useState(false);
const [imgUri, setImgUri] = useState('');
useEffect(() => {
fetch(
'http://api.giphy.com/v1/gifs/trending?api_key=MKSpDwx7kTCbRp23VtVsP4d0EvfwIgSg&limit=50',
)
.then(response => response.json())
.then(responseJson => {
for (let item of responseJson.data) {
allGifResultsHandler(item.images.fixed_height.url);
// console.log(item.images.fixed_height.url);
}
})
.catch(error => {
console.error(error);
});
},[]);
function showModal(uri) {
setImgUri(uri);
setModalVisible(true);
}
function toggleModal() {
setModalVisible(isModalVisible => !isModalVisible);
}
async function downloadImage() {
if (Platform.OS === 'android') {
RNFetchBlob
.config({
fileCache : true,
appendExt : 'jpg'
})
.fetch('GET', imgUri)
.then((res) => {
CameraRoll.saveToCameraRoll(res.path())
.then(Alert.alert('Success', 'Photo added to camera roll!'))
.catch(err => console.log('err:', err))
})
} else {
CameraRoll.saveToCameraRoll(imgUri)
.then(Alert.alert('Success', 'Photo added to camera roll!'))
}
toggleModal();
}
function addSearchResultsHandler(searchTerm) {
console.log(searchTerm);
setAllGifResults([]);
fetchResults(searchTerm);
}
function allGifResultsHandler(url) {
setAllGifResults(currentGifs => [...currentGifs, {id: url, value: url}]);
}
function fetchResults(searchTerm) {
fetch(
'http://api.giphy.com/v1/gifs/search?q=' +
searchTerm +
'&api_key=MKSpDwx7kTCbRp23VtVsP4d0EvfwIgSg&limit=50',
)
.then(response => response.json())
.then(responseJson => {
for (let item of responseJson.data) {
allGifResultsHandler(item.images.fixed_height.url);
console.log(item.images.fixed_height.url);
}
})
.catch(error => {
console.error(error);
});
}
return (
<SafeAreaView style={styles.container}>
<View style={styles.screen}>
<SearchInput onSearchButtonPressed={addSearchResultsHandler} />
</View>
<FlatList
keyExtractor={(item, index) => item.id}
data={allGifResults}
numColumns={2}
renderItem={itemData => (
<TouchableOpacity onPress={showModal.bind(this, itemData.item.value)}>
<Image
source={itemData.item.value ? {uri: itemData.item.value} : null}
style={styles.images}
/>
</TouchableOpacity>
)}
/>
<Modal isVisible={isModalVisible}>
<View style={modalStyles.content}>
<Text style={modalStyles.contentTitle}>Are you going to save this gif to camera!</Text>
<View style={{display:'flex', flexDirection: 'row'}}>
<View style={{marginRight:10}}>
<Button onPress={downloadImage} title="Yes"/>
</View>
<Button onPress={toggleModal} title="No" />
</View>
</View>
</Modal>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF',
},
screen: {
margin: 10,
},
images: {
width: Dimensions.get('window').width / 2 - 20,
height: Dimensions.get('window').width / 2 - 20,
margin: 10,
},
});
const modalStyles = StyleSheet.create({
content: {
backgroundColor: 'white',
padding: 22,
justifyContent: 'center',
alignItems: 'center',
borderRadius: 4,
borderColor: 'rgba(0, 0, 0, 0.1)',
},
contentTitle: {
fontSize: 20,
marginBottom: 12,
},
});