mirror of
https://github.com/movie-web/native-app.git
synced 2025-09-13 18:13:25 +00:00
49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
import { useSharedValue } from "react-native-reanimated";
|
|
import * as Brightness from "expo-brightness";
|
|
import { useDebounceValue } from "tamagui";
|
|
|
|
export const useBrightness = () => {
|
|
const [showBrightnessOverlay, setShowBrightnessOverlay] = useState(false);
|
|
|
|
const brightness = useSharedValue(0.5);
|
|
|
|
const currentBrightness = useDebounceValue(brightness.value, 20);
|
|
const memoizedBrightness = useMemo(
|
|
() => currentBrightness,
|
|
[currentBrightness],
|
|
);
|
|
|
|
useEffect(() => {
|
|
async function init() {
|
|
try {
|
|
const { status } = await Brightness.requestPermissionsAsync();
|
|
if (status === Brightness.PermissionStatus.GRANTED) {
|
|
const currentBrightness = await Brightness.getBrightnessAsync();
|
|
brightness.value = currentBrightness;
|
|
}
|
|
} catch (error) {
|
|
console.error("Failed to get brightness permissions:", error);
|
|
}
|
|
}
|
|
|
|
void init();
|
|
}, [brightness]);
|
|
|
|
const handleBrightnessChange = useCallback(async (newValue: number) => {
|
|
try {
|
|
await Brightness.setBrightnessAsync(newValue);
|
|
} catch (error) {
|
|
console.error("Failed to set brightness:", error);
|
|
}
|
|
}, []);
|
|
|
|
return {
|
|
showBrightnessOverlay,
|
|
setShowBrightnessOverlay,
|
|
brightness,
|
|
currentBrightness: memoizedBrightness,
|
|
handleBrightnessChange,
|
|
} as const;
|
|
};
|