Skip to content

Commit 16e19f3

Browse files
committed
react -> react-native: automatically request permission on Android when import file
1 parent e98b09b commit 16e19f3

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Android iOS Web pixel drawing editor APP and sub-app that comes in handy when cr
3333
`npm run build-web-PixelShapeRN` to generate files in `build/` for production to deploy to `https://foo.bar.com/PixelShapeRN/`, e.g. [https://flyskywhy.github.io/PixelShapeRN/](https://flyskywhy.github.io/PixelShapeRN/) .
3434

3535
## permission
36-
To import gif file, you need manually allow storage permission with PixelShapeRN APP in OS settings. To let your APP has chance to choose which permission lib e.g. `react-native-permissions` or `expo-permissions` and which version, there is no permission lib in PixelShapeRN, so for your APP embeded PixelShapeRN as sub-app, you APP need request permission e.g. `android.permission.READ_EXTERNAL_STORAGE` with [react-native-permissions](https://github.com/zoontek/react-native-permissions).
36+
PixelShapeRN will automatically request `('react-native').PermissionsAndroid` on Android when import or save file, so your iOS APP embeded PixelShapeRN as sub-app maybe need request permission with e.g. `react-native-permissions` or `expo-permissions`.
3737

3838
## be embeded as sub-app
3939
Ref to [Isolating Redux Sub-Apps](https://redux.js.org/usage/isolating-redux-sub-apps) and [Breaking out of Redux paradigm to isolate apps](https://gist.github.com/gaearon/eeee2f619620ab7b55673a4ee2bf8400), PixelShapeRN can be embeded into other react-native APP easily.

src/components/apptoolbox/Apptoolbox.js

+30-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import React, {Component} from 'react';
2-
import {Dimensions, Platform, StyleSheet, View} from 'react-native';
2+
import {
3+
Dimensions,
4+
PermissionsAndroid,
5+
Platform,
6+
StyleSheet,
7+
View,
8+
} from 'react-native';
39
import RNSystemFileBrower from 'react-native-system-file-browser';
410
// import decorateWithKeyBindings from '../../helpers/KeyBindings';
511

@@ -70,7 +76,29 @@ class Apptoolbox extends Component {
7076
this.context.refApptoolbox && this.context.refApptoolbox(this);
7177
}
7278

73-
importFile() {
79+
async importFile() {
80+
if (Platform.OS === 'android') {
81+
try {
82+
const granted = await PermissionsAndroid.request(
83+
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
84+
{
85+
// if user allow fisrt in APP, then manually disallow in OS setting,
86+
// then will show below message
87+
title: 'Read External Storage Permission',
88+
message: 'Your app needs this permission to import file.',
89+
buttonNeutral: 'Ask Me Later',
90+
buttonNegative: 'Cancel',
91+
buttonPositive: 'OK',
92+
},
93+
);
94+
if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
95+
return false;
96+
}
97+
} catch (err) {
98+
return false;
99+
}
100+
}
101+
74102
const params = Platform.OS === 'android' ? {types: 'image/gif'} : undefined;
75103
RNSystemFileBrower.openFileBrower(params).then((res) => {
76104
if (res && typeof res.url === 'string') {

src/components/modals/Downloadproject/Downloadproject.js

+2
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ class DownloadProjectModal extends Component {
160160
const granted = await PermissionsAndroid.request(
161161
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
162162
{
163+
// if user allow fisrt in APP, then manually disallow in OS setting,
164+
// then will show below message
163165
title: 'Write External Storage Permission',
164166
message: 'Your app needs this permission to save file.',
165167
buttonNeutral: 'Ask Me Later',

0 commit comments

Comments
 (0)