Kaynağa Gözat

Improve example

Mathieu Acthernoene 5 yıl önce
ebeveyn
işleme
60b764b1a9
1 değiştirilmiş dosya ile 71 ekleme ve 15 silme
  1. 71
    15
      example/App.tsx

+ 71
- 15
example/App.tsx Dosyayı Görüntüle

@@ -12,16 +12,18 @@ import {
12 12
 } from 'react-native';
13 13
 
14 14
 import RNPermissions, {
15
-  Permission,
16 15
   PermissionStatus,
16
+  Permission,
17 17
   NotificationsResponse,
18 18
 } from 'react-native-permissions';
19 19
 
20
-const {PERMISSIONS} = RNPermissions;
20
+const {PERMISSIONS, RESULTS} = RNPermissions;
21 21
 // eslint-disable-next-line @typescript-eslint/no-unused-vars
22 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 27
   ios: PERMISSIONS_IOS,
26 28
   android: PERMISSIONS.ANDROID,
27 29
   default: {},
@@ -88,6 +90,17 @@ export default class App extends React.Component<{}, State> {
88 90
   }
89 91
 
90 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 104
     return (
92 105
       <View style={{flex: 1, backgroundColor: theme.colors.background}}>
93 106
         <StatusBar
@@ -122,12 +135,8 @@ export default class App extends React.Component<{}, State> {
122 135
                 name={item}
123 136
                 onPress={() => {
124 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,23 +150,70 @@ export default class App extends React.Component<{}, State> {
141 150
 
142 151
         <TouchableRipple
143 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 157
           <List.Item
147 158
             right={() => (
148 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 164
             title="NOTIFICATIONS"
154
-            description={this.state.notifications.status}
165
+            description={notifications.status}
155 166
           />
156 167
         </TouchableRipple>
157 168
 
158 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 217
         </Text>
162 218
       </View>
163 219
     );