-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
BrightnessScreen.tsx
106 lines (94 loc) · 3.32 KB
/
BrightnessScreen.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import Slider from '@react-native-community/slider';
import * as Brightness from 'expo-brightness';
import * as Permissions from 'expo-permissions';
import React from 'react';
import { ScrollView, Text, View } from 'react-native';
import Button from '../components/Button';
import HeadingText from '../components/HeadingText';
interface State {
initBrightness: { [type: string]: number };
sliderBrightness: { [type: string]: number };
systemBrightnessPermissionGranted: boolean;
}
const brightnessTypes: string[] = ['Brightness', 'SystemBrightness'];
export default class BrightnessScreen extends React.Component<{}, State> {
static navigationOptions = {
title: 'Brightness',
};
readonly state: State = {
initBrightness: {},
sliderBrightness: {},
systemBrightnessPermissionGranted: false,
};
componentDidMount() {
Brightness.getBrightnessAsync().then(value => {
this.setState({ initBrightness: { ...this.state.initBrightness, Brightness: value } });
});
Brightness.getSystemBrightnessAsync().then(value => {
this.setState({ initBrightness: { ...this.state.initBrightness, SystemBrightness: value } });
});
Permissions.getAsync(Permissions.SYSTEM_BRIGHTNESS).then(result => {
if (result.status === 'granted') {
this.setState({ systemBrightnessPermissionGranted: true });
}
});
}
askSystemBrightnessPermissionAsync() {
Permissions.askAsync(Permissions.SYSTEM_BRIGHTNESS).then(result => {
if (result.status === 'granted') {
this.setState({ systemBrightnessPermissionGranted: true });
}
alert(JSON.stringify(result, null, 2));
});
}
alertBrightnessAsync(type: string) {
(type === 'Brightness'
? Brightness.getBrightnessAsync()
: Brightness.getSystemBrightnessAsync()
).then(value => {
alert(value);
});
}
updateBrightnessAsync(value: number, type: string) {
this.setState({
sliderBrightness: { ...this.state.sliderBrightness, [type]: value },
});
if (type === 'Brightness') {
Brightness.setBrightnessAsync(value);
} else {
Brightness.setSystemBrightnessAsync(value);
}
}
render() {
let views = brightnessTypes.map(type => {
return (
<View key={type} style={{ padding: 20 }}>
<HeadingText>{type}</HeadingText>
{type === 'SystemBrightness' && (
<Button
title="Permissions.SYSTEM_BRIGHTNESS"
onPress={() => this.askSystemBrightnessPermissionAsync()}
style={{ marginTop: 15 }}
/>
)}
<Button
title={'get' + type + 'Async'}
onPress={() => this.alertBrightnessAsync(type)}
style={{ marginTop: 15, marginBottom: 20 }}
/>
<Text style={{ marginBottom: -2 }}>
{'set' + type + 'Async: '}
{(this.state.sliderBrightness[type] || this.state.initBrightness[type] || 0).toFixed(3)}
</Text>
<Slider
{...this.props}
value={this.state.initBrightness[type] || 0}
disabled={type === 'SystemBrightness' && !this.state.systemBrightnessPermissionGranted}
onValueChange={value => this.updateBrightnessAsync(value, type)}
/>
</View>
);
});
return <ScrollView style={{ flex: 1 }}>{views}</ScrollView>;
}
}