-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
397 lines (374 loc) · 11.6 KB
/
App.js
File metadata and controls
397 lines (374 loc) · 11.6 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import { CameraView, useCameraPermissions } from 'expo-camera';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { MaterialIcons } from '@expo/vector-icons';
import { Vibration, ActivityIndicator } from 'react-native';
import { useEffect, useRef, useState } from 'react';
import {
Alert,
Button,
Image,
Linking,
ScrollView,
StyleSheet,
Text,
TextInput,
TouchableOpacity,
View,
} from 'react-native';
export default function App() {
const [permission, requestPermission] = useCameraPermissions();
const [token, setToken] = useState(null);
const [username, setUsername] = useState(null);
const [inputToken, setInputToken] = useState('');
const [screen, setScreen] = useState('home');
const [result, setResult] = useState(null);
const [folders, setFolders] = useState([]);
const [selectedFolderId, setSelectedFolderId] = useState(null);
const [isPickerOpen, setIsPickerOpen] = useState(false);
const [hasScanned, setHasScanned] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const scannedRef = useRef(false);
useEffect(() => {
const loadStored = async () => {
const savedToken = await AsyncStorage.getItem('discogs_token');
const savedUser = await AsyncStorage.getItem('discogs_username');
if (savedToken) setToken(savedToken);
if (savedUser) setUsername(savedUser);
};
loadStored();
}, []);
const handleSaveToken = async () => {
if (!inputToken) return;
try {
const response = await fetch('https://api.discogs.com/oauth/identity', {
headers: { Authorization: `Discogs token=${inputToken}` },
});
if (!response.ok) throw new Error('Invalid token');
const data = await response.json();
await AsyncStorage.setItem('discogs_token', inputToken);
await AsyncStorage.setItem('discogs_username', data.username);
setToken(inputToken);
setUsername(data.username);
setInputToken('');
} catch (err) {
Alert.alert('Error', 'Failed to validate token');
}
};
const handleScan = async ({ data }) => {
if (scannedRef.current || !token || !username) return;
scannedRef.current = true;
setScreen('home');
setIsLoading(true);
Vibration.vibrate(10);
const [releaseId, instanceId] = data.trim().split('.');
if (!/^\d+$/.test(releaseId) || !/^\d+$/.test(instanceId)) {
Alert.alert('Invalid QR Code', 'Expected format: release_id.instance_id');
scannedRef.current = false;
setIsLoading(false);
return;
}
try {
const headers = { Authorization: `Discogs token=${token}` };
const releaseUrl = `https://api.discogs.com/users/${username}/collection/releases/${releaseId}`;
const releaseRes = await fetch(releaseUrl, { headers });
const releaseData = await releaseRes.json();
const match = releaseData.releases?.find(
r =>
r.basic_information.id.toString() === releaseId &&
r.instance_id.toString() === instanceId
);
if (match) {
const foldersRes = await fetch(
`https://api.discogs.com/users/${username}/collection/folders`,
{ headers }
);
const foldersData = await foldersRes.json();
setFolders(foldersData.folders.filter(f => f.id !== 0));
setSelectedFolderId(match.folder_id);
setResult({
title: match.basic_information.title,
artist: match.basic_information.artists.map(a => a.name).join(', '),
label: match.basic_information.labels.map(l => l.name).join(', '),
catno: match.basic_information.labels.map(l => l.catno).join(', '),
thumb: match.basic_information.thumb,
folder_id: match.folder_id,
release_id: match.basic_information.id,
instance_id: match.instance_id,
});
setHasScanned(true);
} else {
setResult(false);
setHasScanned(true);
}
} catch (err) {
console.error('API error:', err);
Alert.alert('Error', 'Failed to query Discogs collection');
} finally {
scannedRef.current = false;
setIsLoading(false);
}
};
const handleMoveToFolder = async (targetFolderId) => {
if (!result?.release_id || !result?.instance_id) {
Alert.alert('Error', 'No scanned release to move.');
return;
}
if (targetFolderId === selectedFolderId) {
setIsPickerOpen(false);
return;
}
try {
const url = `https://api.discogs.com/users/${username}/collection/folders/${selectedFolderId}/releases/${result.release_id}/instances/${result.instance_id}`;
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Discogs token=${token}`,
},
body: JSON.stringify({ folder_id: targetFolderId }),
});
if (!response.ok) throw new Error('Failed to move record');
Alert.alert('Success', `Moved to '${folders.find(f => f.id === targetFolderId)?.name}'`);
setSelectedFolderId(targetFolderId);
} catch (err) {
console.error('Move error:', err);
Alert.alert('Error', 'Could not move record');
} finally {
setIsPickerOpen(false);
}
};
if (!permission) return <View />;
if (!permission.granted) {
return (
<ScrollView contentContainerStyle={styles.container}>
<View style={styles.centered}>
<Text style={styles.message}>Camera access is required to scan items.</Text>
<TouchableOpacity style={styles.primaryButton} onPress={requestPermission}>
<Text style={styles.buttonText}>Continue</Text>
</TouchableOpacity>
</View>
</ScrollView>
);
}
if (screen === 'camera') {
return (
<View style={styles.containerCamera}>
<CameraView
style={styles.cameraHalf}
onBarcodeScanned={handleScan}
barcodeScannerSettings={{ barcodeTypes: ['qr'] }}
/>
<View style={styles.bottomHalf}>
<Button title="Cancel" onPress={() => setScreen('home')} />
</View>
</View>
);
}
return (
<ScrollView contentContainerStyle={styles.container}>
{!token || !username ? (
<>
<Text style={styles.header}>Discogs Personal Access Token</Text>
<Text style={styles.message}>We need this for the app to access your collection.</Text>
<Text style={styles.message}>You'll need to generate a new token, and then paste the letters/numbers into the box below!</Text>
<Text
style={styles.link}
onPress={() => Linking.openURL('https://www.discogs.com/settings/developers')}
>
Get your token from Discogs
</Text>
<TextInput
style={styles.input}
placeholder="Paste token here"
value={inputToken}
onChangeText={setInputToken}
autoCapitalize="none"
/>
<TouchableOpacity style={styles.primaryButton} onPress={handleSaveToken}>
<Text style={styles.buttonText}>Save Token</Text>
</TouchableOpacity>
</>
) : (
<>
<Text style={styles.header}>Discogs: {username}</Text>
<TouchableOpacity
style={styles.scanButton}
onPress={() => {
setResult(null);
setFolders([]);
setSelectedFolderId(null);
setHasScanned(false);
setScreen('camera');
}}
>
<MaterialIcons name="qr-code-scanner" size={24} color="#fff" style={{ marginRight: 8 }} />
<Text style={styles.buttonText}>Scan item</Text>
</TouchableOpacity>
{isLoading && (
<View style={styles.result}>
<ActivityIndicator size="large" color="#333" />
</View>
)}
{!isLoading && hasScanned && !result && (
<View style={styles.result}>
<Text style={styles.message}>Not found in collection!</Text>
</View>
)}
{!isLoading && result && (
<View style={styles.result}>
<Text style={styles.resultTitle}>{result.title}</Text>
<Text style={styles.resultArtist}>{result.artist}</Text>
{result.thumb && <Image source={{ uri: result.thumb }} style={styles.thumb} />}
<Text style={styles.resultLabel}>{result.label} ({result.catno})</Text>
<TouchableOpacity style={styles.pickerTrigger} onPress={() => setIsPickerOpen(!isPickerOpen)}>
<Text style={styles.pickerTriggerText}>
Location: {folders.find(f => f.id === selectedFolderId)?.name || 'Unknown'}
</Text>
</TouchableOpacity>
{isPickerOpen && (
<View style={styles.pickerList}>
{folders.map(folder => (
<TouchableOpacity
key={folder.id}
style={[
styles.pickerItem,
folder.id === selectedFolderId && styles.pickerItemSelected,
]}
onPress={() => handleMoveToFolder(folder.id)}
>
<Text style={styles.pickerItemText}>{folder.name}</Text>
</TouchableOpacity>
))}
</View>
)}
</View>
)}
</>
)}
</ScrollView>
);
}
const styles = StyleSheet.create({
container: {
flexGrow: 1,
alignItems: 'stretch',
backgroundColor: 'white',
padding: 20,
},
containerCamera: {
flexGrow: 1,
alignItems: 'stretch',
backgroundColor: 'black',
padding: 0,
},
centered: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
header: {
fontSize: 26,
fontWeight: 'bold',
textAlign: 'center',
marginBottom: 16,
marginTop: 80,
},
link: {
color: 'blue',
textAlign: 'center',
marginBottom: 12,
textDecorationLine: 'underline',
},
input: {
borderWidth: 1,
borderColor: '#ccc',
borderRadius: 8,
padding: 12,
fontSize: 16,
marginBottom: 30,
marginTop: 30,
},
primaryButton: {
backgroundColor: '#6200ee',
paddingVertical: 12,
paddingHorizontal: 20,
borderRadius: 8,
alignSelf: 'center',
},
scanButton: {
backgroundColor: '#6200ee',
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
paddingVertical: 12,
paddingHorizontal: 20,
borderRadius: 8,
marginVertical: 16,
},
buttonText: {
color: '#fff',
fontSize: 16,
},
message: {
textAlign: 'center',
paddingVertical: 20,
fontSize: 16,
},
result: {
padding: 16,
borderColor: '#ccc',
borderWidth: 1,
borderRadius: 10,
minHeight: 300,
justifyContent: 'center'
},
resultArtist: {
fontSize: 18,
},
resultTitle: {
fontSize: 18,
fontWeight: 'bold',
},
resultLabel: {
fontSize: 16,
marginTop: 4,
marginBottom: 8,
},
thumb: {
width: 120,
height: 120,
resizeMode: 'contain',
marginVertical: 10,
},
pickerTrigger: {
backgroundColor: '#eee',
padding: 12,
borderRadius: 8,
marginTop: 10,
},
pickerTriggerText: {
fontSize: 16,
},
pickerList: {
marginTop: 8,
},
pickerItem: {
paddingVertical: 10,
paddingHorizontal: 12,
borderBottomWidth: 1,
borderColor: '#ccc',
},
pickerItemSelected: {
backgroundColor: '#d1c4e9',
},
pickerItemText: {
fontSize: 16,
},
cameraHalf: {
flex: 1,
},
bottomHalf: {
padding: 16,
backgroundColor: 'white',
},
});