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 {
|
||||
getPersistedUriPermissions,
|
||||
openDocumentTree,
|
||||
releasePersistableUriPermission,
|
||||
createFile,
|
||||
deleteFile,
|
||||
} from 'react-native-scoped-storage';
|
||||
import {
|
||||
clearPermissions,
|
||||
isPermissionForPath,
|
||||
} from '../utilities/permissions';
|
||||
|
||||
interface SettingsContextType {
|
||||
settings: Settings;
|
||||
@@ -37,13 +40,7 @@ function SettingsProvider({ children }: { children: ReactNode }) {
|
||||
const updateSettings = (newSettings: Partial<Settings>) => {
|
||||
const updatedSettings = { ...settings, ...newSettings };
|
||||
|
||||
void getPersistedUriPermissions().then(permissions => {
|
||||
permissions.forEach(permission => {
|
||||
if (!updatedSettings.storageUri.startsWith(permission + '/')) {
|
||||
void releasePersistableUriPermission(permission);
|
||||
}
|
||||
});
|
||||
});
|
||||
void clearPermissions([updatedSettings.storageUri]);
|
||||
|
||||
void AsyncStorage.setItem('storageUri', updatedSettings.storageUri);
|
||||
|
||||
@@ -78,7 +75,7 @@ function SettingsProvider({ children }: { children: ReactNode }) {
|
||||
const permissions = await getPersistedUriPermissions();
|
||||
if (
|
||||
!permissions.some(permission =>
|
||||
storageUri.startsWith(permission + '/'),
|
||||
isPermissionForPath(permission, 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