App.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // @flow
  2. import * as React from "react";
  3. import { Appbar, List, TouchableRipple, Snackbar } from "react-native-paper";
  4. import * as RNPermissions from "react-native-permissions";
  5. import type { PermissionStatus } from "react-native-permissions";
  6. import theme from "./theme";
  7. import {
  8. AppState,
  9. Platform,
  10. StatusBar,
  11. ScrollView,
  12. StyleSheet,
  13. Text,
  14. View,
  15. } from "react-native";
  16. // $FlowFixMe
  17. let platformPermissions: string[] = Object.values(
  18. Platform.OS === "ios"
  19. ? RNPermissions.IOS_PERMISSIONS
  20. : RNPermissions.ANDROID_PERMISSIONS,
  21. ).filter(permission => permission !== "SIRI");
  22. const statusColors: { [PermissionStatus]: string } = {
  23. granted: "#43a047",
  24. denied: "#ff9800",
  25. never_ask_again: "#e53935",
  26. unavailable: "#cfd8dc",
  27. };
  28. const statusIcons: { [PermissionStatus]: string } = {
  29. granted: "check-circle",
  30. denied: "error",
  31. never_ask_again: "cancel",
  32. unavailable: "lens",
  33. };
  34. type AppStateType = "active" | "background" | "inactive";
  35. type Props = {};
  36. // RNPermissions.checkMultiple([
  37. // RNPermissions.ANDROID_PERMISSIONS.ACCESS_FINE_LOCATION,
  38. // RNPermissions.ANDROID_PERMISSIONS.PROCESS_OUTGOING_CALLS,
  39. // ]).then(result => {
  40. // console.log(result);
  41. // });
  42. type State = {|
  43. snackBarVisible: boolean,
  44. watchAppState: boolean,
  45. statuses: { [string]: PermissionStatus },
  46. |};
  47. export default class App extends React.Component<Props, State> {
  48. constructor(props: Props) {
  49. super(props);
  50. this.state = {
  51. snackBarVisible: false,
  52. watchAppState: false,
  53. statuses: {},
  54. };
  55. setTimeout(() => {
  56. this.checkAllPermissions();
  57. }, 2000);
  58. }
  59. componentDidMount() {
  60. AppState.addEventListener("change", this.onAppStateChange);
  61. }
  62. componentWillUnmount() {
  63. AppState.removeEventListener("change", this.onAppStateChange);
  64. }
  65. checkAllPermissions = () => {
  66. RNPermissions.checkMultiple(platformPermissions)
  67. .then(statuses => {
  68. console.log(statuses);
  69. this.setState({ statuses });
  70. })
  71. .catch(error => {
  72. console.error(error);
  73. });
  74. };
  75. onAppStateChange = (nextAppState: AppStateType) => {
  76. if (this.state.watchAppState && nextAppState === "active") {
  77. this.setState({
  78. snackBarVisible: true,
  79. watchAppState: false,
  80. });
  81. setTimeout(() => {
  82. // @TODO don't fire setState on unmounted component
  83. this.setState({ snackBarVisible: false });
  84. }, 3000);
  85. }
  86. };
  87. render() {
  88. return (
  89. <View style={styles.container}>
  90. <StatusBar
  91. backgroundColor={theme.colors.primary}
  92. barStyle="light-content"
  93. />
  94. <Appbar.Header>
  95. <Appbar.Content
  96. title="react-native-permissions"
  97. subtitle="Example application"
  98. />
  99. <Appbar.Action
  100. icon="settings-applications"
  101. onPress={() => {
  102. this.setState({ watchAppState: true }, () => {
  103. RNPermissions.openSettings();
  104. });
  105. }}
  106. />
  107. </Appbar.Header>
  108. <ScrollView>
  109. <List.Section>
  110. {platformPermissions.map(permission => {
  111. const status = this.state.statuses[permission];
  112. return (
  113. <TouchableRipple
  114. key={permission}
  115. disabled={
  116. status === RNPermissions.RESULTS.UNAVAILABLE ||
  117. status === RNPermissions.RESULTS.NEVER_ASK_AGAIN
  118. }
  119. onPress={() => {
  120. RNPermissions.request(permission).then(status => {
  121. this.setState(prevState => ({
  122. ...prevState,
  123. statuses: {
  124. ...prevState.statuses,
  125. [permission]: status,
  126. },
  127. }));
  128. });
  129. }}
  130. >
  131. <List.Item
  132. title={permission}
  133. description={status}
  134. right={() => (
  135. <List.Icon
  136. color={statusColors[status]}
  137. icon={statusIcons[status]}
  138. />
  139. )}
  140. />
  141. </TouchableRipple>
  142. );
  143. })}
  144. </List.Section>
  145. </ScrollView>
  146. <Snackbar
  147. onDismiss={() => this.setState({ snackBarVisible: false })}
  148. visible={this.state.snackBarVisible}
  149. >
  150. Welcome back ! Refreshing permissions…
  151. </Snackbar>
  152. </View>
  153. );
  154. }
  155. }
  156. const styles = StyleSheet.create({
  157. container: {
  158. flex: 1,
  159. backgroundColor: theme.colors.background,
  160. },
  161. });