Skip to content

Commit 0e28bcc

Browse files
committed
fix(scanner): fix fix repeated scanning bug
now it actually scans again when you navigate back and also if you fail the first time
1 parent 64a3111 commit 0e28bcc

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

src/ui/interaction/component/scanner.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ interface Props {
9191
isTorchPressed: boolean
9292
onPressTorch: (state: boolean) => void
9393
reRenderKey: number
94+
onScannerRef: (s: any) => void
9495
isError: boolean
9596
colorAnimationValue: Animated.Value
9697
textAnimationValue: Animated.Value
@@ -103,6 +104,7 @@ export const ScannerComponent = (props: Props) => {
103104
isTorchPressed,
104105
onPressTorch,
105106
reRenderKey,
107+
onScannerRef,
106108
colorAnimationValue,
107109
textAnimationValue,
108110
} = props
@@ -123,6 +125,7 @@ export const ScannerComponent = (props: Props) => {
123125
return (
124126
<React.Fragment>
125127
<QRScanner
128+
ref={onScannerRef}
126129
//@ts-ignore - see https://github.com/DefinitelyTyped/DefinitelyTyped/issues/29651
127130
containerStyle={{
128131
position: 'absolute',
@@ -140,8 +143,7 @@ export const ScannerComponent = (props: Props) => {
140143
<View
141144
style={{
142145
flexDirection: 'row',
143-
}}
144-
>
146+
}}>
145147
<View style={styles.horizontalOverlay} />
146148
<Animated.View
147149
style={[
@@ -165,8 +167,7 @@ export const ScannerComponent = (props: Props) => {
165167
{
166168
opacity: textAnimationValue,
167169
},
168-
]}
169-
>
170+
]}>
170171
{I18n.t(strings.LOOKS_LIKE_WE_CANT_PROVIDE_THIS_SERVICE)}
171172
</Animated.Text>
172173
) : (
@@ -181,8 +182,7 @@ export const ScannerComponent = (props: Props) => {
181182
onPressOut={() => onPressTorch(false)}
182183
activeOpacity={1}
183184
underlayColor={'transparent'}
184-
style={styles.torch}
185-
>
185+
style={styles.torch}>
186186
{isTorchPressed ? <TorchOnIcon /> : <TorchOffIcon />}
187187
</TouchableHighlight>
188188
</View>

src/ui/interaction/container/interactionScreen.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ interface Props
5757

5858
const InteractionContainer = (props: Props) => {
5959
return (
60-
<Wrapper dark centered withoutSafeArea>
60+
<Wrapper dark centered withoutSafeArea withoutStatusBar>
6161
{IS_IOS && (
6262
<TouchableOpacity
6363
onPress={props.navigateHome}
64-
style={styles.closeButton}>
64+
style={styles.closeButton}
65+
>
6566
<CloseIcon />
6667
</TouchableOpacity>
6768
)}

src/ui/interaction/container/scanner.tsx

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,39 @@ export const ScannerContainer: React.FC<Props> = (props) => {
3939
const [isError, setError] = useState(false)
4040
const [colorAnimationValue] = useState(new Animated.Value(0))
4141
const [textAnimationValue] = useState(new Animated.Value(0))
42+
const [scannerRef, setScannerRef] = useState(null)
43+
44+
// NOTE: this is needed because QRScanner behaves weirdly when the screen is
45+
// remounted....
46+
if (scannerRef) scannerRef.reactivate()
47+
48+
const rerender = () => {
49+
setRenderKey(Date.now())
50+
if (scannerRef) scannerRef.reactivate()
51+
}
4252

4353
useEffect(() => {
44-
let focusListener!: NavigationEventSubscription
54+
let listeners: NavigationEventSubscription[] = []
4555
if (navigation) {
46-
focusListener = navigation.addListener('willFocus', () => {
47-
// NOTE: the re-render and the re-mount should only fire during the willFocus event
48-
setRenderKey(Date.now())
49-
})
56+
listeners.push(navigation.addListener('didFocus', () => {
57+
rerender()
58+
checkCameraPermissions()
59+
}))
60+
} else {
61+
checkCameraPermissions()
5062
}
5163

52-
check(CAMERA_PERMISSION).then(perm => {
64+
return () => listeners.forEach(l => l.remove())
65+
}, [])
66+
67+
const checkCameraPermissions = async () => {
68+
return check(CAMERA_PERMISSION).then(perm => {
5369
setPermission(perm)
5470
if (perm !== RESULTS.GRANTED && perm !== RESULTS.BLOCKED) {
5571
requestCameraPermission()
5672
}
5773
})
58-
59-
return focusListener.remove
60-
}, [])
74+
}
6175

6276
const requestCameraPermission = async () => {
6377
const permission = await request(CAMERA_PERMISSION)
@@ -115,7 +129,7 @@ export const ScannerContainer: React.FC<Props> = (props) => {
115129
}),
116130
])
117131

118-
const parseJWT = (jwt: string) => {
132+
const onScan = (jwt: string) => {
119133
try {
120134
const interactionToken = JolocomLib.parse.interactionToken.fromJWT(jwt)
121135
onScannerSuccess(interactionToken)
@@ -124,6 +138,7 @@ export const ScannerContainer: React.FC<Props> = (props) => {
124138
setError(true)
125139
Animated.parallel([animateColor(), animateText()]).start(() => {
126140
setError(false)
141+
rerender()
127142
})
128143
}
129144
}
@@ -133,7 +148,8 @@ export const ScannerContainer: React.FC<Props> = (props) => {
133148
return (
134149
<ScannerComponent
135150
reRenderKey={reRenderKey}
136-
onScan={parseJWT}
151+
onScan={onScan}
152+
onScannerRef={r => setScannerRef(r)}
137153
isTorchPressed={isTorch}
138154
onPressTorch={(state: boolean) => setTorch(state)}
139155
isError={isError}
@@ -142,6 +158,7 @@ export const ScannerContainer: React.FC<Props> = (props) => {
142158
/>
143159
)
144160
} else if (permission === RESULTS.UNAVAILABLE) {
161+
// TODO: maybe add a message here like "do you even camera?"
145162
return (
146163
<View
147164
style={{

0 commit comments

Comments
 (0)