Add permissions.ts utility file
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
@@ -13,10 +13,13 @@ import { AndroidScoped, FileSystem } from 'react-native-file-access';
|
|||||||
import {
|
import {
|
||||||
getPersistedUriPermissions,
|
getPersistedUriPermissions,
|
||||||
openDocumentTree,
|
openDocumentTree,
|
||||||
releasePersistableUriPermission,
|
|
||||||
createFile,
|
createFile,
|
||||||
deleteFile,
|
deleteFile,
|
||||||
} from 'react-native-scoped-storage';
|
} from 'react-native-scoped-storage';
|
||||||
|
import {
|
||||||
|
clearPermissions,
|
||||||
|
isPermissionForPath,
|
||||||
|
} from '../utilities/permissions';
|
||||||
|
|
||||||
interface SettingsContextType {
|
interface SettingsContextType {
|
||||||
settings: Settings;
|
settings: Settings;
|
||||||
@@ -37,13 +40,7 @@ function SettingsProvider({ children }: { children: ReactNode }) {
|
|||||||
const updateSettings = (newSettings: Partial<Settings>) => {
|
const updateSettings = (newSettings: Partial<Settings>) => {
|
||||||
const updatedSettings = { ...settings, ...newSettings };
|
const updatedSettings = { ...settings, ...newSettings };
|
||||||
|
|
||||||
void getPersistedUriPermissions().then(permissions => {
|
void clearPermissions([updatedSettings.storageUri]);
|
||||||
permissions.forEach(permission => {
|
|
||||||
if (!updatedSettings.storageUri.startsWith(permission + '/')) {
|
|
||||||
void releasePersistableUriPermission(permission);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
void AsyncStorage.setItem('storageUri', updatedSettings.storageUri);
|
void AsyncStorage.setItem('storageUri', updatedSettings.storageUri);
|
||||||
|
|
||||||
@@ -78,7 +75,7 @@ function SettingsProvider({ children }: { children: ReactNode }) {
|
|||||||
const permissions = await getPersistedUriPermissions();
|
const permissions = await getPersistedUriPermissions();
|
||||||
if (
|
if (
|
||||||
!permissions.some(permission =>
|
!permissions.some(permission =>
|
||||||
storageUri.startsWith(permission + '/'),
|
isPermissionForPath(permission, storageUri),
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
storageUri = '';
|
storageUri = '';
|
||||||
|
1
src/utilities/index.ts
Normal file
1
src/utilities/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export { isPermissionForPath, clearPermissions } from './permissions';
|
19
src/utilities/permissions.ts
Normal file
19
src/utilities/permissions.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import {
|
||||||
|
getPersistedUriPermissions,
|
||||||
|
releasePersistableUriPermission,
|
||||||
|
} from 'react-native-scoped-storage';
|
||||||
|
|
||||||
|
const isPermissionForPath = (permission: string, path: string) => {
|
||||||
|
return path.startsWith(permission + '/');
|
||||||
|
};
|
||||||
|
|
||||||
|
const clearPermissions = async (excepts: string[] = []) => {
|
||||||
|
const permissions = await getPersistedUriPermissions();
|
||||||
|
permissions.forEach(permission => {
|
||||||
|
if (!excepts.some(except => isPermissionForPath(permission, except))) {
|
||||||
|
void releasePersistableUriPermission(permission);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export { isPermissionForPath, clearPermissions };
|
Reference in New Issue
Block a user