Reputation: 41
I'm using React Native with @adrianso/react-native-device-brightness
library for this
Imagine I'm in screen A, having device brightness about 0.5
When redirecting to screen B, Current brightness is saved, then I set brightness to 1 (maximum value). If I back to screen A, the brightness will be set back to saved value (0.5)
This would work normally, until:
One of following scenarios would happen:
I've tried:
expo-brightness
By any means, is this issue related to the thread blocking of iOS?
Upvotes: 1
Views: 136
Reputation: 1
const [previousBrightness, setPreviousBrightness] = useState<number | null>(null);
const getBrightness = () => {
Brightness.getBrightnessAsync().then((value) => {
setPreviousBrightness(value);
});
};
const restoreBrightness = useCallback(() => {
if (previousBrightness !== null) {
Brightness.setBrightnessAsync(previousBrightness)
.then(() => {
console.info('Brightness restored');
})
.catch(() => {
console.error('Error restoring brightness');
});
}
}, [previousBrightness]);
useEffect(() => {
(async () => {
getBrightness();
const { status } = await Brightness.requestPermissionsAsync();
if (status === 'granted') {
Brightness.setBrightnessAsync(1)
.then(() => {
console.info('Brightness set to 1');
})
.catch(() => {
console.error('Error setting brightness');
});
}
})();
return restoreBrightness;
},[restoreBrightness]);
Upvotes: 0