import { RouteProp, useFocusEffect, useNavigation, useRoute, } from '@react-navigation/native' import {format} from 'date-fns' import {useCallback, useRef, useState} from 'react' import {NativeModules, TextInput, View} from 'react-native' import DocumentPicker from 'react-native-document-picker' import {Button, Card, TouchableRipple} from 'react-native-paper' import AppInput from './AppInput' import ConfirmDialog from './ConfirmDialog' import {MARGIN, PADDING} from './constants' import {getNow, setRepo, settingsRepo} from './db' import GymSet from './gym-set' import {HomePageParams} from './home-page-params' import Settings from './settings' import StackHeader from './StackHeader' import {toast} from './toast' export default function EditSet() { const {params} = useRoute>() const {set} = params const navigation = useNavigation() const [settings, setSettings] = useState({} as Settings) const [name, setName] = useState(set.name) const [reps, setReps] = useState(set.reps?.toString()) const [weight, setWeight] = useState(set.weight?.toString()) const [newImage, setNewImage] = useState(set.image) const [unit, setUnit] = useState(set.unit) const [showRemove, setShowRemove] = useState(false) const [removeImage, setRemoveImage] = useState(false) const weightRef = useRef(null) const repsRef = useRef(null) const unitRef = useRef(null) const [selection, setSelection] = useState({ start: 0, end: set.reps?.toString().length, }) useFocusEffect( useCallback(() => { settingsRepo.findOne({where: {}}).then(setSettings) }, []), ) const startTimer = useCallback( async (value: string) => { if (!settings.alarm) return const first = await setRepo.findOne({where: {name: value}}) const milliseconds = (first?.minutes ?? 3) * 60 * 1000 + (first?.seconds ?? 0) * 1000 const {vibrate, sound, noSound} = settings const args = [milliseconds, vibrate, sound, noSound] NativeModules.AlarmModule.timer(...args) }, [settings], ) const added = useCallback( async (value: GymSet) => { startTimer(value.name) console.log(`${EditSet.name}.add`, {set: value}) if (!settings.notify) return if ( value.weight > set.weight || (value.reps > set.reps && value.weight === set.weight) ) toast("Great work King! That's a new record.") }, [startTimer, set, settings], ) const handleSubmit = async () => { console.log(`${EditSet.name}.handleSubmit:`, {set, uri: newImage, name}) if (!name) return let image = newImage if (!newImage && !removeImage) image = await setRepo.findOne({where: {name}}).then(s => s?.image) console.log(`${EditSet.name}.handleSubmit:`, {image}) const [{now}] = await getNow() const saved = await setRepo.save({ id: set.id, name, created: set.created || now, reps: Number(reps), weight: Number(weight), unit, image, minutes: Number(set.minutes ?? 3), seconds: Number(set.seconds ?? 30), sets: set.sets ?? 3, hidden: false, }) if (typeof set.id !== 'number') added(saved) navigation.goBack() } const changeImage = useCallback(async () => { const {fileCopyUri} = await DocumentPicker.pickSingle({ type: DocumentPicker.types.images, copyTo: 'documentDirectory', }) if (fileCopyUri) setNewImage(fileCopyUri) }, []) const handleRemove = useCallback(async () => { setNewImage('') setRemoveImage(true) setShowRemove(false) }, []) return ( <> repsRef.current?.focus()} /> weightRef.current?.focus()} selection={selection} onSelectionChange={e => setSelection(e.nativeEvent.selection)} autoFocus={!!name} innerRef={repsRef} /> {settings.showUnit && ( )} {typeof set.id === 'number' && settings.showDate && ( )} {settings.images && newImage && ( setShowRemove(true)}> )} {settings.images && !newImage && ( )} Are you sure you want to remove the image? ) }