Add variable storage locations & batch adding
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
@@ -10,7 +10,6 @@ import {
|
||||
Text,
|
||||
useTheme,
|
||||
} from 'react-native-paper';
|
||||
import { openDocumentTree } from 'react-native-scoped-storage';
|
||||
import { useDispatch, useSelector } from 'react-redux';
|
||||
import type {} from 'redux-thunk/extend-redux';
|
||||
import {
|
||||
@@ -18,8 +17,11 @@ import {
|
||||
setGridColumns,
|
||||
setMasonryColumns,
|
||||
setNoMedia,
|
||||
setStorageUri,
|
||||
} from '../state';
|
||||
import StorageLocationChangeDialog from '../components/storageLocationChangeDialog';
|
||||
import { useRealm } from '@realm/react';
|
||||
import { FileSystem, FileStat } from 'react-native-file-access';
|
||||
import { Meme } from '../database';
|
||||
|
||||
const settingsStyles = StyleSheet.create({
|
||||
scrollView: {
|
||||
@@ -51,18 +53,38 @@ const Settings = () => {
|
||||
const gridColumns = useSelector(
|
||||
(state: RootState) => state.settings.gridColumns,
|
||||
);
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
const storageUri = useSelector(
|
||||
(state: RootState) => state.settings.storageUri,
|
||||
)!;
|
||||
const dispatch = useDispatch();
|
||||
const realm = useRealm();
|
||||
|
||||
const [isOptimizingDatabase, setIsOptimizingDatabase] = useState(false);
|
||||
const [snackbarVisible, setSnackbarVisible] = useState(false);
|
||||
const [snackbarMessage, setSnackbarMessage] = useState('');
|
||||
|
||||
const optimizeDatabase = () => {
|
||||
setIsOptimizingDatabase(true);
|
||||
// TODO: clean up missing / extra files
|
||||
setSnackbarMessage('Database optimized!');
|
||||
const [
|
||||
storageLocationChangeDialogVisible,
|
||||
setStorageLocationChangeDialogVisible,
|
||||
] = useState(false);
|
||||
|
||||
const refreshMemeMetadata = async () => {
|
||||
const stat = await FileSystem.statDir(storageUri);
|
||||
|
||||
const statMap = new Map<string, FileStat>();
|
||||
stat.forEach(s => statMap.set(s.filename, s));
|
||||
|
||||
const memes = realm.objects<Meme>(Meme.schema.name);
|
||||
|
||||
realm.write(() => {
|
||||
memes.forEach(meme => {
|
||||
const fileStat = statMap.get(meme.filename);
|
||||
meme.size = fileStat?.size ?? 0;
|
||||
});
|
||||
});
|
||||
|
||||
setSnackbarMessage('Meme metadata refreshed.');
|
||||
setSnackbarVisible(true);
|
||||
setIsOptimizingDatabase(false);
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -111,15 +133,18 @@ const Settings = () => {
|
||||
/>
|
||||
</List.Section>
|
||||
<List.Section>
|
||||
<List.Subheader>Media Storage</List.Subheader>
|
||||
<List.Subheader>Storage</List.Subheader>
|
||||
<Button
|
||||
mode="elevated"
|
||||
style={settingsStyles.marginBottom}
|
||||
onPress={async () => {
|
||||
const { uri } = await openDocumentTree(true);
|
||||
void dispatch(setStorageUri(uri));
|
||||
}}>
|
||||
Change External Storage Path
|
||||
onPress={() => setStorageLocationChangeDialogVisible(true)}>
|
||||
Change Storage Location
|
||||
</Button>
|
||||
<Button
|
||||
mode="elevated"
|
||||
style={settingsStyles.marginBottom}
|
||||
onPress={refreshMemeMetadata}>
|
||||
Refresh Meme Metadata
|
||||
</Button>
|
||||
<View style={settingsStyles.hideMediaSwitch}>
|
||||
<Text>Hide media from gallery</Text>
|
||||
@@ -131,17 +156,16 @@ const Settings = () => {
|
||||
/>
|
||||
</View>
|
||||
</List.Section>
|
||||
<List.Section>
|
||||
<List.Subheader>Database</List.Subheader>
|
||||
<Button
|
||||
mode="elevated"
|
||||
loading={isOptimizingDatabase}
|
||||
onPress={optimizeDatabase}>
|
||||
Optimize Database Now
|
||||
</Button>
|
||||
</List.Section>
|
||||
</ScrollView>
|
||||
<Portal>
|
||||
<Portal>
|
||||
<StorageLocationChangeDialog
|
||||
visible={storageLocationChangeDialogVisible}
|
||||
setVisible={setStorageLocationChangeDialogVisible}
|
||||
setSnackbarVisible={setSnackbarVisible}
|
||||
setSnackbarMessage={setSnackbarMessage}
|
||||
/>
|
||||
</Portal>
|
||||
<Snackbar
|
||||
visible={snackbarVisible}
|
||||
onDismiss={() => setSnackbarVisible(false)}
|
||||
|
Reference in New Issue
Block a user