Add permission handling

Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
2023-07-09 19:34:14 +03:00
parent 9ecc326008
commit 5c6d26e5a0
2 changed files with 23 additions and 11 deletions

View File

@@ -30,14 +30,15 @@ const SettingsProvider: React.FC<{ children: React.ReactNode }> = ({
const updateSettings = (newSettings: Partial<Settings>) => { const updateSettings = (newSettings: Partial<Settings>) => {
const updatedSettings = { ...settings, ...newSettings }; const updatedSettings = { ...settings, ...newSettings };
if (settings.storageUri !== updatedSettings.storageUri) { void getPersistedUriPermissions().then(permissions => {
void getPersistedUriPermissions().then(permissions => { permissions.forEach(permission => {
if (permissions.includes(settings.storageUri)) { if (!updatedSettings.storageUri.startsWith(permission + '/')) {
void releasePersistableUriPermission(settings.storageUri); void releasePersistableUriPermission(permission);
} }
}); });
void AsyncStorage.setItem('storageUri', updatedSettings.storageUri); });
}
void AsyncStorage.setItem('storageUri', updatedSettings.storageUri);
setSettings(updatedSettings); setSettings(updatedSettings);
}; };
@@ -49,12 +50,23 @@ const SettingsProvider: React.FC<{ children: React.ReactNode }> = ({
if (storageUri !== '') { if (storageUri !== '') {
const permissions = await getPersistedUriPermissions(); const permissions = await getPersistedUriPermissions();
if (!permissions.includes(storageUri)) { if (
!permissions.some(permission => storageUri.startsWith(permission + '/'))
) {
storageUri = ''; storageUri = '';
} }
const exists = await FileSystem.exists(storageUri); try {
if (!exists) { const exists = await FileSystem.exists(storageUri);
if (!exists) {
throw new Error('Storage URI does not exist');
}
const isDirectory = await FileSystem.isDir(storageUri);
if (!isDirectory) {
throw new Error('Storage URI is not a directory');
}
} catch {
storageUri = ''; storageUri = '';
} }
} }
@@ -75,7 +87,7 @@ const SettingsProvider: React.FC<{ children: React.ReactNode }> = ({
settings.storageUri === '' ? ( settings.storageUri === '' ? (
<WelcomeScreen <WelcomeScreen
selectStorageLocation={() => { selectStorageLocation={() => {
void openDocumentTree().then(uri => { void openDocumentTree(true).then(uri => {
updateSettings({ storageUri: uri.uri }); updateSettings({ storageUri: uri.uri });
}); });
}} }}

View File

@@ -60,7 +60,7 @@ const SettingsScreen = () => {
mode="elevated" mode="elevated"
style={styles.marginBottom} style={styles.marginBottom}
onPress={() => { onPress={() => {
void openDocumentTree().then(uri => { void openDocumentTree(true).then(uri => {
setSettings({ storageUri: uri.uri }); setSettings({ storageUri: uri.uri });
}); });
}}> }}>