-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.jsx
More file actions
288 lines (270 loc) · 7.06 KB
/
App.jsx
File metadata and controls
288 lines (270 loc) · 7.06 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
import React, { useState } from 'react';
import {
View,
Text,
TextInput,
TouchableOpacity,
Image,
FlatList,
StyleSheet,
Alert,
StatusBar,
} from 'react-native';
import * as FileSystem from 'expo-file-system';
import * as MediaLibrary from 'expo-media-library';
import { LinearGradient } from 'expo-linear-gradient'; // Import LinearGradient component
const App = () => {
const [qrText, setQrText] = useState('');
const [qrImage, setQrImage] = useState('');
const [showQR, setShowQR] = useState(false);
const [alertMessage, setAlertMessage] = useState('');
const [filteredSuggestions, setFilteredSuggestions] = useState([]);
const suggestions = [
'https://www.google.com',
'https://www.facebook.com',
'https://www.instagram.com',
'https://www.amazon.com',
'https://www.openAI.com',
'https://www.x.com',
'https://www.youtube.com',
'Hello, World!',
'React Native QR Generator',
];
// Filter suggestions based on input
const handleInputChange = (text) => {
setQrText(text);
if (text.trim().length > 0) {
const filtered = suggestions.filter((suggestion) =>
suggestion.toLowerCase().includes(text.toLowerCase())
);
setFilteredSuggestions(filtered);
} else {
setFilteredSuggestions([]);
}
};
// Select a suggestion
const selectSuggestion = (suggestion) => {
setQrText(suggestion);
setFilteredSuggestions([]);
};
// Generate QR Code URL
const generateQR = () => {
if (qrText.trim().length > 0) {
const qrUrl = `https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=${encodeURIComponent(
qrText.trim()
)}`;
setQrImage(qrUrl);
setShowQR(false);
setAlertMessage('QR Code generated successfully!');
setTimeout(() => setAlertMessage(''), 3000); // Clear alert after 3 seconds
} else {
Alert.alert('Input Error', 'Please enter some text or URL!');
}
};
// Toggle QR Code Visibility
const toggleQR = () => {
if (!qrImage) {
Alert.alert('No QR Code', 'Please generate the QR code first!');
} else {
setShowQR((prev) => !prev);
}
};
// Download QR Code
const downloadQR = async () => {
if (!qrImage) {
Alert.alert(
'No QR Code',
'Please generate the QR code before downloading!'
);
return;
}
try {
const { status } = await MediaLibrary.requestPermissionsAsync();
if (status !== 'granted') {
Alert.alert(
'Permission Denied',
'Permission to access media library is required!'
);
return;
}
// Download image
const localUri = `${FileSystem.documentDirectory}qrcode.png`;
const downloadedFile = await FileSystem.downloadAsync(qrImage, localUri);
// Save to media library
await MediaLibrary.saveToLibraryAsync(downloadedFile.uri);
Alert.alert(
'Success',
'QR Code downloaded successfully to your gallery!'
);
} catch (error) {
Alert.alert('Error', 'Failed to download the QR Code.');
}
};
return (
<LinearGradient
colors={['#C4D9FF', '#4DA1A9']} // Starting and ending colors of the gradient
style={styles.container}>
{/* Customize the Status Bar */}
<StatusBar barStyle="dark-content" backgroundColor="#2E5077" />
{/* App Bar */}
<View style={styles.appBar}>
<Text style={styles.appBarText}>QRgeN</Text>
</View>
<Text style={styles.title}>Enter your Text or URL</Text>
<TextInput
style={styles.input}
placeholder="Text or URL"
value={qrText}
onChangeText={handleInputChange}
/>
{/* Suggestions */}
{filteredSuggestions.length > 0 && (
<FlatList
style={styles.suggestionsList}
data={filteredSuggestions}
keyExtractor={(item, index) => index.toString()}
renderItem={({ item }) => (
<TouchableOpacity
onPress={() => selectSuggestion(item)}
style={styles.suggestionItem}>
<Text>{item}</Text>
</TouchableOpacity>
)}
/>
)}
<TouchableOpacity style={styles.button} onPress={generateQR}>
<Text style={styles.buttonText}>Generate QR Code</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.card} onPress={toggleQR}>
<Text style={styles.cardText}>
{showQR ? 'QR Code:' : 'Tap to Show QR Code'}
</Text>
{showQR && qrImage && (
<View style={styles.qrContainer}>
<Image source={{ uri: qrImage }} style={styles.qrImage} />
</View>
)}
</TouchableOpacity>
{qrImage && (
<TouchableOpacity style={styles.downloadButton} onPress={downloadQR}>
<Text style={styles.buttonText}>Download QR Code</Text>
</TouchableOpacity>
)}
{alertMessage ? (
<Text style={styles.alertMessage}>{alertMessage}</Text>
) : null}
</LinearGradient>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'flex-start',
alignItems: 'center',
padding: 20,
},
appBar: {
width: '150%',
height: 60,
backgroundColor: '#2E5077',
justifyContent: 'center',
alignItems: 'center',
elevation: 3, // For shadow effect
marginBottom: 50,
marginTop: 30,
},
appBarText: {
fontSize: 24,
fontWeight: 'bold',
color: '#fff',
},
title: {
fontSize: 20,
fontWeight: '700',
fontFamily: 'serif',
marginBottom: 10,
},
input: {
width: '90%',
height: 50,
borderColor: '#3c3b3b',
borderWidth: 3,
borderRadius: 5,
padding: 10,
marginBottom: 20,
backgroundColor: '#fff',
},
suggestionsList: {
width: '90%',
backgroundColor: '#fff',
borderRadius: 5,
borderColor: '#ccc',
borderWidth: 1,
marginBottom: 10,
maxHeight: 100,
},
suggestionItem: {
padding: 10,
borderBottomWidth: 1,
borderBottomColor: '#eee',
},
button: {
backgroundColor: '#08478a',
paddingVertical: 15,
paddingHorizontal: 50,
borderRadius: 10,
marginVertical: 10,
alignItems: 'center',
},
buttonText: {
color: '#fff',
fontSize: 15,
fontWeight: '500',
},
card: {
width: '80%',
padding: 20,
backgroundColor: '#fff',
borderRadius: 15,
elevation: 5,
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.2,
shadowRadius: 4,
alignItems: 'center',
marginTop: 20,
marginBottom: 10,
},
cardText: {
fontSize: 18,
color: '#555',
marginBottom: 10,
textAlign: 'center',
},
qrContainer: {
marginTop: 15,
width: 200,
height: 200,
},
qrImage: {
width: '100%',
height: '100%',
borderRadius: 15,
},
downloadButton: {
backgroundColor: '#333',
paddingVertical: 15,
paddingHorizontal: 50,
borderRadius: 10,
marginVertical: 10,
alignItems: 'center',
},
alertMessage: {
marginTop: 15,
fontSize: 16,
color: 'green',
fontWeight: '500',
textAlign: 'center',
},
});
export default App;