-
Notifications
You must be signed in to change notification settings - Fork 19
Fix double camera permission request on OS back and refactor qr scanner #655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
pepina-dev
wants to merge
4
commits into
master
Choose a base branch
from
refactor/qr-scanner
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c44eb83
fix: add qr scanner hook with fixed os back case on camera access per…
pepina-dev 79a3da3
refactor: use new hook in qr scanner widget and rename barcode to qrcode
pepina-dev cf3c882
refactor: use renamed qr code method in scan npub screen
pepina-dev 838992f
docs: update changelog
pepina-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| import 'dart:async'; | ||
|
|
||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter_hooks/flutter_hooks.dart'; | ||
| import 'package:mobile_scanner/mobile_scanner.dart'; | ||
| import 'package:permission_handler/permission_handler.dart'; | ||
|
|
||
| class _Injectable<T extends Function> { | ||
| final T _default; | ||
| late T _current; | ||
| _Injectable(this._default) : _current = _default; | ||
| T get value => _current; | ||
| void set(T v) => _current = v; | ||
| void reset() => _current = _default; | ||
| } | ||
|
|
||
| final _controllerFactory = _Injectable<MobileScannerController Function()>( | ||
| () => MobileScannerController( | ||
| formats: [BarcodeFormat.qrCode], | ||
| autoStart: false, | ||
| ), | ||
| ); | ||
| final _permissionRequester = _Injectable<Future<PermissionStatus> Function()>( | ||
| () => Permission.camera.request(), | ||
| ); | ||
| final _permissionStatusChecker = _Injectable<Future<PermissionStatus> Function()>( | ||
| () => Permission.camera.status, | ||
| ); | ||
|
|
||
| MobileScannerController createScannerController() => _controllerFactory.value(); | ||
| Future<PermissionStatus> requestCameraPermission() => _permissionRequester.value(); | ||
| Future<PermissionStatus> checkCameraPermissionStatus() => _permissionStatusChecker.value(); | ||
|
|
||
| void setScannerControllerFactory(MobileScannerController Function() factory) => | ||
| _controllerFactory.set(factory); | ||
| void resetScannerControllerFactory() => _controllerFactory.reset(); | ||
| void setPermissionRequester(Future<PermissionStatus> Function() requester) => | ||
| _permissionRequester.set(requester); | ||
| void resetPermissionRequester() => _permissionRequester.reset(); | ||
| void setPermissionStatusChecker(Future<PermissionStatus> Function() checker) => | ||
| _permissionStatusChecker.set(checker); | ||
| void resetPermissionStatusChecker() => _permissionStatusChecker.reset(); | ||
|
|
||
| enum ScannerState { loading, ready, permissionDenied, initError, error } | ||
|
|
||
| void _useCameraPermission({ | ||
| required UniqueKey retryKey, | ||
| required ObjectRef<bool> isMounted, | ||
| required VoidCallback onGranted, | ||
| required VoidCallback onDenied, | ||
| required VoidCallback retryScanner, | ||
| }) { | ||
| final isDenied = useRef(false); | ||
| final permissionStatusAtDenial = useRef<PermissionStatus?>(null); | ||
|
|
||
| useEffect(() { | ||
| Future<void> checkPermission() async { | ||
| final status = await requestCameraPermission(); | ||
| if (!isMounted.value) return; | ||
|
|
||
| if (status.isGranted || status.isLimited) { | ||
| isDenied.value = false; | ||
| permissionStatusAtDenial.value = null; | ||
| onGranted(); | ||
| } else { | ||
| final currentStatus = await checkCameraPermissionStatus(); | ||
| if (!isMounted.value) return; | ||
| isDenied.value = true; | ||
| permissionStatusAtDenial.value = currentStatus; | ||
| onDenied(); | ||
| } | ||
| } | ||
|
|
||
| unawaited(checkPermission()); | ||
| return null; | ||
| }, [retryKey]); | ||
|
|
||
| useOnAppLifecycleStateChange((previous, current) { | ||
| if (current != AppLifecycleState.resumed || !isMounted.value) return; | ||
| if (!isDenied.value) return; | ||
|
|
||
| Future<void> checkAndRetry() async { | ||
| final cameraPermissionStatus = await checkCameraPermissionStatus(); | ||
| if (!isMounted.value) return; | ||
| final snapshotWasDenied = | ||
| permissionStatusAtDenial.value != null && | ||
| !(permissionStatusAtDenial.value!.isGranted || permissionStatusAtDenial.value!.isLimited); | ||
| if (snapshotWasDenied && | ||
| (cameraPermissionStatus.isGranted || cameraPermissionStatus.isLimited)) { | ||
| retryScanner(); | ||
| } | ||
| } | ||
|
|
||
| unawaited(checkAndRetry()); | ||
| }); | ||
| } | ||
|
|
||
| ({ | ||
| ScannerState scannerState, | ||
| MobileScannerController controller, | ||
| VoidCallback retryScanner, | ||
| void Function(ScannerState) setErrorState, | ||
| }) | ||
| useQrScanner({required void Function(String) onQrCodeDetected}) { | ||
| final isProcessing = useState(false); | ||
| final scannerRetryKey = useState(UniqueKey()); | ||
| final isMounted = useRef(true); | ||
| final scannerState = useState(ScannerState.loading); | ||
|
|
||
| useEffect(() { | ||
| isMounted.value = true; | ||
| return () => isMounted.value = false; | ||
| }, const []); | ||
|
|
||
| void retryScanner() { | ||
| scannerState.value = ScannerState.loading; | ||
| scannerRetryKey.value = UniqueKey(); | ||
| } | ||
|
|
||
| _useCameraPermission( | ||
| retryKey: scannerRetryKey.value, | ||
| isMounted: isMounted, | ||
| onGranted: () => scannerState.value = ScannerState.ready, | ||
| onDenied: () => scannerState.value = ScannerState.permissionDenied, | ||
| retryScanner: retryScanner, | ||
| ); | ||
|
|
||
| final controller = useMemoized(createScannerController, [ | ||
| scannerRetryKey.value, | ||
| ]); | ||
|
|
||
| useEffect(() { | ||
| if (scannerState.value != ScannerState.ready) return null; | ||
|
|
||
| var startCalled = false; | ||
|
|
||
| void handleQrCode(BarcodeCapture capture) { | ||
| if (capture.barcodes.isEmpty) return; | ||
| if (isProcessing.value) return; | ||
| final barcode = capture.barcodes.first; | ||
| final rawValue = barcode.rawValue ?? ''; | ||
| if (rawValue.isEmpty) return; | ||
| isProcessing.value = true; | ||
| onQrCodeDetected(rawValue.trim()); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| Future.delayed(const Duration(milliseconds: 500), () { | ||
| if (isMounted.value) isProcessing.value = false; | ||
| }); | ||
| } | ||
|
|
||
| Future<void> startController() async { | ||
| if (startCalled) return; | ||
| startCalled = true; | ||
| try { | ||
| await controller.start(); | ||
| } on MobileScannerException { | ||
| if (isMounted.value) scannerState.value = ScannerState.initError; | ||
| } | ||
| } | ||
|
|
||
| final subscription = controller.barcodes.listen(handleQrCode); | ||
| unawaited(startController()); | ||
|
|
||
| return () { | ||
| unawaited(subscription.cancel()); | ||
| unawaited(controller.dispose()); | ||
| }; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }, [controller, scannerState.value]); | ||
|
|
||
| useOnAppLifecycleStateChange((previous, current) { | ||
| if (current != AppLifecycleState.resumed || !isMounted.value) return; | ||
| if (scannerState.value == ScannerState.permissionDenied) return; | ||
|
|
||
| if (scannerState.value == ScannerState.loading) { | ||
| unawaited(() async { | ||
| final status = await checkCameraPermissionStatus(); | ||
| if (!isMounted.value) return; | ||
| if (status.isGranted || status.isLimited) { | ||
| retryScanner(); | ||
| } | ||
| }()); | ||
| return; | ||
| } | ||
|
|
||
| retryScanner(); | ||
| }); | ||
|
|
||
| void setErrorState(ScannerState state) { | ||
| if (isMounted.value) scannerState.value = state; | ||
| } | ||
|
|
||
| return ( | ||
| scannerState: scannerState.value, | ||
| controller: controller, | ||
| retryScanner: retryScanner, | ||
| setErrorState: setErrorState, | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.