Add settings page
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
159
src/screens/settings.tsx
Normal file
159
src/screens/settings.tsx
Normal file
@@ -0,0 +1,159 @@
|
||||
import React, { useState } from 'react';
|
||||
import { View } from 'react-native';
|
||||
import {
|
||||
Button,
|
||||
Switch,
|
||||
SegmentedButtons,
|
||||
Text,
|
||||
List,
|
||||
Snackbar,
|
||||
} from 'react-native-paper';
|
||||
import { useRealm } from '@realm/react';
|
||||
import { openDocumentTree } from 'react-native-scoped-storage';
|
||||
import { DocumentDirectoryPath } from 'react-native-fs';
|
||||
import { PaddedView } from '../components';
|
||||
import styles from '../styles';
|
||||
import { Meme } from '../database';
|
||||
import { useSettings } from '../contexts';
|
||||
|
||||
const SettingsScreen = () => {
|
||||
const [optimizingDatabase, setOptimizingDatabase] = useState(false);
|
||||
|
||||
const [snackbarVisible, setSnackbarVisible] = useState(false);
|
||||
const [snackbarMessage, setSnackbarMessage] = useState('');
|
||||
|
||||
const { settings, setSettings } = useSettings();
|
||||
|
||||
const setUseInternalStorage = (use: boolean) => {
|
||||
if (settings.useInternalStorage === use) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (use) {
|
||||
setSettings({
|
||||
useInternalStorage: use,
|
||||
storageUri: DocumentDirectoryPath,
|
||||
});
|
||||
} else {
|
||||
openDocumentTree(true)
|
||||
.then(uri => {
|
||||
setSettings({
|
||||
useInternalStorage: use,
|
||||
storageUri: uri.uri,
|
||||
});
|
||||
})
|
||||
.catch(() => {
|
||||
setSnackbarMessage('Failed to select storage path!');
|
||||
setSnackbarVisible(true);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const setStorageUri = (uri: string) => {
|
||||
if (settings.storageUri === uri) {
|
||||
return;
|
||||
}
|
||||
|
||||
setSettings({ storageUri: uri });
|
||||
};
|
||||
|
||||
const setAddNoMedia = (add: boolean) => {
|
||||
if (settings.addNoMedia === add) {
|
||||
return;
|
||||
}
|
||||
|
||||
setSettings({ addNoMedia: add });
|
||||
};
|
||||
|
||||
const realm = useRealm();
|
||||
|
||||
const optimizeDatabase = () => {
|
||||
setOptimizingDatabase(true);
|
||||
|
||||
const memes = realm.objects<Meme>('Meme');
|
||||
realm.write(() => {
|
||||
for (let index = memes.length - 1; index >= 0; index--) {
|
||||
// TODO: stat the uri to see if it exists and remove entry if it doesn't
|
||||
}
|
||||
});
|
||||
|
||||
const success = realm.compact();
|
||||
|
||||
if (success) {
|
||||
setSnackbarMessage('Database optimized!');
|
||||
setSnackbarVisible(true);
|
||||
} else {
|
||||
setSnackbarMessage('Database optimization failed!');
|
||||
setSnackbarVisible(true);
|
||||
}
|
||||
setOptimizingDatabase(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<PaddedView>
|
||||
<View>
|
||||
<List.Section>
|
||||
<List.Subheader>Database</List.Subheader>
|
||||
<Button
|
||||
mode="elevated"
|
||||
style={styles.marginBottom}
|
||||
loading={optimizingDatabase}
|
||||
onPress={optimizeDatabase}>
|
||||
Optimize Database Now
|
||||
</Button>
|
||||
</List.Section>
|
||||
<List.Section>
|
||||
<List.Subheader>Media Storage</List.Subheader>
|
||||
<SegmentedButtons
|
||||
style={styles.marginBottom}
|
||||
buttons={[
|
||||
{ label: 'Internal', value: 'interneal' },
|
||||
{ label: 'External', value: 'external' },
|
||||
]}
|
||||
value={settings.useInternalStorage ? 'interneal' : 'external'}
|
||||
onValueChange={value =>
|
||||
setUseInternalStorage(value === 'interneal')
|
||||
}
|
||||
/>
|
||||
<Button
|
||||
mode="elevated"
|
||||
style={styles.marginBottom}
|
||||
disabled={settings.useInternalStorage}
|
||||
onPress={() => {
|
||||
openDocumentTree(true)
|
||||
.then(uri => {
|
||||
setStorageUri(uri.uri);
|
||||
})
|
||||
.catch(() => {
|
||||
setSnackbarMessage('Failed to select storage path!');
|
||||
setSnackbarVisible(true);
|
||||
});
|
||||
}}>
|
||||
Change External Storage Path
|
||||
</Button>
|
||||
<View style={styles.spaceBetweenHorizontal}>
|
||||
<Text>Hide media from Gallery</Text>
|
||||
<Switch
|
||||
value={settings.addNoMedia}
|
||||
onValueChange={setAddNoMedia}
|
||||
disabled={settings.useInternalStorage}
|
||||
/>
|
||||
</View>
|
||||
</List.Section>
|
||||
</View>
|
||||
</PaddedView>
|
||||
<Snackbar
|
||||
visible={snackbarVisible}
|
||||
onDismiss={() => setSnackbarVisible(false)}
|
||||
action={{
|
||||
label: 'Dismiss',
|
||||
onPress: () => setSnackbarVisible(false),
|
||||
}}>
|
||||
{snackbarMessage}
|
||||
</Snackbar>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default SettingsScreen;
|
Reference in New Issue
Block a user