-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
156 lines (130 loc) · 4.34 KB
/
index.tsx
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
import {
requireNativeComponent,
UIManager,
Platform,
NativeModules,
type ViewStyle, Dimensions, LayoutRectangle, NativeModule, findNodeHandle
} from "react-native"
import React, { Ref, useEffect, useImperativeHandle, useRef, useState, forwardRef } from "react"
import { MapMarker } from "@models/map/MapMarker"
import MapboxGL, { CameraBoundsWithPadding } from "@rnmapbox/maps"
import { Text } from "@components/Text"
import { COLORS } from "@styles/color"
import Polyline from "@mapbox/polyline"
import { MapRefType, MapView } from "@components/Map"
const LINKING_ERROR =
`The package 'react-native-genius-scan' doesn't seem to be linked. Make sure: \n\n` +
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
'- You rebuilt the app after installing the package\n' +
'- You are not using Expo Go\n';
export type GeniusScanOnEnableTakePictureEvent = {
enable: boolean;
};
export type GeniusScanOnImageCapturedEvent = {
imageBase64: string;
};
type GeniusScanProps = {
onEnableTakePicture: (enable: boolean) => void;
onImageCaptured: (imagePath: string) => void;
style: ViewStyle;
};
const ComponentName = 'GeniusScanView';
const RNGeniusScanView =
UIManager.getViewManagerConfig(ComponentName) != null
? requireNativeComponent<GeniusScanProps>(ComponentName)
: () => {
throw new Error(LINKING_ERROR);
};
export interface GeniusScanViewRefType {
takePicture: () => void
}
export const GeniusScanner = forwardRef(
(props: GeniusScanProps,
ref: Ref<GeniusScanViewRefType>,
): JSX.Element => {
// Refs
const scanRef = useRef(null)
const [enableTakePicture, setEnableTakePicture] = useState(false)
const [debouncedEnableTakePicture, setDebouncedEnableTakePicture] = useState(false)
// useEffect(
// () => {
// // Update debounced value after delay
// let handler: string | number | NodeJS.Timeout | undefined
// if (enableTakePicture) {
// setDebouncedEnableTakePicture(enableTakePicture)
// } else {
// handler = setTimeout(() => {
// setDebouncedEnableTakePicture(enableTakePicture)
// }, 500)
// }
//
// // Cancel the timeout if value changes (also on delay change or unmount)
// // This is how we prevent debounced value from updating if value is changed ...
// // .. within the delay period. Timeout gets cleared and restarted.
// return () => {
// clearTimeout(handler)
// }
// },
// [enableTakePicture], // Only re-call effect if value or delay changes
// )
useEffect(() => {
props.onEnableTakePicture(enableTakePicture)
}, [enableTakePicture])
useImperativeHandle(ref, () => ({ takePicture }))
const _onEnableTakePicture = (event: { nativeEvent: GeniusScanOnEnableTakePictureEvent }) => {
if (!props.onEnableTakePicture) {
return;
}
// console.log(`Detected ${event.nativeEvent.enable}`)
// process raw event...
setEnableTakePicture(event.nativeEvent.enable)
};
const _onImageCaptured = (event: { nativeEvent: GeniusScanOnImageCapturedEvent }) => {
if (!props.onImageCaptured) {
return;
}
// process raw event...
props.onImageCaptured(event.nativeEvent.imagePath);
};
// Exposed public functions
const takePicture = (): void => {
NativeModules.GeniusScanView.takePicture(findNodeHandle(scanRef.current))
}
return (
<RNGeniusScanView
ref={scanRef} {...props}
// @ts-ignore
onEnableTakePicture={_onEnableTakePicture}
// @ts-ignore
onImageCaptured={_onImageCaptured}
/>
)
},
)
export const GeniusScanModule = NativeModules.RNGeniusScan
? NativeModules.RNGeniusScan
: new Proxy(
{},
{
get() {
throw new Error(LINKING_ERROR);
},
}
);
export type GeniusScanSingleScan = {
enhancedUrl: string;
originalUrl: string;
};
export type GeniusScanResult = {
multiPageDocumentUrl: string;
pdfUrl: string;
scans: GeniusScanSingleScan[];
};
export function scanWithConfiguration(
scanOptions: Record<string, unknown>
): Promise<GeniusScanResult> {
return GeniusScanModule.scanWithConfiguration(scanOptions);
}
export function setLicenceKey(apiKey: string): Promise<void> {
return GeniusScanModule.setLicenceKey(apiKey);
}