import React, { useState } from 'react'; import { ScrollView, StyleSheet, View } from 'react-native'; import { Button, List, Portal, SegmentedButtons, Snackbar, Switch, Text, useTheme, } from 'react-native-paper'; import { useDispatch, useSelector } from 'react-redux'; import type {} from 'redux-thunk/extend-redux'; import { RootState, setGridColumns, setMasonryColumns, setNoMedia, } 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: { paddingHorizontal: '4%', }, snackbar: { marginBottom: 90, }, marginBottom: { marginBottom: 15, }, columnSegmentedButtons: { marginBottom: 15, paddingHorizontal: '2%', }, hideMediaSwitch: { flexDirection: 'row', justifyContent: 'space-between', paddingHorizontal: '2%', }, }); const Settings = () => { const { colors } = useTheme(); const noMedia = useSelector((state: RootState) => state.settings.noMedia); const masonryColumns = useSelector( (state: RootState) => state.settings.masonryColumns, ); 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 [snackbarMessage, setSnackbarMessage] = useState(); const [ storageLocationChangeDialogVisible, setStorageLocationChangeDialogVisible, ] = useState(false); const refreshMemeMetadata = async () => { const stat = await FileSystem.statDir(storageUri); const statMap = new Map(); stat.forEach(s => statMap.set(s.filename, s)); const memes = realm.objects(Meme.schema.name); realm.write(() => { memes.forEach(meme => { const fileStat = statMap.get(meme.filename); meme.size = fileStat?.size ?? 0; }); }); setSnackbarMessage('Meme metadata refreshed.'); }; return ( <> Views Masonry Columns { void dispatch( setMasonryColumns(Number.parseInt(value) as 1 | 2 | 3 | 4), ); }} buttons={[ { label: '1', value: '1' }, { label: '2', value: '2' }, { label: '3', value: '3' }, { label: '4', value: '4' }, ]} style={settingsStyles.marginBottom} /> Grid Columns { void dispatch( setGridColumns(Number.parseInt(value) as 1 | 2 | 3 | 4), ); }} buttons={[ { label: '1', value: '1' }, { label: '2', value: '2' }, { label: '3', value: '3' }, { label: '4', value: '4' }, ]} /> Storage Hide media from gallery { void dispatch(setNoMedia(value)); }} /> setSnackbarMessage(undefined)} style={settingsStyles.snackbar} action={{ label: 'Dismiss', // eslint-disable-next-line unicorn/no-useless-undefined onPress: () => setSnackbarMessage(undefined), }}> {snackbarMessage} ); }; export default Settings;