Massive/SettingsPage.tsx

333 lines
10 KiB
TypeScript
Raw Normal View History

import {
NavigationProp,
useFocusEffect,
useNavigation,
} from '@react-navigation/native'
import {format} from 'date-fns'
import {useCallback, useMemo, useState} from 'react'
import {DeviceEventEmitter, NativeModules, Platform, View} from 'react-native'
2022-10-31 04:22:08 +00:00
import DocumentPicker from 'react-native-document-picker'
import {Dirs, FileSystem} from 'react-native-file-access'
2022-11-30 02:15:19 +00:00
import {Button, Subheading} from 'react-native-paper'
import ConfirmDialog from './ConfirmDialog'
2022-12-01 02:45:18 +00:00
import {ITEM_PADDING, MARGIN} from './constants'
import {AppDataSource} from './data-source'
import {setRepo, settingsRepo} from './db'
import {DrawerParamList} from './drawer-param-list'
2022-10-31 04:22:08 +00:00
import DrawerHeader from './DrawerHeader'
import Input from './input'
2022-12-01 02:45:18 +00:00
import {darkOptions, lightOptions, themeOptions} from './options'
2022-10-31 04:22:08 +00:00
import Page from './Page'
2022-11-01 03:06:25 +00:00
import Select from './Select'
2022-10-31 04:22:08 +00:00
import Switch from './Switch'
import {toast} from './toast'
import {useTheme} from './use-theme'
2022-07-08 03:45:24 +00:00
2022-11-21 05:15:43 +00:00
const defaultFormats = ['P', 'Pp', 'ccc p', 'p']
export default function SettingsPage() {
2022-10-31 04:22:08 +00:00
const [ignoring, setIgnoring] = useState(false)
const [term, setTerm] = useState('')
const [vibrate, setVibrate] = useState(false)
const [alarm, setAlarm] = useState(false)
const [sound, setSound] = useState('')
const [notify, setNotify] = useState(false)
const [images, setImages] = useState(false)
const [showUnit, setShowUnit] = useState(false)
const [steps, setSteps] = useState(false)
2022-11-14 01:27:50 +00:00
const [date, setDate] = useState('P')
const {theme, setTheme, lightColor, setLightColor, darkColor, setDarkColor} =
useTheme()
const [showDate, setShowDate] = useState(false)
const [noSound, setNoSound] = useState(false)
2022-11-21 05:15:43 +00:00
const [formatOptions, setFormatOptions] = useState<string[]>(defaultFormats)
const [importing, setImporting] = useState(false)
const {reset} = useNavigation<NavigationProp<DrawerParamList>>()
const today = new Date()
useFocusEffect(
useCallback(() => {
settingsRepo.findOne({where: {}}).then(settings => {
setAlarm(settings.alarm)
setVibrate(settings.vibrate)
setSound(settings.sound)
setNotify(settings.notify)
setImages(settings.images)
setShowUnit(settings.showUnit)
setSteps(settings.steps)
setDate(settings.date)
setShowDate(settings.showDate)
2022-11-03 08:58:33 +00:00
setNoSound(settings.noSound)
NativeModules.SettingsModule.ignoringBattery((isIgnoring: boolean) => {
if (!isIgnoring && settings.alarm) setAlarm(false)
setIgnoring(isIgnoring)
})
})
2022-11-21 05:15:43 +00:00
NativeModules.SettingsModule.is24().then((is24: boolean) => {
console.log(`${SettingsPage.name}.focus:`, {is24})
if (is24) setFormatOptions(['P', 'P, k:m', 'ccc k:m', 'k:m'])
else setFormatOptions(defaultFormats)
})
}, []),
2022-10-31 04:22:08 +00:00
)
2022-07-03 01:50:01 +00:00
2022-12-28 01:15:02 +00:00
const soundString = useMemo(() => {
if (!sound) return null
const split = sound.split('/')
return split.pop()
}, [sound])
const changeAlarmEnabled = useCallback(
(enabled: boolean) => {
if (enabled)
DeviceEventEmitter.emit('toast', {
value: 'Timers will now run after each set',
timeout: 4000,
})
else toast('Stopped timers running after each set.')
if (enabled && !ignoring) NativeModules.SettingsModule.ignoreBattery()
setAlarm(enabled)
settingsRepo.update({}, {alarm: enabled})
},
[ignoring],
2022-10-31 04:22:08 +00:00
)
const changeVibrate = useCallback((enabled: boolean) => {
if (enabled) toast('When a timer completes, vibrate your phone.')
else toast('Stop vibrating at the end of timers.')
setVibrate(enabled)
settingsRepo.update({}, {vibrate: enabled})
}, [])
const changeSound = useCallback(async () => {
const {fileCopyUri} = await DocumentPicker.pickSingle({
type: 'audio/*',
copyTo: 'documentDirectory',
2022-10-31 04:22:08 +00:00
})
if (!fileCopyUri) return
settingsRepo.update({}, {sound: fileCopyUri})
setSound(fileCopyUri)
2022-12-28 01:15:02 +00:00
toast('Sound will play after rest timers.')
}, [])
const changeNotify = useCallback((enabled: boolean) => {
setNotify(enabled)
settingsRepo.update({}, {notify: enabled})
if (enabled) toast('Show when a set is a new record.')
else toast('Stopped showing notifications for new records.')
}, [])
const changeImages = useCallback((enabled: boolean) => {
setImages(enabled)
settingsRepo.update({}, {images: enabled})
if (enabled) toast('Show images for sets.')
else toast('Stopped showing images for sets.')
}, [])
const changeUnit = useCallback((enabled: boolean) => {
setShowUnit(enabled)
settingsRepo.update({}, {showUnit: enabled})
if (enabled) toast('Show option to select unit for sets.')
else toast('Hid unit option for sets.')
}, [])
const changeSteps = useCallback((enabled: boolean) => {
setSteps(enabled)
settingsRepo.update({}, {steps: enabled})
if (enabled) toast('Show steps for a workout.')
else toast('Stopped showing steps for workouts.')
}, [])
const changeShowDate = useCallback((enabled: boolean) => {
setShowDate(enabled)
settingsRepo.update({}, {showDate: enabled})
if (enabled) toast('Show date for sets by default.')
else toast('Stopped showing date for sets by default.')
}, [])
const changeNoSound = useCallback((enabled: boolean) => {
setNoSound(enabled)
settingsRepo.update({}, {noSound: enabled})
if (enabled) toast('Disable sound on rest timer alarms.')
else toast('Enabled sound for rest timer alarms.')
}, [])
2022-12-01 02:45:18 +00:00
const switches: Input<boolean>[] = [
{name: 'Rest timers', value: alarm, onChange: changeAlarmEnabled},
{name: 'Vibrate', value: vibrate, onChange: changeVibrate},
{name: 'Disable sound', value: noSound, onChange: changeNoSound},
{name: 'Notifications', value: notify, onChange: changeNotify},
{name: 'Show images', value: images, onChange: changeImages},
{name: 'Show unit', value: showUnit, onChange: changeUnit},
{name: 'Show steps', value: steps, onChange: changeSteps},
{name: 'Show date', value: showDate, onChange: changeShowDate},
].filter(({name}) => name.toLowerCase().includes(term.toLowerCase()))
2022-08-23 00:04:52 +00:00
const changeTheme = useCallback(
(value: string) => {
2022-10-31 04:22:08 +00:00
settingsRepo.update({}, {theme: value})
setTheme(value)
},
[setTheme],
2022-10-31 04:22:08 +00:00
)
const changeDate = useCallback((value: string) => {
settingsRepo.update({}, {date: value})
setDate(value)
toast('Changed date format.')
}, [])
const changeDarkColor = useCallback(
(value: string) => {
setDarkColor(value)
settingsRepo.update({}, {darkColor: value})
toast('Set primary color for dark mode.')
},
[setDarkColor],
)
const changeLightColor = useCallback(
(value: string) => {
setLightColor(value)
settingsRepo.update({}, {lightColor: value})
toast('Set primary color for light mode.')
},
[setLightColor],
)
2022-10-31 08:00:10 +00:00
2022-12-01 02:45:18 +00:00
const renderSwitch = useCallback(
(item: Input<boolean>) => (
2022-12-24 06:55:38 +00:00
<Switch key={item.name} value={item.value} onChange={item.onChange}>
{item.name}
</Switch>
),
[],
)
2022-12-01 02:45:18 +00:00
const selects: Input<string>[] = [
{name: 'Theme', value: theme, onChange: changeTheme, items: themeOptions},
{
name: 'Dark color',
value: darkColor,
onChange: changeDarkColor,
items: lightOptions,
},
{
name: 'Light color',
value: lightColor,
onChange: changeLightColor,
items: darkOptions,
},
{
name: 'Date format',
value: date,
onChange: changeDate,
items: formatOptions.map(option => ({
label: format(today, option),
value: option,
})),
},
].filter(({name}) => name.toLowerCase().includes(term.toLowerCase()))
const renderSelect = useCallback(
(item: Input<string>) => (
2022-12-01 02:45:18 +00:00
<Select
2022-12-24 00:18:03 +00:00
key={item.name}
2022-12-01 02:45:18 +00:00
value={item.value}
onChange={item.onChange}
label={item.name}
items={item.items}
/>
),
[],
)
const confirmImport = useCallback(async () => {
setImporting(false)
await AppDataSource.destroy()
const result = await DocumentPicker.pickSingle()
await FileSystem.cp(result.uri, Dirs.DatabaseDir + '/massive.db')
await AppDataSource.initialize()
await setRepo.createQueryBuilder().update().set({image: null}).execute()
await settingsRepo
.createQueryBuilder()
.update()
2022-12-10 09:22:51 +00:00
.set({sound: null})
.execute()
reset({index: 0, routes: [{name: 'Settings'}]})
}, [reset])
const exportDatabase = useCallback(async () => {
const path = Dirs.DatabaseDir + '/massive.db'
await FileSystem.cpExternal(path, 'massive.db', 'downloads')
toast('Database exported. Check downloads.')
}, [])
2022-12-24 00:36:11 +00:00
const buttons = useMemo(
() =>
[
{
name: 'Alarm sound',
element: (
<View
key="alarm-sound"
style={{
flexDirection: 'row',
alignItems: 'center',
paddingLeft: ITEM_PADDING,
}}>
<Subheading style={{width: 100}}>Alarm sound</Subheading>
<Button onPress={changeSound}>{soundString || 'Default'}</Button>
</View>
),
},
{
name: 'Export database',
element: (
<Button
key="export-db"
style={{alignSelf: 'flex-start'}}
onPress={exportDatabase}>
Export database
</Button>
),
},
{
name: 'Import database',
element: (
<Button
key="import-db"
style={{alignSelf: 'flex-start'}}
onPress={() => setImporting(true)}>
Import database
</Button>
),
},
].filter(({name}) => name.toLowerCase().includes(term.toLowerCase())),
[changeSound, exportDatabase, soundString, term],
2022-12-24 00:36:11 +00:00
)
return (
<>
<DrawerHeader name="Settings" />
<Page term={term} search={setTerm} style={{flexGrow: 0}}>
<View style={{marginTop: MARGIN}}>
{switches.map(s => renderSwitch(s))}
{selects.map(s => renderSelect(s))}
{buttons.map(b => b.element)}
2022-12-24 00:36:11 +00:00
</View>
</Page>
<ConfirmDialog
title="Are you sure?"
onOk={confirmImport}
setShow={setImporting}
show={importing}>
Importing a database overwrites your current data. This action cannot be
reversed!
</ConfirmDialog>
</>
2022-10-31 04:22:08 +00:00
)
2022-07-03 01:50:01 +00:00
}