-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathBarcodeScanningScreen.tsx
More file actions
241 lines (212 loc) · 7.15 KB
/
Copy pathBarcodeScanningScreen.tsx
File metadata and controls
241 lines (212 loc) · 7.15 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
import React, { FC, useState, useEffect, useCallback } from "react"
import { observer } from "mobx-react-lite"
import { ViewStyle, View, ImageStyle, TextStyle, ScrollView, Pressable } from "react-native"
import { NativeStackScreenProps } from "@react-navigation/native-stack"
import { AppStackScreenProps } from "../navigators"
import { Text, Icon, ImageSelector, Screen } from "../components"
import { useTypedNavigation } from "../navigators/useTypedNavigation"
import RNMLKitBarcodeScanningModule, {
BarcodeFormat,
} from "@infinitered/react-native-mlkit-barcode-scanning"
import type {
BarcodeScannerOptions,
BarcodeScannerResult,
} from "@infinitered/react-native-mlkit-barcode-scanning"
import { UseExampleImageStatus, SelectedImage } from "../utils/useExampleImage"
type BarcodeScanningScreenProps = NativeStackScreenProps<AppStackScreenProps<"BarcodeScanning">>
function DebugOutput({ data }: { data: unknown }) {
const [expanded, setExpanded] = useState(false)
return (
<View style={$debugContainer}>
<Pressable onPress={() => setExpanded(!expanded)} style={$debugHeader}>
<Text style={$debugTitle}>{expanded ? "▼" : "▶"} Debug Output</Text>
</Pressable>
{expanded && (
<ScrollView style={$debugContent} horizontal>
<ScrollView nestedScrollEnabled>
<Text style={$debugText}>{JSON.stringify(data, null, 2)}</Text>
</ScrollView>
</ScrollView>
)}
</View>
)
}
const DEFAULT_OPTIONS: BarcodeScannerOptions = {
barcodeFormats: [BarcodeFormat.ALL],
enableAllPotentialBarcodes: false,
}
export const BarcodeScanningScreen: FC<BarcodeScanningScreenProps> = observer(
function BarcodeScanningScreen() {
const navigation = useTypedNavigation<"BarcodeScanning">()
const [image, setImage] = useState<SelectedImage | null>(null)
const handleImageChange = useCallback((nextImage: SelectedImage) => {
setImage(nextImage)
}, [])
const [result, setResult] = useState<BarcodeScannerResult | null>(null)
const [status, setStatus] = useState<
"init" | "noPermissions" | "done" | "error" | "loading" | UseExampleImageStatus
>("init")
const onStatusChange = React.useCallback(
(status: "init" | "noPermissions" | "done" | "error" | "loading" | UseExampleImageStatus) => {
setStatus(status)
},
[],
)
const [isScanning, setIsScanning] = useState(false)
useEffect(() => {
const scanBarcode = async () => {
if (!image?.uri) return
setIsScanning(true)
try {
await RNMLKitBarcodeScanningModule.initialize(DEFAULT_OPTIONS)
const scanResult = await RNMLKitBarcodeScanningModule.process(image.uri)
setResult(scanResult)
setStatus("done")
} catch (error) {
console.error("Error scanning barcode:", error)
setStatus("error")
} finally {
setIsScanning(false)
}
}
scanBarcode().then(() => null)
}, [image])
const statusMessage = React.useMemo(() => {
if (!image && status !== "init") {
setStatus("init")
}
if (isScanning) {
return "Scanning for barcodes..."
}
switch (status) {
case "init":
return "Take a photo or select one from your camera roll"
case "noPermissions":
return "You need to grant camera permissions to take a photo"
case "takingPhoto":
return "Taking photo..."
case "selectingPhoto":
return "Selecting photo..."
case "done":
return result?.barcodes.length
? `Found ${result.barcodes.length} barcode${result.barcodes.length > 1 ? "s" : ""}!`
: "No barcodes found"
case "error":
return "Error during scanning!"
case "loading":
return "Loading Example Images..."
default:
return ""
}
}, [result, image, status, isScanning])
const clearResults = useCallback(() => {
setResult(null)
}, [])
return (
<Screen style={$root} preset="scroll" safeAreaEdges={["top", "bottom"]}>
<View>
<Icon icon={"back"} onPress={() => navigation.navigate("Home")} style={$backIcon} />
<Text preset={"heading"} text="Barcode Scanning" />
<Text style={$description}>Take a photo of a barcode and scan it.</Text>
</View>
<ImageSelector
onImageChange={handleImageChange}
onImageClear={clearResults}
onStatusChange={onStatusChange}
statusMessage={statusMessage}
status={isScanning ? "loading" : status}
isLoading={false}
images={{
filter: "all",
groupBy: "label",
}}
/>
{result && result.barcodes.length > 0 && (
<>
<View style={$resultContainer}>
<Text preset="subheading">Scanned Barcodes</Text>
{result.barcodes.map((barcode, index) => (
<View key={index} style={$barcodeItem}>
<Text style={$barcodeLabel}>Format: {barcode.format}</Text>
<Text style={$barcodeLabel}>Type: {barcode.valueType}</Text>
{barcode.displayValue && (
<Text style={$barcodeValue}>Value: {barcode.displayValue}</Text>
)}
{barcode.rawValue && barcode.rawValue !== barcode.displayValue && (
<Text style={$barcodeRaw}>Raw: {barcode.rawValue}</Text>
)}
</View>
))}
</View>
<DebugOutput data={result} />
</>
)}
</Screen>
)
},
)
const $root: ViewStyle = {
flex: 1,
padding: 16,
display: "flex",
flexDirection: "column",
}
const $backIcon: ImageStyle = { marginVertical: 8 }
const $description: TextStyle = {
marginVertical: 8,
color: "rgba(0,0,0,0.6)",
}
const $resultContainer: ViewStyle = {
width: "100%",
borderWidth: 1,
borderColor: "rgba(0,0,0,0.2)",
borderRadius: 8,
padding: 12,
marginVertical: 16,
}
const $barcodeItem: ViewStyle = {
marginTop: 12,
paddingTop: 12,
borderTopWidth: 1,
borderTopColor: "rgba(0,0,0,0.1)",
}
const $barcodeLabel: TextStyle = {
fontSize: 12,
color: "rgba(0,0,0,0.6)",
marginBottom: 4,
}
const $barcodeValue: TextStyle = {
fontSize: 16,
fontWeight: "600",
marginTop: 4,
}
const $barcodeRaw: TextStyle = {
fontSize: 12,
fontFamily: "monospace",
marginTop: 4,
color: "rgba(0,0,0,0.5)",
}
const $debugContainer: ViewStyle = {
width: "100%",
borderWidth: 1,
borderColor: "rgba(0,0,0,0.2)",
borderRadius: 8,
marginBottom: 24,
overflow: "hidden",
}
const $debugHeader: ViewStyle = {
padding: 12,
backgroundColor: "rgba(0,0,0,0.05)",
}
const $debugTitle: TextStyle = {
fontWeight: "bold",
}
const $debugContent: ViewStyle = {
maxHeight: 300,
padding: 12,
backgroundColor: "rgba(0,0,0,0.02)",
}
const $debugText: TextStyle = {
fontFamily: "monospace",
fontSize: 12,
}