App.tsx
4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import React from 'react';
import {FlatList, Platform, StatusBar, Text, View} from 'react-native';
import {Appbar, List, TouchableRipple} from 'react-native-paper';
import RNPermissions, {
NotificationsResponse,
Permission,
PERMISSIONS,
PermissionStatus,
RESULTS,
} from 'react-native-permissions';
import theme from './theme';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const {SIRI, ...PERMISSIONS_IOS} = PERMISSIONS.IOS; // remove siri (certificate required)
const PLATFORM_PERMISSIONS = Platform.select<
typeof PERMISSIONS_IOS | typeof PERMISSIONS.ANDROID | {}
>({
ios: PERMISSIONS_IOS,
android: PERMISSIONS.ANDROID,
default: {},
});
const PERMISSIONS_VALUES: Permission[] = Object.values(PLATFORM_PERMISSIONS);
const colors: {[key: string]: string} = {
unavailable: '#cfd8dc',
denied: '#ff9800',
granted: '#43a047',
blocked: '#e53935',
};
const icons: {[key: string]: string} = {
unavailable: 'circle',
denied: 'alert-circle',
granted: 'check-circle',
blocked: 'close-circle',
};
const PermissionRow = ({
name,
status,
onPress,
}: {
name: string;
status: string;
onPress: () => void;
}) => (
<TouchableRipple onPress={onPress}>
<List.Item
right={() => <List.Icon color={colors[status]} icon={icons[status]} />}
title={name}
description={status}
/>
</TouchableRipple>
);
interface State {
statuses: Partial<Record<Permission, PermissionStatus>>;
notifications: NotificationsResponse;
}
function toSettingString(setting: boolean | undefined) {
switch (setting) {
case true:
return RESULTS.GRANTED;
case false:
return RESULTS.DENIED;
default:
return RESULTS.UNAVAILABLE;
}
}
export default class App extends React.Component<{}, State> {
state: State = {
statuses: {},
notifications: {status: 'unavailable', settings: {}},
};
check = () =>
RNPermissions.checkMultiple(PERMISSIONS_VALUES)
.then((statuses) => this.setState({statuses}))
.then(() => RNPermissions.checkNotifications())
.then((notifications) => this.setState({notifications}))
.catch((error) => console.warn(error));
refresh = () => {
this.setState({statuses: {}}, this.check);
};
componentDidMount() {
this.check();
}
render() {
const {notifications} = this.state;
const {settings} = notifications;
return (
<View style={{flex: 1, backgroundColor: theme.colors.background}}>
<StatusBar
backgroundColor={theme.colors.primary}
barStyle="light-content"
/>
<Appbar.Header>
<Appbar.Content
title="react-native-permissions"
subtitle="Example application"
/>
<Appbar.Action onPress={this.refresh} icon="refresh" />
<Appbar.Action
icon="settings"
onPress={() => {
RNPermissions.openSettings();
}}
/>
</Appbar.Header>
<FlatList
keyExtractor={(item) => item}
data={Object.keys(PLATFORM_PERMISSIONS)}
renderItem={({item, index}) => {
const value = PERMISSIONS_VALUES[index];
const status = this.state.statuses[value];
if (!status) {
return null;
}
return (
<PermissionRow
status={status}
name={item}
onPress={() => {
RNPermissions.request(value)
.then(() => this.check())
.catch((error) => console.error(error));
}}
/>
);
}}
/>
<View
style={{backgroundColor: '#e0e0e0', height: 1}}
accessibilityRole="none"
/>
<TouchableRipple
onPress={() => {
RNPermissions.requestNotifications(['alert', 'badge', 'sound'])
.then(() => this.check())
.catch((error) => console.error(error));
}}>
<List.Item
right={() => (
<List.Icon
color={colors[notifications.status]}
icon={icons[notifications.status]}
/>
)}
title="NOTIFICATIONS"
description={notifications.status}
/>
</TouchableRipple>
<Text style={{margin: 15, marginTop: 0, color: '#777'}}>
{`alert: ${toSettingString(settings.alert)}\n`}
{`badge: ${toSettingString(settings.badge)}\n`}
{`sound: ${toSettingString(settings.sound)}\n`}
{`lockScreen: ${toSettingString(settings.lockScreen)}\n`}
{`notificationCenter: ${toSettingString(
settings.notificationCenter,
)}\n`}
{`carPlay: ${toSettingString(settings.carPlay)}\n`}
{`criticalAlert: ${toSettingString(settings.criticalAlert)}\n`}
</Text>
</View>
);
}
}