-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
113 lines (99 loc) · 3.22 KB
/
index.js
File metadata and controls
113 lines (99 loc) · 3.22 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
import React from 'react'
import {
View,
requireNativeComponent,
NativeModules,
Platform,
Dimensions,
StatusBar,
findNodeHandle
} from 'react-native'
type
EventCallbackArgumentsType = {
nativeEvent: Object,
};
const NativeBarCode = requireNativeComponent('RNScanCode', RNScanCode)
export const ScanCodeModule = NativeModules.ScanCodeModule || NativeModules.RNScanCodeManager
const EventThrottleMs = 500;
let _this = null
export class RNScanCode extends React.Component {
constructor(props) {
super(props)
this._lastEvents = {};
this._lastEventsTimes = {};
_this = this
const {width, height} = Dimensions.get('window')
let _height = height + (Platform.OS === 'ios' ? 0 : StatusBar.currentHeight)
this.state = {
surfaceWidth: width,
surfaceHeight: _height
};
}
static Constants = {
CodeType: ScanCodeModule.CodeType
};
_lastEvents: { [string]: string };
_lastEventsTimes: { [string]: Date };
// 存储组件实例的tag值
_scancodeHandle: ?number = 0;
_onObjectDetected = (callback: ?Function) => ({nativeEvent}: EventCallbackArgumentsType) => {
const {type} = nativeEvent;
if (
this._lastEvents[type] &&
this._lastEventsTimes[type] &&
JSON.stringify(nativeEvent) === this._lastEvents[type] &&
new Date() - this._lastEventsTimes[type] < EventThrottleMs
) {
return;
}
if (callback) {
callback(nativeEvent);
this._lastEventsTimes[type] = new Date();
this._lastEvents[type] = JSON.stringify(nativeEvent);
}
};
/** 查找对应组件实例的tag值 */
_setReference = (ref: ?Object) => {
if (ref) {
this._scancodeHandle = findNodeHandle(ref);
} else {
this._scancodeHandle = null;
}
}
/** 设置手电筒 */
static setFlashlight(flash) {
if (Platform.OS === 'ios') {
ScanCodeModule.setFlashlight(_this._scancodeHandle, flash)
} else {
ScanCodeModule.setFlashlight(flash)
}
}
render() {
const {
children,
style,
onLightBright,
onBarCodeRead,
codeTypes = Object.values(ScanCodeModule.CodeType),
...otherProps
} = this.props
const {surfaceWidth, surfaceHeight} = this.state
return (
<View style={{flex: 1, backgroundColor: 'green'}} onLayout={event => {
this.setState({surfaceHeight: event.nativeEvent.layout.height})
}}>
<NativeBarCode
style={{width: surfaceWidth, height: surfaceHeight}}
{...otherProps}
codeTypes={codeTypes}
onBarCodeRead={this._onObjectDetected(onBarCodeRead)}
onLightBright={this._onObjectDetected(onLightBright)}
ref={this._setReference}
/>
<View style={[{position: 'absolute', top: 0, left: 0}, style]} pointerEvents="box-none">
{children}
</View>
</View>
)
}
}