Browse Source

Improve example

Mathieu Acthernoene 5 years ago
parent
commit
60b764b1a9
1 changed files with 71 additions and 15 deletions
  1. 71
    15
      example/App.tsx

+ 71
- 15
example/App.tsx View File

12
 } from 'react-native';
12
 } from 'react-native';
13
 
13
 
14
 import RNPermissions, {
14
 import RNPermissions, {
15
-  Permission,
16
   PermissionStatus,
15
   PermissionStatus,
16
+  Permission,
17
   NotificationsResponse,
17
   NotificationsResponse,
18
 } from 'react-native-permissions';
18
 } from 'react-native-permissions';
19
 
19
 
20
-const {PERMISSIONS} = RNPermissions;
20
+const {PERMISSIONS, RESULTS} = RNPermissions;
21
 // eslint-disable-next-line @typescript-eslint/no-unused-vars
21
 // eslint-disable-next-line @typescript-eslint/no-unused-vars
22
 const {SIRI, ...PERMISSIONS_IOS} = PERMISSIONS.IOS; // remove siri (certificate required)
22
 const {SIRI, ...PERMISSIONS_IOS} = PERMISSIONS.IOS; // remove siri (certificate required)
23
 
23
 
24
-const PLATFORM_PERMISSIONS = Platform.select({
24
+const PLATFORM_PERMISSIONS = Platform.select<
25
+  typeof PERMISSIONS_IOS | typeof PERMISSIONS.ANDROID | {}
26
+>({
25
   ios: PERMISSIONS_IOS,
27
   ios: PERMISSIONS_IOS,
26
   android: PERMISSIONS.ANDROID,
28
   android: PERMISSIONS.ANDROID,
27
   default: {},
29
   default: {},
88
   }
90
   }
89
 
91
 
90
   render() {
92
   render() {
93
+    const {notifications} = this.state;
94
+
95
+    const {
96
+      alert,
97
+      badge,
98
+      sound,
99
+      lockScreen,
100
+      carPlay,
101
+      critical,
102
+    } = notifications.settings;
103
+
91
     return (
104
     return (
92
       <View style={{flex: 1, backgroundColor: theme.colors.background}}>
105
       <View style={{flex: 1, backgroundColor: theme.colors.background}}>
93
         <StatusBar
106
         <StatusBar
122
                 name={item}
135
                 name={item}
123
                 onPress={() => {
136
                 onPress={() => {
124
                   RNPermissions.request(value)
137
                   RNPermissions.request(value)
125
-                    .then(result => {
126
-                      const statuses = [...this.state.statuses];
127
-                      statuses[index] = result;
128
-                      this.setState({statuses});
129
-                    })
130
-                    .then(() => this.check());
138
+                    .then(() => this.check())
139
+                    .catch(err => console.error(err));
131
                 }}
140
                 }}
132
               />
141
               />
133
             );
142
             );
141
 
150
 
142
         <TouchableRipple
151
         <TouchableRipple
143
           onPress={() => {
152
           onPress={() => {
144
-            RNPermissions.requestNotifications(['alert', 'badge', 'sound']);
153
+            RNPermissions.requestNotifications(['alert', 'badge', 'sound'])
154
+              .then(() => this.check())
155
+              .catch(err => console.error(err));
145
           }}>
156
           }}>
146
           <List.Item
157
           <List.Item
147
             right={() => (
158
             right={() => (
148
               <List.Icon
159
               <List.Icon
149
-                color={colors[this.state.notifications.status]}
150
-                icon={icons[this.state.notifications.status]}
160
+                color={colors[notifications.status]}
161
+                icon={icons[notifications.status]}
151
               />
162
               />
152
             )}
163
             )}
153
             title="NOTIFICATIONS"
164
             title="NOTIFICATIONS"
154
-            description={this.state.notifications.status}
165
+            description={notifications.status}
155
           />
166
           />
156
         </TouchableRipple>
167
         </TouchableRipple>
157
 
168
 
158
         <Text style={{margin: 15, marginTop: 0, color: '#777'}}>
169
         <Text style={{margin: 15, marginTop: 0, color: '#777'}}>
159
-          {'settings = ' +
160
-            JSON.stringify(this.state.notifications.settings, null, 2)}
170
+          {`alert: ${
171
+            alert
172
+              ? RESULTS.GRANTED
173
+              : alert === false
174
+              ? RESULTS.DENIED
175
+              : RESULTS.UNAVAILABLE
176
+          }\n`}
177
+
178
+          {`badge: ${
179
+            badge
180
+              ? RESULTS.GRANTED
181
+              : badge === false
182
+              ? RESULTS.DENIED
183
+              : RESULTS.UNAVAILABLE
184
+          }\n`}
185
+
186
+          {`sound: ${
187
+            sound
188
+              ? RESULTS.GRANTED
189
+              : sound === false
190
+              ? RESULTS.DENIED
191
+              : RESULTS.UNAVAILABLE
192
+          }\n`}
193
+
194
+          {`lockScreen: ${
195
+            lockScreen
196
+              ? RESULTS.GRANTED
197
+              : lockScreen === false
198
+              ? RESULTS.DENIED
199
+              : RESULTS.UNAVAILABLE
200
+          }\n`}
201
+
202
+          {`carPlay: ${
203
+            carPlay
204
+              ? RESULTS.GRANTED
205
+              : carPlay === false
206
+              ? RESULTS.DENIED
207
+              : RESULTS.UNAVAILABLE
208
+          }\n`}
209
+
210
+          {`critical: ${
211
+            critical
212
+              ? RESULTS.GRANTED
213
+              : critical === false
214
+              ? RESULTS.DENIED
215
+              : RESULTS.UNAVAILABLE
216
+          }\n`}
161
         </Text>
217
         </Text>
162
       </View>
218
       </View>
163
     );
219
     );