diff --git a/app/(features)/(cards)/qrcode.tsx b/app/(features)/(cards)/qrcode.tsx index 84763ff93..bc03cbfda 100644 --- a/app/(features)/(cards)/qrcode.tsx +++ b/app/(features)/(cards)/qrcode.tsx @@ -19,6 +19,7 @@ import OnboardingBackButton from "@/components/onboarding/OnboardingBackButton"; import { Services } from "@/stores/account/types"; import Stack from "@/ui/components/Stack"; import Typography from "@/ui/components/Typography"; +import { useMaxBrightness } from "@/hooks/useMaxBrightness"; export default function QRCodePage() { @@ -35,6 +36,8 @@ export default function QRCodePage() { const finalTranslation = Dimensions.get("window").height / 2; + useMaxBrightness(); + const panGesture = Gesture.Pan() .onUpdate((e) => { translationY.value = e.translationY < 0 ? e.translationY / 10 : e.translationY; @@ -125,4 +128,4 @@ export default function QRCodePage() { ); -} \ No newline at end of file +} diff --git a/hooks/useMaxBrightness.ts b/hooks/useMaxBrightness.ts new file mode 100644 index 000000000..813bd64d8 --- /dev/null +++ b/hooks/useMaxBrightness.ts @@ -0,0 +1,65 @@ +import { AppState } from "react-native"; +import { useEffect, useRef } from "react"; +import {setBrightnessAsync, getBrightnessAsync} from "expo-brightness" +import { warn } from "@/utils/logger/logger"; + +export function useMaxBrightness() { + + const previousBrightness = useRef(null); + const timeoutRef = useRef(null); + + useEffect(() => { + let isMounted = true; + + const enableBrightness = async () => { + try { + const current = await getBrightnessAsync(); + + if (!isMounted) return; + + if (previousBrightness.current === null) { + previousBrightness.current = current + } + await setBrightnessAsync(1); + } catch (error) { + warn("Failed to set brightness"); + } + }; + + const restoreBrightness = async () => { + try { + if (timeoutRef.current) { + clearTimeout(timeoutRef.current); + timeoutRef.current = null; + } + if (previousBrightness.current !== null) { + await setBrightnessAsync(previousBrightness.current); + } + } catch (error) { + warn("Failed to restore brightness"); + } + }; + + timeoutRef.current = setTimeout(() => { + if (isMounted) { + enableBrightness(); + } + }, 100); + + const subscription = AppState.addEventListener("change", (nextAppState) => { + if (nextAppState === "inactive" || nextAppState === "background" ) { + restoreBrightness(); + } else if (nextAppState === "active") { + timeoutRef.current = setTimeout(() => { + if (isMounted) enableBrightness(); + }, 100); + } + }); + + return () => { + isMounted = false; + restoreBrightness(); + subscription.remove(); + }; + }, []); +} diff --git a/package.json b/package.json index ab983bcff..af6efc6ab 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "expo-battery": "~55.0.11", "expo-blur": "~55.0.12", "expo-build-properties": "~55.0.11", + "expo-brightness": "~55.0.12", "expo-camera": "~55.0.13", "expo-crypto": "~55.0.12", "expo-dev-client": "~55.0.22",