2022-10-31 04:22:08 +00:00
|
|
|
import {useFocusEffect} from '@react-navigation/native'
|
2022-11-12 01:38:39 +00:00
|
|
|
import {format} from 'date-fns'
|
2022-11-01 05:58:09 +00:00
|
|
|
import {useCallback, useMemo, useState} from 'react'
|
2022-11-14 08:42:37 +00:00
|
|
|
import {
|
|
|
|
DeviceEventEmitter,
|
|
|
|
FlatList,
|
|
|
|
NativeModules,
|
|
|
|
Platform,
|
|
|
|
View,
|
|
|
|
} from 'react-native'
|
2022-10-31 04:22:08 +00:00
|
|
|
import DocumentPicker from 'react-native-document-picker'
|
2022-11-12 03:02:16 +00:00
|
|
|
import {Button} from 'react-native-paper'
|
2022-10-31 04:22:08 +00:00
|
|
|
import {darkColors, lightColors} from './colors'
|
|
|
|
import ConfirmDialog from './ConfirmDialog'
|
|
|
|
import {MARGIN} from './constants'
|
|
|
|
import {settingsRepo} from './db'
|
|
|
|
import DrawerHeader from './DrawerHeader'
|
|
|
|
import Input from './input'
|
|
|
|
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'
|
2022-11-01 02:55:37 +00:00
|
|
|
import {toast} from './toast'
|
2022-11-01 05:58:09 +00:00
|
|
|
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']
|
|
|
|
|
2022-07-08 03:20:03 +00:00
|
|
|
export default function SettingsPage() {
|
2022-10-31 04:22:08 +00:00
|
|
|
const [battery, setBattery] = useState(false)
|
|
|
|
const [ignoring, setIgnoring] = useState(false)
|
|
|
|
const [term, setTerm] = useState('')
|
2022-11-01 05:58:09 +00:00
|
|
|
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')
|
2022-11-01 05:58:09 +00:00
|
|
|
const {theme, setTheme, color, setColor} = 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)
|
2022-11-12 01:38:39 +00:00
|
|
|
const today = new Date()
|
2022-10-18 08:43:46 +00:00
|
|
|
|
2022-09-21 01:48:45 +00:00
|
|
|
useFocusEffect(
|
|
|
|
useCallback(() => {
|
2022-11-01 05:58:09 +00:00
|
|
|
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)
|
2022-11-01 05:58:09 +00:00
|
|
|
})
|
2022-11-14 08:42:37 +00:00
|
|
|
if (Platform.OS !== 'android') return
|
2022-11-21 05:15:43 +00:00
|
|
|
NativeModules.SettingsModule.ignoringBattery(setIgnoring)
|
|
|
|
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-09-21 01:48:45 +00:00
|
|
|
}, []),
|
2022-10-31 04:22:08 +00:00
|
|
|
)
|
2022-07-03 01:50:01 +00:00
|
|
|
|
2022-07-09 07:39:11 +00:00
|
|
|
const changeAlarmEnabled = useCallback(
|
|
|
|
(enabled: boolean) => {
|
2022-11-01 02:55:37 +00:00
|
|
|
if (enabled)
|
|
|
|
DeviceEventEmitter.emit('toast', {
|
|
|
|
value: 'Timers will now run after each set',
|
|
|
|
timeout: 4000,
|
|
|
|
})
|
|
|
|
else toast('Stopped timers running after each set.')
|
2022-10-31 04:22:08 +00:00
|
|
|
if (enabled && !ignoring) setBattery(true)
|
2022-11-01 05:58:09 +00:00
|
|
|
setAlarm(enabled)
|
|
|
|
settingsRepo.update({}, {alarm: enabled})
|
2022-07-09 07:39:11 +00:00
|
|
|
},
|
2022-11-01 05:58:09 +00:00
|
|
|
[setBattery, ignoring],
|
2022-10-31 04:22:08 +00:00
|
|
|
)
|
2022-07-06 09:03:56 +00:00
|
|
|
|
2022-11-01 05:58:09 +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})
|
|
|
|
}, [])
|
2022-07-10 07:03:07 +00:00
|
|
|
|
2022-08-25 08:41:01 +00:00
|
|
|
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})
|
2022-11-01 05:58:09 +00:00
|
|
|
setSound(fileCopyUri)
|
2022-11-01 02:55:37 +00:00
|
|
|
toast('This song will now play after rest timers complete.')
|
2022-11-01 05:58:09 +00:00
|
|
|
}, [])
|
|
|
|
|
|
|
|
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-10-27 04:28:27 +00:00
|
|
|
|
2022-11-07 01:30:25 +00:00
|
|
|
const switches: Input<boolean>[] = useMemo(
|
|
|
|
() =>
|
|
|
|
[
|
|
|
|
{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())),
|
|
|
|
[
|
|
|
|
term,
|
|
|
|
showDate,
|
|
|
|
changeShowDate,
|
|
|
|
alarm,
|
|
|
|
changeAlarmEnabled,
|
|
|
|
vibrate,
|
|
|
|
changeVibrate,
|
|
|
|
noSound,
|
|
|
|
changeNoSound,
|
|
|
|
notify,
|
|
|
|
changeNotify,
|
|
|
|
images,
|
|
|
|
changeImages,
|
|
|
|
showUnit,
|
|
|
|
changeUnit,
|
|
|
|
steps,
|
|
|
|
changeSteps,
|
|
|
|
],
|
|
|
|
)
|
2022-08-23 00:04:52 +00:00
|
|
|
|
2022-10-18 08:43:46 +00:00
|
|
|
const changeTheme = useCallback(
|
|
|
|
(value: string) => {
|
2022-10-31 04:22:08 +00:00
|
|
|
settingsRepo.update({}, {theme: value})
|
2022-11-01 05:58:09 +00:00
|
|
|
setTheme(value)
|
2022-10-18 08:43:46 +00:00
|
|
|
},
|
2022-11-01 05:58:09 +00:00
|
|
|
[setTheme],
|
2022-10-31 04:22:08 +00:00
|
|
|
)
|
2022-10-18 08:43:46 +00:00
|
|
|
|
2022-11-01 05:58:09 +00:00
|
|
|
const changeDate = useCallback((value: string) => {
|
|
|
|
settingsRepo.update({}, {date: value})
|
|
|
|
setDate(value)
|
|
|
|
}, [])
|
2022-10-18 08:43:46 +00:00
|
|
|
|
2022-11-01 05:58:09 +00:00
|
|
|
const soundString = useMemo(() => {
|
|
|
|
if (!sound) return null
|
|
|
|
const split = sound.split('/')
|
2022-11-01 03:06:25 +00:00
|
|
|
return ': ' + split.pop()
|
2022-11-01 05:58:09 +00:00
|
|
|
}, [sound])
|
2022-10-31 08:32:33 +00:00
|
|
|
|
2022-11-01 02:55:37 +00:00
|
|
|
const changeColor = useCallback(
|
|
|
|
(value: string) => {
|
2022-11-01 05:58:09 +00:00
|
|
|
setColor(value)
|
2022-11-01 02:55:37 +00:00
|
|
|
settingsRepo.update({}, {color: value})
|
|
|
|
},
|
2022-11-01 05:58:09 +00:00
|
|
|
[setColor],
|
2022-11-01 02:55:37 +00:00
|
|
|
)
|
2022-10-31 08:00:10 +00:00
|
|
|
|
2022-11-07 01:30:25 +00:00
|
|
|
const renderItem = useCallback(
|
|
|
|
({item}: {item: Input<boolean>}) => (
|
|
|
|
<Switch
|
|
|
|
onPress={() => item.onChange(!item.value)}
|
|
|
|
key={item.name}
|
|
|
|
value={item.value}
|
|
|
|
onValueChange={item.onChange}>
|
|
|
|
{item.name}
|
|
|
|
</Switch>
|
|
|
|
),
|
|
|
|
[],
|
|
|
|
)
|
|
|
|
|
2022-08-20 04:37:59 +00:00
|
|
|
return (
|
2022-10-22 23:35:58 +00:00
|
|
|
<>
|
2022-10-23 06:13:58 +00:00
|
|
|
<DrawerHeader name="Settings" />
|
2022-10-28 05:59:54 +00:00
|
|
|
<Page term={term} search={setTerm}>
|
2022-11-07 01:30:25 +00:00
|
|
|
<View>
|
|
|
|
<FlatList
|
2022-11-16 05:32:59 +00:00
|
|
|
style={{marginTop: MARGIN}}
|
2022-11-07 01:30:25 +00:00
|
|
|
data={switches}
|
|
|
|
renderItem={renderItem}
|
|
|
|
/>
|
2022-11-01 05:58:09 +00:00
|
|
|
{'theme'.includes(term.toLowerCase()) && (
|
2022-11-15 06:51:15 +00:00
|
|
|
<Select
|
2022-11-21 04:54:45 +00:00
|
|
|
value={theme}
|
2022-11-15 06:51:15 +00:00
|
|
|
onChange={changeTheme}
|
|
|
|
items={[
|
2022-11-16 04:07:43 +00:00
|
|
|
{label: 'Follow system theme', value: 'system'},
|
|
|
|
{label: 'Dark theme', value: 'dark'},
|
|
|
|
{label: 'Light theme', value: 'light'},
|
2022-11-15 06:51:15 +00:00
|
|
|
]}
|
|
|
|
/>
|
2022-11-01 05:58:09 +00:00
|
|
|
)}
|
2022-11-16 04:07:43 +00:00
|
|
|
{'date format'.includes(term.toLowerCase()) && (
|
|
|
|
<Select
|
|
|
|
value={date}
|
|
|
|
onChange={changeDate}
|
|
|
|
items={formatOptions.map(option => ({
|
2022-11-16 05:17:59 +00:00
|
|
|
label: `Date format: ${format(today, option)}`,
|
2022-11-16 04:07:43 +00:00
|
|
|
value: option,
|
|
|
|
}))}
|
|
|
|
/>
|
|
|
|
)}
|
2022-10-28 05:59:54 +00:00
|
|
|
{'color'.includes(term.toLowerCase()) && (
|
2022-11-15 06:51:15 +00:00
|
|
|
<Select
|
|
|
|
value={color}
|
|
|
|
onChange={changeColor}
|
|
|
|
items={lightColors.concat(darkColors).map(colorOption => ({
|
|
|
|
label: 'Primary color',
|
|
|
|
value: colorOption,
|
|
|
|
color: colorOption,
|
|
|
|
}))}
|
|
|
|
/>
|
2022-10-22 23:35:58 +00:00
|
|
|
)}
|
2022-10-28 05:59:54 +00:00
|
|
|
{'alarm sound'.includes(term.toLowerCase()) && (
|
2022-11-16 04:10:31 +00:00
|
|
|
<Button
|
|
|
|
style={{alignSelf: 'flex-start', marginTop: MARGIN}}
|
|
|
|
onPress={changeSound}>
|
2022-11-01 05:58:09 +00:00
|
|
|
Alarm sound{soundString}
|
2022-10-22 23:35:58 +00:00
|
|
|
</Button>
|
|
|
|
)}
|
2022-11-07 01:30:25 +00:00
|
|
|
</View>
|
2022-10-22 23:35:58 +00:00
|
|
|
<ConfirmDialog
|
|
|
|
title="Battery optimizations"
|
|
|
|
show={battery}
|
|
|
|
setShow={setBattery}
|
|
|
|
onOk={() => {
|
2022-11-21 05:15:43 +00:00
|
|
|
NativeModules.SettingsModule.ignoreBattery()
|
2022-10-31 04:22:08 +00:00
|
|
|
setBattery(false)
|
2022-10-22 23:35:58 +00:00
|
|
|
}}>
|
|
|
|
Disable battery optimizations for Massive to use rest timers.
|
|
|
|
</ConfirmDialog>
|
|
|
|
</Page>
|
|
|
|
</>
|
2022-10-31 04:22:08 +00:00
|
|
|
)
|
2022-07-03 01:50:01 +00:00
|
|
|
}
|