Skip to content

Commit

Permalink
fix: camera black screen UI
Browse files Browse the repository at this point in the history
  • Loading branch information
jimcase committed Feb 25, 2025
1 parent ae2bc08 commit 9a28a01
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 42 deletions.
6 changes: 6 additions & 0 deletions src/ui/components/Scanner/Scanner.scss
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ ion-grid.qr-code-scanner {
background: rgba(var(--ion-color-neutral-700-rgb), 0.2);
}

&.transitioning {
.scanner-spinner-container {
background: #000000;
}
}

.page-footer .secondary-button {
opacity: 0.5;
backdrop-filter: blur(0.33rem);
Expand Down
111 changes: 69 additions & 42 deletions src/ui/components/Scanner/Scanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ const Scanner = forwardRef(
const [groupIdentifierOpen, setGroupIdentifierOpen] = useState(false);
const [resumeMultiSig, setResumeMultiSig] =
useState<IdentifierShortDetails | null>(null);
const [isTransitioning, setIsTransitioning] = useState(false);
const isHandlingQR = useRef(false);
const scannerState = useRef<"stopped" | "starting" | "running">("stopped");

useEffect(() => {
if (platforms.includes("mobileweb")) {
Expand All @@ -125,11 +127,16 @@ const Scanner = forwardRef(
};

const stopScan = async () => {
if (permission) {
await BarcodeScanner.stopScan();
await BarcodeScanner.removeAllListeners();
if (!permission || !Capacitor.isNativePlatform()) {
setScanning(false);
document?.querySelector("body")?.classList.remove("scanner-active");
scannerState.current = "stopped";
return;
}

await BarcodeScanner.stopScan();
await BarcodeScanner.removeAllListeners();
scannerState.current = "stopped";
setScanning(false);
document?.querySelector("body")?.classList.remove("scanner-active");
setGroupId("");
Expand All @@ -139,6 +146,26 @@ const Scanner = forwardRef(
stopScan,
}));

useEffect(() => {
const handleCameraChange = async () => {
if (
!scanning ||
!Capacitor.isNativePlatform() ||
isTransitioning ||
scannerState.current === "starting"
)
return;

setIsTransitioning(true);
await stopScan();

await initScan();
setIsTransitioning(false);
};

handleCameraChange();
}, [cameraDirection]);

const handleConnectWallet = (id: string) => {
if (/^b[1-9A-HJ-NP-Za-km-z]{33}/.test(id)) {
dispatch(setToastMsg(ToastMsgType.PEER_ID_SUCCESS));
Expand Down Expand Up @@ -418,47 +445,42 @@ const Scanner = forwardRef(
};

const initScan = async () => {
if (Capacitor.isNativePlatform()) {
const allowed = await checkPermission();
setPermisson(!!allowed);
onCheckPermissionFinish?.(!!allowed);

if (allowed) {
await BarcodeScanner.removeAllListeners();
const listener = await BarcodeScanner.addListener(
"barcodesScanned",
async (result) => {
await listener.remove();
if (isHandlingQR.current) return;
isHandlingQR.current = true;
if (!result.barcodes?.length) return;
await processValue(result.barcodes[0].rawValue);
}
);
const listenerError = await BarcodeScanner.addListener(
"scanError",
async (result) => {
await listenerError.remove();
}
);
try {
await BarcodeScanner.startScan({
formats: [BarcodeFormat.QrCode],
lensFacing: cameraDirection,
});
} catch (error) {
showError("Error starting barcode scan:", error);
setScanUnavailable(true);
stopScan();
if (!Capacitor.isNativePlatform() || mobileweb) return;

const allowed = await checkPermission();
setPermisson(!!allowed);
onCheckPermissionFinish?.(!!allowed);

if (!allowed) return;

if (scannerState.current !== "stopped") {
await stopScan();
}

try {
scannerState.current = "starting";
const listener = await BarcodeScanner.addListener(
"barcodesScanned",
async (result) => {
await listener.remove();
if (isHandlingQR.current || !result.barcodes?.length) return;
isHandlingQR.current = true;
await processValue(result.barcodes[0].rawValue);
}
}
);

document?.querySelector("body")?.classList.add("scanner-active");
await BarcodeScanner.startScan({
formats: [BarcodeFormat.QrCode],
lensFacing: cameraDirection,
});

scannerState.current = "running";
setScanning(true);
document?.querySelector("body")?.classList.add("scanner-active");
document
?.querySelector("body.scanner-active > div:last-child")
?.classList.remove("hide");
} catch (error) {
setScanUnavailable(true);
await stopScan();
showError("Error starting barcode scan:", error, dispatch);
}
};

Expand Down Expand Up @@ -507,7 +529,6 @@ const Scanner = forwardRef(
}, [
currentOperation,
routePath,
cameraDirection,
isShowConnectionsModal,
createIdentifierModalIsOpen,
currentToastMsgs,
Expand Down Expand Up @@ -594,6 +615,7 @@ const Scanner = forwardRef(
const containerClass = combineClassNames("qr-code-scanner", {
"no-permission": !permission || mobileweb,
"scan-unavailable": scanUnavailable,
transitioning: isTransitioning,
});

return (
Expand All @@ -602,7 +624,12 @@ const Scanner = forwardRef(
className={containerClass}
data-testid="qr-code-scanner"
>
{scanning || mobileweb || scanUnavailable ? (
{isTransitioning ? (
<div
className="scanner-spinner-container"
data-testid="scanner-spinner-container"
/>
) : scanning || mobileweb || scanUnavailable ? (
<>
<IonRow>
<IonCol size="12">
Expand Down

0 comments on commit 9a28a01

Please sign in to comment.