Skip to content

Commit

Permalink
fix: Fixed some remaining problems with QR code scanning and URI pars…
Browse files Browse the repository at this point in the history
…ing.
  • Loading branch information
Skyost committed Jul 11, 2024
1 parent 48cba96 commit be3a739
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
3 changes: 2 additions & 1 deletion lib/i18n/en/totp.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"validity": "Validity (in seconds)",
"advancedOptions": "Advanced options",
"showQrCode": "Show QR code",
"save": "Save"
"save": "Save",
"uriError": "Unable to read this TOTP. Are you it is valid ?"
}
}
3 changes: 2 additions & 1 deletion lib/i18n/fr/totp.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"validity": "Validité (en secondes)",
"advancedOptions": "Options avancées",
"showQrCode": "Afficher le QR code",
"save": "Enregistrer"
"save": "Enregistrer",
"uriError": "Impossible de lire ce TOTP. Êtes-vous sûr qu'il est valide ?"
}
}
1 change: 1 addition & 0 deletions lib/pages/scan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ScanPage extends ConsumerWidget {
}
Uri? uri = Uri.tryParse(code);
if (uri == null) {
Navigator.pop(context);
SnackBarIcon.showErrorSnackBar(context, text: translations.error.scan.noUri);
return;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/pages/totp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ class TotpPage extends ConsumerStatefulWidget {
return;
}
if (totp == null) {
SnackBarIcon.showErrorSnackBar(context, text: translations.error.generic.withException(exception: Exception('Failed to decrypt TOTP.')));
return;
SnackBarIcon.showErrorSnackBar(context, text: translations.totp.page.uriError);
}
Navigator.pushNamedAndRemoveUntil(
context,
Expand Down
44 changes: 22 additions & 22 deletions lib/widgets/code_scan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class _CodeScannerState extends State<CodeScanner> with WidgetsBindingObserver {
/// The camera listener.
CodeScannerCameraListener? listener;

/// Whether to retry.
/// Whether to retry for when camera permission is denied.
bool retry = true;

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

if (state == AppLifecycleState.inactive) {
CameraController? cameraController = controller;
if (cameraController == null || !cameraController.value.isInitialized) {
return;
}
cameraController.dispose();
if (mounted) {
setState(() => controller = null);
}
} else if (state == AppLifecycleState.resumed && retry && initialized && controller == null) {
initialized = false;
controller!.dispose();
} else if (state == AppLifecycleState.resumed) {
_initCameraController();
}
}
Expand Down Expand Up @@ -267,11 +259,26 @@ class _CodeScannerState extends State<CodeScanner> with WidgetsBindingObserver {
}
listener = CodeScannerCameraListener(
this.controller!,
onScan: widget.onScan,
onScanAll: widget.onScanAll,
onScan: (code, details, listener) async {
widget.onScan?.call(code, details, listener);
if (widget.once) {
setState(() {
this.controller = null;
listener.stop();
});
}
},
onScanAll: (barcodes, listener) async {
widget.onScanAll?.call(barcodes, listener);
if (widget.once) {
setState(() {
this.controller = null;
listener.stop();
});
}
},
formats: widget.formats,
interval: widget.scanInterval,
once: widget.once,
);

initialized = true;
Expand Down Expand Up @@ -360,9 +367,6 @@ class CodeScannerCameraListener {
/// The scanner instance.
final BarcodeScanner scanner;

/// Whether there is only one asked scan.
final bool once;

/// Called when a scan occurs.
final void Function(String? code, Barcode details, CodeScannerCameraListener listener)? onScan;

Expand All @@ -380,7 +384,6 @@ class CodeScannerCameraListener {
this.controller, {
List<BarcodeFormat> formats = const [BarcodeFormat.all],
Duration interval = const Duration(milliseconds: 500),
this.once = false,
this.onScan,
this.onScanAll,
this.onError,
Expand Down Expand Up @@ -454,9 +457,6 @@ class CodeScannerCameraListener {
if (!controller.value.isStreamingImages || barcodes.isEmpty) {
return;
}
if (once) {
await stop();
}
onScan?.call(barcodes.first.rawValue, barcodes.first, this);
onScanAll?.call(barcodes, this);
}
Expand Down

0 comments on commit be3a739

Please sign in to comment.