浏览代码

Update example (fix typings)

Mathieu Acthernoene 5 年前
父节点
当前提交
8be8119a63
共有 1 个文件被更改,包括 55 次插入44 次删除
  1. 55
    44
      example/App.js

+ 55
- 44
example/App.js 查看文件

@@ -1,6 +1,7 @@
1 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 6
 import {
6 7
   StyleSheet,
@@ -12,38 +13,52 @@ import {
12 13
   Platform,
13 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 24
     types: [],
20 25
     status: {},
26
+    isAlways: false,
27
+    statuses: {},
21 28
   };
22 29
 
23 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 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 62
     Permissions.checkMultiple(types)
48 63
       .then(status => {
49 64
         if (this.state.isAlways) {
@@ -57,7 +72,7 @@ export default class App extends Component {
57 72
       .then(status => this.setState({status}));
58 73
   };
59 74
 
60
-  _requestPermission = permission => {
75
+  requestPermission = (permission: string) => {
61 76
     var options;
62 77
 
63 78
     if (permission == 'location') {
@@ -65,63 +80,59 @@ export default class App extends Component {
65 80
     }
66 81
 
67 82
     Permissions.request(permission, options)
68
-      .then(res => {
83
+      .then(result => {
69 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 89
           Alert.alert(
81 90
             'Whoops!',
82 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 100
     this.setState({isAlways: !this.state.isAlways});
92
-    this._updatePermissions(this.state.types);
101
+    this.updatePermissions(this.state.types);
93 102
   };
94 103
 
95 104
   render() {
96 105
     return (
97 106
       <View style={styles.container}>
98
-        {this.state.types.map(p => (
107
+        {this.state.types.map(permission => (
99 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 112
             <View>
104 113
               <Text style={styles.text}>
105
-                {Platform.OS == 'ios' && p == 'location'
114
+                {Platform.OS == 'ios' && permission == 'location'
106 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 121
               </Text>
109
-              <Text style={styles.subtext}>{this.state.status[p]}</Text>
110 122
             </View>
111 123
           </TouchableHighlight>
112 124
         ))}
125
+
113 126
         <View style={styles.footer}>
114 127
           <TouchableHighlight
115 128
             style={styles['footer_' + Platform.OS]}
116
-            onPress={this._onLocationSwitchChange}>
129
+            onPress={this.onLocationSwitchChange}>
117 130
             <Text style={styles.text}>Toggle location type</Text>
118 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 136
         </View>
126 137
 
127 138
         <Text style={styles['footer_' + Platform.OS]}>