App.tsx 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import React from 'react';
  2. import {Appbar, List, TouchableRipple} from 'react-native-paper';
  3. import theme from './theme';
  4. import {
  5. FlatList,
  6. Platform,
  7. StatusBar,
  8. Text,
  9. View,
  10. GestureResponderEvent,
  11. } from 'react-native';
  12. import RNPermissions, {
  13. Permission,
  14. PermissionStatus,
  15. NotificationsResponse,
  16. } from 'react-native-permissions';
  17. const {PERMISSIONS} = RNPermissions;
  18. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  19. const {SIRI, ...PERMISSIONS_IOS} = PERMISSIONS.IOS; // remove siri (certificate required)
  20. const PLATFORM_PERMISSIONS = Platform.select({
  21. ios: PERMISSIONS_IOS,
  22. android: PERMISSIONS.ANDROID,
  23. default: {},
  24. });
  25. const PERMISSIONS_VALUES: Permission[] = Object.values(PLATFORM_PERMISSIONS);
  26. const colors: {[key: string]: string} = {
  27. unavailable: '#cfd8dc',
  28. denied: '#ff9800',
  29. granted: '#43a047',
  30. blocked: '#e53935',
  31. };
  32. const icons: {[key: string]: string} = {
  33. unavailable: 'lens',
  34. denied: 'error',
  35. granted: 'check-circle',
  36. blocked: 'cancel',
  37. };
  38. const PermissionRow = ({
  39. name,
  40. status,
  41. onPress,
  42. }: {
  43. name: string;
  44. status: string;
  45. onPress: (event: GestureResponderEvent) => void;
  46. }) => (
  47. <TouchableRipple onPress={onPress}>
  48. <List.Item
  49. right={() => <List.Icon color={colors[status]} icon={icons[status]} />}
  50. title={name}
  51. description={status}
  52. />
  53. </TouchableRipple>
  54. );
  55. interface State {
  56. statuses: PermissionStatus[];
  57. notifications: NotificationsResponse;
  58. }
  59. export default class App extends React.Component<{}, State> {
  60. state: State = {
  61. statuses: [],
  62. notifications: {status: 'unavailable', settings: {}},
  63. };
  64. check = () =>
  65. Promise.all(PERMISSIONS_VALUES.map(_ => RNPermissions.check(_)))
  66. .then(statuses => this.setState({statuses}))
  67. .then(() => RNPermissions.checkNotifications())
  68. .then(notifications => this.setState({notifications}))
  69. .catch(error => console.warn(error));
  70. refresh = () => {
  71. this.setState({statuses: []}, this.check);
  72. };
  73. componentDidMount() {
  74. this.check();
  75. }
  76. render() {
  77. return (
  78. <View style={{flex: 1, backgroundColor: theme.colors.background}}>
  79. <StatusBar
  80. backgroundColor={theme.colors.primary}
  81. barStyle="light-content"
  82. />
  83. <Appbar.Header>
  84. <Appbar.Content
  85. title="react-native-permissions"
  86. subtitle="Example application"
  87. />
  88. <Appbar.Action onPress={this.refresh} icon="refresh" />
  89. <Appbar.Action
  90. onPress={RNPermissions.openSettings}
  91. icon="settings-applications"
  92. />
  93. </Appbar.Header>
  94. <FlatList
  95. keyExtractor={item => item}
  96. data={Object.keys(PLATFORM_PERMISSIONS)}
  97. renderItem={({item, index}) => {
  98. const value = PERMISSIONS_VALUES[index];
  99. const status = this.state.statuses[index];
  100. return (
  101. <PermissionRow
  102. status={status}
  103. name={item}
  104. onPress={() => {
  105. RNPermissions.request(value)
  106. .then(result => {
  107. const statuses = [...this.state.statuses];
  108. statuses[index] = result;
  109. this.setState({statuses});
  110. })
  111. .then(() => this.check());
  112. }}
  113. />
  114. );
  115. }}
  116. />
  117. <View
  118. style={{backgroundColor: '#e0e0e0', height: 1}}
  119. accessibilityRole="none"
  120. />
  121. <TouchableRipple
  122. onPress={() => {
  123. RNPermissions.requestNotifications(['alert', 'badge', 'sound']);
  124. }}>
  125. <List.Item
  126. right={() => (
  127. <List.Icon
  128. color={colors[this.state.notifications.status]}
  129. icon={icons[this.state.notifications.status]}
  130. />
  131. )}
  132. title="NOTIFICATIONS"
  133. description={this.state.notifications.status}
  134. />
  135. </TouchableRipple>
  136. <Text style={{margin: 15, marginTop: 0, color: '#777'}}>
  137. {'settings = ' +
  138. JSON.stringify(this.state.notifications.settings, null, 2)}
  139. </Text>
  140. </View>
  141. );
  142. }
  143. }