Skip to content

Commit be3a739

Browse files
committed
fix: Fixed some remaining problems with QR code scanning and URI parsing.
1 parent 48cba96 commit be3a739

File tree

5 files changed

+28
-26
lines changed

5 files changed

+28
-26
lines changed

lib/i18n/en/totp.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"validity": "Validity (in seconds)",
6666
"advancedOptions": "Advanced options",
6767
"showQrCode": "Show QR code",
68-
"save": "Save"
68+
"save": "Save",
69+
"uriError": "Unable to read this TOTP. Are you it is valid ?"
6970
}
7071
}

lib/i18n/fr/totp.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"validity": "Validité (en secondes)",
6666
"advancedOptions": "Options avancées",
6767
"showQrCode": "Afficher le QR code",
68-
"save": "Enregistrer"
68+
"save": "Enregistrer",
69+
"uriError": "Impossible de lire ce TOTP. Êtes-vous sûr qu'il est valide ?"
6970
}
7071
}

lib/pages/scan.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class ScanPage extends ConsumerWidget {
2929
}
3030
Uri? uri = Uri.tryParse(code);
3131
if (uri == null) {
32+
Navigator.pop(context);
3233
SnackBarIcon.showErrorSnackBar(context, text: translations.error.scan.noUri);
3334
return;
3435
}

lib/pages/totp.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ class TotpPage extends ConsumerStatefulWidget {
5757
return;
5858
}
5959
if (totp == null) {
60-
SnackBarIcon.showErrorSnackBar(context, text: translations.error.generic.withException(exception: Exception('Failed to decrypt TOTP.')));
61-
return;
60+
SnackBarIcon.showErrorSnackBar(context, text: translations.totp.page.uriError);
6261
}
6362
Navigator.pushNamedAndRemoveUntil(
6463
context,

lib/widgets/code_scan.dart

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class _CodeScannerState extends State<CodeScanner> with WidgetsBindingObserver {
184184
/// The camera listener.
185185
CodeScannerCameraListener? listener;
186186

187-
/// Whether to retry.
187+
/// Whether to retry for when camera permission is denied.
188188
bool retry = true;
189189

190190
/// Whether it's initialized.
@@ -209,21 +209,13 @@ class _CodeScannerState extends State<CodeScanner> with WidgetsBindingObserver {
209209
@override
210210
void didChangeAppLifecycleState(AppLifecycleState state) {
211211
super.didChangeAppLifecycleState(state);
212-
if (!isInternalController) {
212+
if (!isInternalController || controller == null || !controller!.value.isInitialized) {
213213
return;
214214
}
215215

216216
if (state == AppLifecycleState.inactive) {
217-
CameraController? cameraController = controller;
218-
if (cameraController == null || !cameraController.value.isInitialized) {
219-
return;
220-
}
221-
cameraController.dispose();
222-
if (mounted) {
223-
setState(() => controller = null);
224-
}
225-
} else if (state == AppLifecycleState.resumed && retry && initialized && controller == null) {
226-
initialized = false;
217+
controller!.dispose();
218+
} else if (state == AppLifecycleState.resumed) {
227219
_initCameraController();
228220
}
229221
}
@@ -267,11 +259,26 @@ class _CodeScannerState extends State<CodeScanner> with WidgetsBindingObserver {
267259
}
268260
listener = CodeScannerCameraListener(
269261
this.controller!,
270-
onScan: widget.onScan,
271-
onScanAll: widget.onScanAll,
262+
onScan: (code, details, listener) async {
263+
widget.onScan?.call(code, details, listener);
264+
if (widget.once) {
265+
setState(() {
266+
this.controller = null;
267+
listener.stop();
268+
});
269+
}
270+
},
271+
onScanAll: (barcodes, listener) async {
272+
widget.onScanAll?.call(barcodes, listener);
273+
if (widget.once) {
274+
setState(() {
275+
this.controller = null;
276+
listener.stop();
277+
});
278+
}
279+
},
272280
formats: widget.formats,
273281
interval: widget.scanInterval,
274-
once: widget.once,
275282
);
276283

277284
initialized = true;
@@ -360,9 +367,6 @@ class CodeScannerCameraListener {
360367
/// The scanner instance.
361368
final BarcodeScanner scanner;
362369

363-
/// Whether there is only one asked scan.
364-
final bool once;
365-
366370
/// Called when a scan occurs.
367371
final void Function(String? code, Barcode details, CodeScannerCameraListener listener)? onScan;
368372

@@ -380,7 +384,6 @@ class CodeScannerCameraListener {
380384
this.controller, {
381385
List<BarcodeFormat> formats = const [BarcodeFormat.all],
382386
Duration interval = const Duration(milliseconds: 500),
383-
this.once = false,
384387
this.onScan,
385388
this.onScanAll,
386389
this.onError,
@@ -454,9 +457,6 @@ class CodeScannerCameraListener {
454457
if (!controller.value.isStreamingImages || barcodes.isEmpty) {
455458
return;
456459
}
457-
if (once) {
458-
await stop();
459-
}
460460
onScan?.call(barcodes.first.rawValue, barcodes.first, this);
461461
onScanAll?.call(barcodes, this);
462462
}

0 commit comments

Comments
 (0)