Browse Source

Update example (fix typings)

Mathieu Acthernoene 5 years ago
parent
commit
8be8119a63
1 changed files with 55 additions and 44 deletions
  1. 55
    44
      example/App.js

+ 55
- 44
example/App.js View File

1
 // @flow
1
 // @flow
2
 
2
 
3
-import React, {Component} from 'react';
3
+import React from 'react';
4
+import Permissions, {type PermissionStatus} from 'react-native-permissions';
4
 
5
 
5
 import {
6
 import {
6
   StyleSheet,
7
   StyleSheet,
12
   Platform,
13
   Platform,
13
 } from 'react-native';
14
 } from 'react-native';
14
 
15
 
15
-import Permissions from 'react-native-permissions';
16
+type State = {
17
+  types: string[],
18
+  status: {[permission: string]: PermissionStatus},
19
+  isAlways: boolean,
20
+};
16
 
21
 
17
-export default class App extends Component {
18
-  state = {
22
+export default class App extends React.Component<{}, State> {
23
+  state: State = {
19
     types: [],
24
     types: [],
20
     status: {},
25
     status: {},
26
+    isAlways: false,
27
+    statuses: {},
21
   };
28
   };
22
 
29
 
23
   componentDidMount() {
30
   componentDidMount() {
24
-    let types = Permissions.getTypes();
25
-    let canOpenSettings = Permissions.canOpenSettings();
31
+    const types = Permissions.getTypes();
26
 
32
 
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);
30
   }
37
   }
31
 
38
 
32
   componentWillUnmount() {
39
   componentWillUnmount() {
33
-    AppState.removeEventListener('change', this._handleAppStateChange);
40
+    AppState.removeEventListener('change', this.onAppStateChange);
34
   }
41
   }
35
 
42
 
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);
40
     }
46
     }
41
   };
47
   };
42
 
48
 
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
+    });
45
 
60
 
46
-  _updatePermissions = types => {
61
+  updatePermissions = (types: string[]) => {
47
     Permissions.checkMultiple(types)
62
     Permissions.checkMultiple(types)
48
       .then(status => {
63
       .then(status => {
49
         if (this.state.isAlways) {
64
         if (this.state.isAlways) {
57
       .then(status => this.setState({status}));
72
       .then(status => this.setState({status}));
58
   };
73
   };
59
 
74
 
60
-  _requestPermission = permission => {
75
+  requestPermission = (permission: string) => {
61
     var options;
76
     var options;
62
 
77
 
63
     if (permission == 'location') {
78
     if (permission == 'location') {
65
     }
80
     }
66
 
81
 
67
     Permissions.request(permission, options)
82
     Permissions.request(permission, options)
68
-      .then(res => {
83
+      .then(result => {
69
         this.setState({
84
         this.setState({
70
-          status: {...this.state.status, [permission]: res},
85
+          status: {...this.state.status, [permission]: result},
71
         });
86
         });
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
-            });
79
 
87
 
88
+        if (result != 'authorized') {
80
           Alert.alert(
89
           Alert.alert(
81
             'Whoops!',
90
             'Whoops!',
82
             'There was a problem getting your permission. Please enable it from settings.',
91
             'There was a problem getting your permission. Please enable it from settings.',
83
-            buttons,
92
+            [{text: 'Cancel', style: 'cancel'}],
84
           );
93
           );
85
         }
94
         }
86
       })
95
       })
87
-      .catch(e => console.warn(e));
96
+      .catch(error => console.warn(error));
88
   };
97
   };
89
 
98
 
90
-  _onLocationSwitchChange = () => {
99
+  onLocationSwitchChange = () => {
91
     this.setState({isAlways: !this.state.isAlways});
100
     this.setState({isAlways: !this.state.isAlways});
92
-    this._updatePermissions(this.state.types);
101
+    this.updatePermissions(this.state.types);
93
   };
102
   };
94
 
103
 
95
   render() {
104
   render() {
96
     return (
105
     return (
97
       <View style={styles.container}>
106
       <View style={styles.container}>
98
-        {this.state.types.map(p => (
107
+        {this.state.types.map(permission => (
99
           <TouchableHighlight
108
           <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)}>
103
             <View>
112
             <View>
104
               <Text style={styles.text}>
113
               <Text style={styles.text}>
105
-                {Platform.OS == 'ios' && p == 'location'
114
+                {Platform.OS == 'ios' && permission == 'location'
106
                   ? `location ${this.state.isAlways ? 'always' : 'whenInUse'}`
115
                   ? `location ${this.state.isAlways ? 'always' : 'whenInUse'}`
107
-                  : p}
116
+                  : permission}
117
+              </Text>
118
+
119
+              <Text style={styles.subtext}>
120
+                {this.state.status[permission]}
108
               </Text>
121
               </Text>
109
-              <Text style={styles.subtext}>{this.state.status[p]}</Text>
110
             </View>
122
             </View>
111
           </TouchableHighlight>
123
           </TouchableHighlight>
112
         ))}
124
         ))}
125
+
113
         <View style={styles.footer}>
126
         <View style={styles.footer}>
114
           <TouchableHighlight
127
           <TouchableHighlight
115
             style={styles['footer_' + Platform.OS]}
128
             style={styles['footer_' + Platform.OS]}
116
-            onPress={this._onLocationSwitchChange}>
129
+            onPress={this.onLocationSwitchChange}>
117
             <Text style={styles.text}>Toggle location type</Text>
130
             <Text style={styles.text}>Toggle location type</Text>
118
           </TouchableHighlight>
131
           </TouchableHighlight>
119
 
132
 
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>
125
         </View>
136
         </View>
126
 
137
 
127
         <Text style={styles['footer_' + Platform.OS]}>
138
         <Text style={styles['footer_' + Platform.OS]}>