Skip to content

Commit 8be8119

Browse files
committed
Update example (fix typings)
1 parent c1e3251 commit 8be8119

File tree

1 file changed

+55
-44
lines changed

1 file changed

+55
-44
lines changed

example/App.js

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @flow
22

3-
import React, {Component} from 'react';
3+
import React from 'react';
4+
import Permissions, {type PermissionStatus} from 'react-native-permissions';
45

56
import {
67
StyleSheet,
@@ -12,38 +13,52 @@ import {
1213
Platform,
1314
} from 'react-native';
1415

15-
import Permissions from 'react-native-permissions';
16+
type State = {
17+
types: string[],
18+
status: {[permission: string]: PermissionStatus},
19+
isAlways: boolean,
20+
};
1621

17-
export default class App extends Component {
18-
state = {
22+
export default class App extends React.Component<{}, State> {
23+
state: State = {
1924
types: [],
2025
status: {},
26+
isAlways: false,
27+
statuses: {},
2128
};
2229

2330
componentDidMount() {
24-
let types = Permissions.getTypes();
25-
let canOpenSettings = Permissions.canOpenSettings();
31+
const types = Permissions.getTypes();
2632

27-
this.setState({types, canOpenSettings});
28-
this._updatePermissions(types);
29-
AppState.addEventListener('change', this._handleAppStateChange);
33+
this.setState({types});
34+
this.updatePermissions(types);
35+
36+
AppState.addEventListener('change', this.onAppStateChange);
3037
}
3138

3239
componentWillUnmount() {
33-
AppState.removeEventListener('change', this._handleAppStateChange);
40+
AppState.removeEventListener('change', this.onAppStateChange);
3441
}
3542

36-
//update permissions when app comes back from settings
37-
_handleAppStateChange = appState => {
38-
if (appState == 'active') {
39-
this._updatePermissions(this.state.types);
43+
onAppStateChange = (appState: 'active' | 'inactive' | 'background') => {
44+
if (appState === 'active') {
45+
this.updatePermissions(this.state.types);
4046
}
4147
};
4248

43-
_openSettings = () =>
44-
Permissions.openSettings().then(() => alert('back to app!!'));
49+
openSettings = () =>
50+
Permissions.canOpenSettings().then(canIt => {
51+
if (canIt) {
52+
return Permissions.openSettings();
53+
}
54+
55+
Alert.alert(
56+
'Not supported',
57+
'openSettings is currently not supported on this platform',
58+
);
59+
});
4560

46-
_updatePermissions = types => {
61+
updatePermissions = (types: string[]) => {
4762
Permissions.checkMultiple(types)
4863
.then(status => {
4964
if (this.state.isAlways) {
@@ -57,71 +72,67 @@ export default class App extends Component {
5772
.then(status => this.setState({status}));
5873
};
5974

60-
_requestPermission = permission => {
75+
requestPermission = (permission: string) => {
6176
var options;
6277

6378
if (permission == 'location') {
6479
options = this.state.isAlways ? 'always' : 'whenInUse';
6580
}
6681

6782
Permissions.request(permission, options)
68-
.then(res => {
83+
.then(result => {
6984
this.setState({
70-
status: {...this.state.status, [permission]: res},
85+
status: {...this.state.status, [permission]: result},
7186
});
72-
if (res != 'authorized') {
73-
var buttons = [{text: 'Cancel', style: 'cancel'}];
74-
if (this.state.canOpenSettings)
75-
buttons.push({
76-
text: 'Open Settings',
77-
onPress: this._openSettings,
78-
});
7987

88+
if (result != 'authorized') {
8089
Alert.alert(
8190
'Whoops!',
8291
'There was a problem getting your permission. Please enable it from settings.',
83-
buttons,
92+
[{text: 'Cancel', style: 'cancel'}],
8493
);
8594
}
8695
})
87-
.catch(e => console.warn(e));
96+
.catch(error => console.warn(error));
8897
};
8998

90-
_onLocationSwitchChange = () => {
99+
onLocationSwitchChange = () => {
91100
this.setState({isAlways: !this.state.isAlways});
92-
this._updatePermissions(this.state.types);
101+
this.updatePermissions(this.state.types);
93102
};
94103

95104
render() {
96105
return (
97106
<View style={styles.container}>
98-
{this.state.types.map(p => (
107+
{this.state.types.map(permission => (
99108
<TouchableHighlight
100-
style={[styles.button, styles[this.state.status[p]]]}
101-
key={p}
102-
onPress={() => this._requestPermission(p)}>
109+
style={[styles.button, styles[this.state.status[permission]]]}
110+
key={permission}
111+
onPress={() => this.requestPermission(permission)}>
103112
<View>
104113
<Text style={styles.text}>
105-
{Platform.OS == 'ios' && p == 'location'
114+
{Platform.OS == 'ios' && permission == 'location'
106115
? `location ${this.state.isAlways ? 'always' : 'whenInUse'}`
107-
: p}
116+
: permission}
117+
</Text>
118+
119+
<Text style={styles.subtext}>
120+
{this.state.status[permission]}
108121
</Text>
109-
<Text style={styles.subtext}>{this.state.status[p]}</Text>
110122
</View>
111123
</TouchableHighlight>
112124
))}
125+
113126
<View style={styles.footer}>
114127
<TouchableHighlight
115128
style={styles['footer_' + Platform.OS]}
116-
onPress={this._onLocationSwitchChange}>
129+
onPress={this.onLocationSwitchChange}>
117130
<Text style={styles.text}>Toggle location type</Text>
118131
</TouchableHighlight>
119132

120-
{this.state.canOpenSettings && (
121-
<TouchableHighlight onPress={this._openSettings}>
122-
<Text style={styles.text}>Open settings</Text>
123-
</TouchableHighlight>
124-
)}
133+
<TouchableHighlight onPress={this.openSettings}>
134+
<Text style={styles.text}>Open settings</Text>
135+
</TouchableHighlight>
125136
</View>
126137

127138
<Text style={styles['footer_' + Platform.OS]}>

0 commit comments

Comments
 (0)