diff --git a/PlanItem.tsx b/PlanItem.tsx index 2dcd173..26cce4f 100644 --- a/PlanItem.tsx +++ b/PlanItem.tsx @@ -1,10 +1,12 @@ import {NavigationProp, useNavigation} from '@react-navigation/native'; -import React, {useCallback, useState} from 'react'; -import {GestureResponderEvent} from 'react-native'; +import React, {useCallback, useMemo, useState} from 'react'; +import {GestureResponderEvent, Text} from 'react-native'; import {List, Menu} from 'react-native-paper'; +import {getBestSet} from './best.service'; import {Plan} from './plan'; import {PlanPageParams} from './plan-page-params'; import {deletePlan} from './plan.service'; +import {DAYS} from './time'; export default function PlanItem({ item, @@ -15,7 +17,9 @@ export default function PlanItem({ }) { const [show, setShow] = useState(false); const [anchor, setAnchor] = useState({x: 0, y: 0}); + const days = useMemo(() => item.days.split(','), [item.days]); const navigation = useNavigation>(); + const today = useMemo(() => DAYS[new Date().getDay()], []); const remove = useCallback(async () => { if (typeof item.id === 'number') await deletePlan(item.id); @@ -23,6 +27,14 @@ export default function PlanItem({ onRemove(); }, [setShow, item.id, onRemove]); + const start = useCallback(async () => { + const workouts = item.workouts.split(','); + const first = workouts[0]; + const set = await getBestSet(first); + setShow(false); + navigation.navigate('StartPlan', {plan: item, set}); + }, [item, navigation]); + const longPress = useCallback( (e: GestureResponderEvent) => { setAnchor({x: e.nativeEvent.pageX, y: e.nativeEvent.pageY}); @@ -31,19 +43,33 @@ export default function PlanItem({ [setAnchor, setShow], ); + const edit = useCallback(() => { + setShow(false); + navigation.navigate('EditPlan', {plan: item}); + }, [navigation, item]); + return ( <> navigation.navigate('EditPlan', {plan: item})} - title={ - item.days - ? item.days.replace(/,/g, ', ') - : item.workouts.replace(/,/g, ', ') - } + onPress={start} + title={days.map((day, index) => ( + + {day === today ? ( + + {day} + + ) : ( + day + )} + {index === days.length - 1 ? '' : ', '} + + ))} description={item.days ? item.workouts.replace(/,/g, ', ') : null} onLongPress={longPress} right={() => ( setShow(false)}> + )} diff --git a/PlanPage.tsx b/PlanPage.tsx index 8a08823..d093b12 100644 --- a/PlanPage.tsx +++ b/PlanPage.tsx @@ -7,6 +7,7 @@ import {DrawerParamList} from './drawer-param-list'; import EditPlan from './EditPlan'; import {PlanPageParams} from './plan-page-params'; import PlanList from './PlanList'; +import StartPlan from './StartPlan'; const Stack = createStackNavigator(); @@ -31,6 +32,20 @@ export default function PlanPage() { }, }} /> + { + navigation.setOptions({ + headerLeft: () => ( + + ), + title: 'Plans', + }); + }, + }} + /> ); } diff --git a/Routes.tsx b/Routes.tsx index b34bcb8..c6cd3ff 100644 --- a/Routes.tsx +++ b/Routes.tsx @@ -6,7 +6,6 @@ import {DrawerParamList} from './drawer-param-list'; import HomePage from './HomePage'; import PlanPage from './PlanPage'; import Route from './route'; -import SessionPage from './SessionPage'; import SettingsPage from './SettingsPage'; import useDark from './use-dark'; import WorkoutsPage from './WorkoutsPage'; @@ -19,7 +18,6 @@ export default function Routes() { const routes: Route[] = [ {name: 'Home', component: HomePage, icon: 'home'}, {name: 'Plans', component: PlanPage, icon: 'event'}, - {name: 'Session', component: SessionPage, icon: 'directions-run'}, {name: 'Best', component: BestPage, icon: 'insights'}, {name: 'Workouts', component: WorkoutsPage, icon: 'fitness-center'}, {name: 'Settings', component: SettingsPage, icon: 'settings'}, diff --git a/SessionList.tsx b/SessionList.tsx deleted file mode 100644 index f53c4b0..0000000 --- a/SessionList.tsx +++ /dev/null @@ -1,71 +0,0 @@ -import { - NavigationProp, - useFocusEffect, - useNavigation, -} from '@react-navigation/native'; -import React, {useCallback, useEffect, useState} from 'react'; -import {FlatList} from 'react-native'; -import {List} from 'react-native-paper'; -import {getBestSet} from './best.service'; -import Page from './Page'; -import {Plan} from './plan'; -import {getPlans} from './plan.service'; -import {SessionPageParams} from './session-page-params'; - -export default function SessionList() { - const [search, setSearch] = useState(''); - const [plans, setPlans] = useState([]); - const navigation = useNavigation>(); - - const refresh = useCallback(async () => { - getPlans(search).then(setPlans); - }, [search]); - - useFocusEffect( - useCallback(() => { - refresh(); - }, [refresh]), - ); - - useEffect(() => { - refresh(); - }, [search, refresh]); - - const press = useCallback( - async (item: Plan) => { - const workouts = item.workouts.split(','); - const first = workouts[0]; - const set = await getBestSet(first); - navigation.navigate('StartSession', {plan: item, set}); - }, - [navigation], - ); - - const renderItem = useCallback( - ({item}: {item: Plan}) => ( - press(item)} - /> - ), - [press], - ); - - return ( - - set.id?.toString() || ''} - ListEmptyComponent={ - - } - /> - - ); -} diff --git a/SessionPage.tsx b/SessionPage.tsx deleted file mode 100644 index 3bfe1d2..0000000 --- a/SessionPage.tsx +++ /dev/null @@ -1,36 +0,0 @@ -import {DrawerNavigationProp} from '@react-navigation/drawer'; -import {useNavigation} from '@react-navigation/native'; -import {createStackNavigator} from '@react-navigation/stack'; -import React from 'react'; -import {IconButton} from 'react-native-paper'; -import {DrawerParamList} from './drawer-param-list'; -import {SessionPageParams} from './session-page-params'; -import SessionList from './SessionList'; -import StartSession from './StartSession'; - -const Stack = createStackNavigator(); - -export default function SessionPage() { - const navigation = useNavigation>(); - - return ( - - - { - navigation.setOptions({ - headerLeft: () => ( - - ), - title: 'Session', - }); - }, - }} - /> - - ); -} diff --git a/StartSession.tsx b/StartPlan.tsx similarity index 64% rename from StartSession.tsx rename to StartPlan.tsx index e10aa25..0a7dd1c 100644 --- a/StartSession.tsx +++ b/StartPlan.tsx @@ -12,13 +12,13 @@ import {MARGIN, PADDING} from './constants'; import CountMany from './count-many'; import MassiveInput from './MassiveInput'; import {SnackbarContext} from './MassiveSnack'; -import {SessionPageParams} from './session-page-params'; +import {PlanPageParams} from './plan-page-params'; import {addSet, countManyToday} from './set.service'; import SetForm from './SetForm'; import {useSettings} from './use-settings'; -export default function StartSession() { - const {params} = useRoute>(); +export default function StartPlan() { + const {params} = useRoute>(); const {set} = params; const [name, setName] = useState(set.name); const [reps, setReps] = useState(set.reps.toString()); @@ -64,6 +64,7 @@ export default function StartSession() { unit, }); countManyToday().then(setCounts); + toast('Saved workout', 3000); if (!settings.alarm) return; const milliseconds = Number(minutes) * 60 * 1000 + Number(seconds) * 1000; const args = [milliseconds, !!settings.vibrate, settings.sound]; @@ -81,11 +82,11 @@ export default function StartSession() { const select = useCallback( async (index: number) => { - console.log(`${StartSession.name}.next:`, {name, workouts}); + console.log(`${StartPlan.name}.next:`, {name, workouts}); const workout = workouts[index]; - console.log(`${StartSession.name}.next:`, {workout}); + console.log(`${StartPlan.name}.next:`, {workout}); const best = await getBestSet(workout); - console.log(`${StartSession.name}.next:`, {best}); + console.log(`${StartPlan.name}.next:`, {best}); setMinutes(best.minutes); setSeconds(best.seconds); setName(best.name); @@ -97,47 +98,50 @@ export default function StartSession() { ); return ( - - weightRef.current?.focus()} - selection={selection} - onSelectionChange={e => setSelection(e.nativeEvent.selection)} - autoFocus - innerRef={repsRef} - /> - - {!!settings.showUnit && ( + + weightRef.current?.focus()} + selection={selection} + onSelectionChange={e => setSelection(e.nativeEvent.selection)} + autoFocus + innerRef={repsRef} /> - )} - - {workouts.map((workout, index) => ( - select(index)} - style={{marginBottom: MARGIN, marginRight: MARGIN}}> - {workout} x - {counts?.find(count => count.name === workout)?.total || 0} - - ))} + + {!!settings.showUnit && ( + + )} + + {workouts.map((workout, index) => ( + select(index)} + style={{marginBottom: MARGIN, marginRight: MARGIN}}> + {workout} x + {counts?.find(count => count.name === workout)?.total || 0} + + ))} +