parent
2be2893dc1
commit
3cbabb723a
|
@ -18,7 +18,7 @@ import {useSettings} from './use-settings';
|
|||
|
||||
export default function EditSet() {
|
||||
const {params} = useRoute<RouteProp<HomePageParams, 'EditSet'>>();
|
||||
const {set, count, workouts} = params;
|
||||
const {set} = params;
|
||||
const navigation = useNavigation();
|
||||
const {toast} = useContext(SnackbarContext);
|
||||
const {settings, setSettings} = useSettings();
|
||||
|
@ -28,8 +28,6 @@ export default function EditSet() {
|
|||
console.log(`${EditSet.name}.focus:`, set);
|
||||
let title = 'Create set';
|
||||
if (typeof set.id === 'number') title = 'Edit set';
|
||||
else if (Number(set.sets) > 0 && settings.newSet === 'predict')
|
||||
title = `${set.name} (${count + 1} / ${set.sets})`;
|
||||
navigation.getParent()?.setOptions({
|
||||
headerLeft: () => (
|
||||
<IconButton icon="arrow-back" onPress={() => navigation.goBack()} />
|
||||
|
@ -37,7 +35,7 @@ export default function EditSet() {
|
|||
headerRight: null,
|
||||
title,
|
||||
});
|
||||
}, [navigation, set, count, settings.newSet]),
|
||||
}, [navigation, set]),
|
||||
);
|
||||
|
||||
const startTimer = useCallback(
|
||||
|
@ -93,7 +91,7 @@ export default function EditSet() {
|
|||
|
||||
return (
|
||||
<View style={{padding: PADDING}}>
|
||||
<SetForm save={save} set={set} workouts={workouts} />
|
||||
<SetForm save={save} set={set} />
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ 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';
|
||||
|
@ -18,6 +19,7 @@ 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'},
|
||||
|
|
71
SessionList.tsx
Normal file
71
SessionList.tsx
Normal file
|
@ -0,0 +1,71 @@
|
|||
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<Plan[]>([]);
|
||||
const navigation = useNavigation<NavigationProp<SessionPageParams>>();
|
||||
|
||||
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}) => (
|
||||
<List.Item
|
||||
title={item.days.replace(/,/g, ', ')}
|
||||
description={item.workouts.replace(/,/g, ', ')}
|
||||
onPress={() => press(item)}
|
||||
/>
|
||||
),
|
||||
[press],
|
||||
);
|
||||
|
||||
return (
|
||||
<Page search={search} setSearch={setSearch}>
|
||||
<FlatList
|
||||
style={{height: '99%'}}
|
||||
data={plans}
|
||||
renderItem={renderItem}
|
||||
keyExtractor={set => set.id?.toString() || ''}
|
||||
ListEmptyComponent={
|
||||
<List.Item
|
||||
title="No plans yet"
|
||||
description="After making a plan, you can use it here."
|
||||
/>
|
||||
}
|
||||
/>
|
||||
</Page>
|
||||
);
|
||||
}
|
36
SessionPage.tsx
Normal file
36
SessionPage.tsx
Normal file
|
@ -0,0 +1,36 @@
|
|||
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<SessionPageParams>();
|
||||
|
||||
export default function SessionPage() {
|
||||
const navigation = useNavigation<DrawerNavigationProp<DrawerParamList>>();
|
||||
|
||||
return (
|
||||
<Stack.Navigator
|
||||
screenOptions={{headerShown: false, animationEnabled: false}}>
|
||||
<Stack.Screen name="SessionList" component={SessionList} />
|
||||
<Stack.Screen
|
||||
name="StartSession"
|
||||
component={StartSession}
|
||||
listeners={{
|
||||
beforeRemove: () => {
|
||||
navigation.setOptions({
|
||||
headerLeft: () => (
|
||||
<IconButton icon="menu" onPress={navigation.openDrawer} />
|
||||
),
|
||||
title: 'Session',
|
||||
});
|
||||
},
|
||||
}}
|
||||
/>
|
||||
</Stack.Navigator>
|
||||
);
|
||||
}
|
28
SetForm.tsx
28
SetForm.tsx
|
@ -1,7 +1,7 @@
|
|||
import React, {useCallback, useContext, useRef, useState} from 'react';
|
||||
import {ScrollView, TextInput, View} from 'react-native';
|
||||
import {TextInput} from 'react-native';
|
||||
import DocumentPicker from 'react-native-document-picker';
|
||||
import {Button, Card, Text, TouchableRipple} from 'react-native-paper';
|
||||
import {Button, Card, TouchableRipple} from 'react-native-paper';
|
||||
import ConfirmDialog from './ConfirmDialog';
|
||||
import {MARGIN} from './constants';
|
||||
import MassiveInput from './MassiveInput';
|
||||
|
@ -13,11 +13,11 @@ import {useSettings} from './use-settings';
|
|||
export default function SetForm({
|
||||
save,
|
||||
set,
|
||||
workouts,
|
||||
next,
|
||||
}: {
|
||||
set: Set;
|
||||
save: (set: Set) => void;
|
||||
workouts: string[];
|
||||
next?: () => void;
|
||||
}) {
|
||||
const [name, setName] = useState(set.name);
|
||||
const [reps, setReps] = useState(set.reps.toString());
|
||||
|
@ -86,7 +86,6 @@ export default function SetForm({
|
|||
|
||||
return (
|
||||
<>
|
||||
<ScrollView style={{height: '90%'}}>
|
||||
<MassiveInput
|
||||
label="Name"
|
||||
value={name}
|
||||
|
@ -123,23 +122,6 @@ export default function SetForm({
|
|||
innerRef={unitRef}
|
||||
/>
|
||||
)}
|
||||
{workouts.length > 0 && !!settings.workouts && (
|
||||
<View style={{flexDirection: 'row', marginBottom: MARGIN}}>
|
||||
{workouts.map((workout, index) => (
|
||||
<Text key={workout}>
|
||||
<Text
|
||||
style={
|
||||
workout === name
|
||||
? {textDecorationLine: 'underline', fontWeight: 'bold'}
|
||||
: null
|
||||
}>
|
||||
{workout}
|
||||
</Text>
|
||||
{index === workouts.length - 1 ? '' : ', '}
|
||||
</Text>
|
||||
))}
|
||||
</View>
|
||||
)}
|
||||
{!!settings.images && newImage && (
|
||||
<TouchableRipple
|
||||
style={{marginBottom: MARGIN}}
|
||||
|
@ -156,7 +138,7 @@ export default function SetForm({
|
|||
Image
|
||||
</Button>
|
||||
)}
|
||||
</ScrollView>
|
||||
{next && <Button icon="navigate-next">Next</Button>}
|
||||
<Button
|
||||
disabled={!name}
|
||||
mode="contained"
|
||||
|
|
|
@ -33,7 +33,7 @@ export default function SetItem({
|
|||
const set: Set = {...item};
|
||||
delete set.id;
|
||||
setShowMenu(false);
|
||||
navigation.navigate('EditSet', {set, workouts: [], count: 0});
|
||||
navigation.navigate('EditSet', {set});
|
||||
}, [navigation, item]);
|
||||
|
||||
const longPress = useCallback(
|
||||
|
@ -52,9 +52,7 @@ export default function SetItem({
|
|||
return (
|
||||
<>
|
||||
<List.Item
|
||||
onPress={() =>
|
||||
navigation.navigate('EditSet', {set: item, workouts: [], count: 0})
|
||||
}
|
||||
onPress={() => navigation.navigate('EditSet', {set: item})}
|
||||
title={item.name}
|
||||
description={`${item.reps} x ${item.weight}${item.unit || 'kg'}`}
|
||||
onLongPress={longPress}
|
||||
|
|
53
SetList.tsx
53
SetList.tsx
|
@ -6,13 +6,11 @@ import {
|
|||
import React, {useCallback, useEffect, useState} from 'react';
|
||||
import {FlatList} from 'react-native';
|
||||
import {List} from 'react-native-paper';
|
||||
import {getBestSet} from './best.service';
|
||||
import DrawerMenu from './DrawerMenu';
|
||||
import {HomePageParams} from './home-page-params';
|
||||
import Page from './Page';
|
||||
import {getTodaysPlan} from './plan.service';
|
||||
import Set from './set';
|
||||
import {countToday, defaultSet, getSets, getToday} from './set.service';
|
||||
import {defaultSet, getSets, getToday} from './set.service';
|
||||
import SetItem from './SetItem';
|
||||
import {useSettings} from './use-settings';
|
||||
|
||||
|
@ -21,8 +19,6 @@ const limit = 15;
|
|||
export default function SetList() {
|
||||
const [sets, setSets] = useState<Set[]>();
|
||||
const [set, setSet] = useState<Set>();
|
||||
const [count, setCount] = useState(0);
|
||||
const [workouts, setWorkouts] = useState<string[]>([]);
|
||||
const [offset, setOffset] = useState(0);
|
||||
const [search, setSearch] = useState('');
|
||||
const [end, setEnd] = useState(false);
|
||||
|
@ -30,44 +26,9 @@ export default function SetList() {
|
|||
const [dates, setDates] = useState(!!settings.showDate);
|
||||
const navigation = useNavigation<NavigationProp<HomePageParams>>();
|
||||
|
||||
const predict = useCallback(async () => {
|
||||
setCount(0);
|
||||
setSet({...defaultSet});
|
||||
setWorkouts([]);
|
||||
if (settings.newSet === 'empty') return;
|
||||
const todaysSet = await getToday();
|
||||
console.log(`${SetList.name}.predict:`, {todaysSet});
|
||||
if (!settings.newSet && todaysSet) return setSet({...todaysSet});
|
||||
const todaysPlan = await getTodaysPlan();
|
||||
console.log(`${SetList.name}.predict:`, {todaysPlan});
|
||||
if (todaysPlan.length === 0) return;
|
||||
const todaysWorkouts = todaysPlan[0].workouts.split(',');
|
||||
setWorkouts(todaysWorkouts);
|
||||
let workout = todaysWorkouts[0];
|
||||
let best = await getBestSet(workout);
|
||||
let [{image}] = await getSets({search: best.name, limit: 1, offset: 0});
|
||||
console.log(`${SetList.name}.predict:`, {workout, best, image});
|
||||
if (!todaysSet || !todaysWorkouts.includes(todaysSet.name))
|
||||
return setSet({...best, image});
|
||||
let _count = await countToday(todaysSet.name);
|
||||
console.log(`${SetList.name}.predict:`, {_count});
|
||||
workout = todaysSet.name;
|
||||
best = await getBestSet(workout);
|
||||
[{image}] = await getSets({search: best.name, limit: 1, offset: 0});
|
||||
console.log(`${SetList.name}.predict:`, {workout, best, image});
|
||||
const index = todaysWorkouts.indexOf(todaysSet.name) + 1;
|
||||
if (_count >= Number(best.sets)) {
|
||||
best = await getBestSet(todaysWorkouts[index]);
|
||||
[{image}] = await getSets({search: best.name, limit: 1, offset: 0});
|
||||
_count = 0;
|
||||
}
|
||||
if (best.name === '') setCount(0);
|
||||
else setCount(_count);
|
||||
setSet({...best, image});
|
||||
}, [settings]);
|
||||
|
||||
const refresh = useCallback(async () => {
|
||||
predict();
|
||||
const todaysSet = await getToday();
|
||||
if (todaysSet) setSet({...todaysSet});
|
||||
const newSets = await getSets({
|
||||
search: `%${search}%`,
|
||||
limit,
|
||||
|
@ -79,7 +40,7 @@ export default function SetList() {
|
|||
setSets(newSets);
|
||||
setOffset(0);
|
||||
setEnd(false);
|
||||
}, [search, predict, settings.date]);
|
||||
}, [search, settings.date]);
|
||||
|
||||
useFocusEffect(
|
||||
useCallback(() => {
|
||||
|
@ -126,13 +87,11 @@ export default function SetList() {
|
|||
}, [search, end, offset, sets, settings.date]);
|
||||
|
||||
const onAdd = useCallback(async () => {
|
||||
console.log(`${SetList.name}.onAdd`, {set, workouts});
|
||||
console.log(`${SetList.name}.onAdd`, {set});
|
||||
navigation.navigate('EditSet', {
|
||||
set: set || {...defaultSet},
|
||||
workouts,
|
||||
count,
|
||||
});
|
||||
}, [navigation, set, workouts, count]);
|
||||
}, [navigation, set]);
|
||||
|
||||
return (
|
||||
<Page onAdd={onAdd} search={search} setSearch={setSearch}>
|
||||
|
|
|
@ -22,12 +22,10 @@ export default function SettingsPage() {
|
|||
const {settings, setSettings} = useSettings();
|
||||
const [vibrate, setVibrate] = useState(!!settings.vibrate);
|
||||
const [alarm, setAlarm] = useState(!!settings.alarm);
|
||||
const [newSet, setNewSet] = useState(settings.newSet);
|
||||
const [sound, setSound] = useState(settings.sound);
|
||||
const [notify, setNotify] = useState(!!settings.notify);
|
||||
const [images, setImages] = useState(!!settings.images);
|
||||
const [showUnit, setShowUnit] = useState(!!settings.showUnit);
|
||||
const [workouts, setWorkouts] = useState(!!settings.workouts);
|
||||
const [steps, setSteps] = useState(!!settings.steps);
|
||||
const [date, setDate] = useState(settings.date || '%Y-%m-%d %H:%M');
|
||||
const [theme, setTheme] = useState(settings.theme || 'system');
|
||||
|
@ -45,13 +43,11 @@ export default function SettingsPage() {
|
|||
updateSettings({
|
||||
vibrate: +vibrate,
|
||||
alarm: +alarm,
|
||||
newSet,
|
||||
sound,
|
||||
notify: +notify,
|
||||
images: +images,
|
||||
showUnit: +showUnit,
|
||||
color,
|
||||
workouts: +workouts,
|
||||
steps: +steps,
|
||||
date,
|
||||
showDate: +showDate,
|
||||
|
@ -61,13 +57,11 @@ export default function SettingsPage() {
|
|||
}, [
|
||||
vibrate,
|
||||
alarm,
|
||||
newSet,
|
||||
sound,
|
||||
notify,
|
||||
images,
|
||||
showUnit,
|
||||
color,
|
||||
workouts,
|
||||
steps,
|
||||
setSettings,
|
||||
date,
|
||||
|
@ -131,15 +125,6 @@ export default function SettingsPage() {
|
|||
[toast],
|
||||
);
|
||||
|
||||
const changeWorkouts = useCallback(
|
||||
(enabled: boolean) => {
|
||||
setWorkouts(enabled);
|
||||
if (enabled) toast('Show workout for sets.', 4000);
|
||||
else toast('Stopped showing workout for sets.', 4000);
|
||||
},
|
||||
[toast],
|
||||
);
|
||||
|
||||
const changeSteps = useCallback(
|
||||
(enabled: boolean) => {
|
||||
setSteps(enabled);
|
||||
|
@ -164,7 +149,6 @@ export default function SettingsPage() {
|
|||
{name: 'Record notifications', value: notify, onChange: changeNotify},
|
||||
{name: 'Show images', value: images, onChange: changeImages},
|
||||
{name: 'Show unit', value: showUnit, onChange: changeUnit},
|
||||
{name: 'Show workouts', value: workouts, onChange: changeWorkouts},
|
||||
{name: 'Show steps', value: steps, onChange: changeSteps},
|
||||
{name: 'Show date', value: showDate, onChange: changeShowDate},
|
||||
];
|
||||
|
@ -212,17 +196,6 @@ export default function SettingsPage() {
|
|||
))}
|
||||
</Picker>
|
||||
)}
|
||||
{'new set'.includes(search.toLowerCase()) && (
|
||||
<Picker
|
||||
style={{color, marginTop: -10}}
|
||||
dropdownIconColor={color}
|
||||
selectedValue={newSet}
|
||||
onValueChange={value => setNewSet(value)}>
|
||||
<Picker.Item value="" label="Copy new sets" />
|
||||
<Picker.Item value="predict" label="Predict new sets" />
|
||||
<Picker.Item value="empty" label="New sets are empty" />
|
||||
</Picker>
|
||||
)}
|
||||
{'date format'.includes(search.toLowerCase()) && (
|
||||
<Picker
|
||||
style={{color, marginTop: -10}}
|
||||
|
|
147
StartSession.tsx
Normal file
147
StartSession.tsx
Normal file
|
@ -0,0 +1,147 @@
|
|||
import {
|
||||
RouteProp,
|
||||
useFocusEffect,
|
||||
useNavigation,
|
||||
useRoute,
|
||||
} from '@react-navigation/native';
|
||||
import React, {useCallback, useContext, useMemo, useRef, useState} from 'react';
|
||||
import {NativeModules, TextInput, View} from 'react-native';
|
||||
import {Button, Chip, IconButton} from 'react-native-paper';
|
||||
import {getBestSet} from './best.service';
|
||||
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 {addSet, countManyToday} from './set.service';
|
||||
import SetForm from './SetForm';
|
||||
import {useSettings} from './use-settings';
|
||||
|
||||
export default function StartSession() {
|
||||
const {params} = useRoute<RouteProp<SessionPageParams, 'StartSession'>>();
|
||||
const {set} = params;
|
||||
const [name, setName] = useState(set.name);
|
||||
const [reps, setReps] = useState(set.reps.toString());
|
||||
const [weight, setWeight] = useState(set.weight.toString());
|
||||
const [unit, setUnit] = useState<string>();
|
||||
const {toast} = useContext(SnackbarContext);
|
||||
const [minutes, setMinutes] = useState(set.minutes);
|
||||
const [seconds, setSeconds] = useState(set.seconds);
|
||||
const {settings} = useSettings();
|
||||
const [counts, setCounts] = useState<CountMany[]>();
|
||||
const [selection, setSelection] = useState({
|
||||
start: 0,
|
||||
end: set.reps.toString().length,
|
||||
});
|
||||
const weightRef = useRef<TextInput>(null);
|
||||
const repsRef = useRef<TextInput>(null);
|
||||
const unitRef = useRef<TextInput>(null);
|
||||
const navigation = useNavigation();
|
||||
const workouts = useMemo(() => params.plan.workouts.split(','), [params]);
|
||||
|
||||
useFocusEffect(
|
||||
useCallback(() => {
|
||||
navigation.getParent()?.setOptions({
|
||||
headerLeft: () => (
|
||||
<IconButton icon="arrow-back" onPress={() => navigation.goBack()} />
|
||||
),
|
||||
headerRight: null,
|
||||
title: params.plan.days.replace(/,/g, ', '),
|
||||
});
|
||||
countManyToday().then(setCounts);
|
||||
}, [navigation, params]),
|
||||
);
|
||||
|
||||
const handleSubmit = async () => {
|
||||
console.log(`${SetForm.name}.handleSubmit:`, {reps, weight, unit});
|
||||
await addSet({
|
||||
name,
|
||||
weight: +weight,
|
||||
reps: +reps,
|
||||
minutes: set.minutes,
|
||||
seconds: set.seconds,
|
||||
steps: set.steps,
|
||||
unit,
|
||||
});
|
||||
countManyToday().then(setCounts);
|
||||
if (!settings.alarm) return;
|
||||
const milliseconds = Number(minutes) * 60 * 1000 + Number(seconds) * 1000;
|
||||
const args = [milliseconds, !!settings.vibrate, settings.sound];
|
||||
NativeModules.AlarmModule.timer(...args);
|
||||
};
|
||||
|
||||
const handleUnit = useCallback(
|
||||
(value: string) => {
|
||||
setUnit(value.replace(/,|'/g, ''));
|
||||
if (value.match(/,|'/))
|
||||
toast('Commas and single quotes would break CSV exports', 6000);
|
||||
},
|
||||
[toast],
|
||||
);
|
||||
|
||||
const select = useCallback(
|
||||
async (index: number) => {
|
||||
console.log(`${StartSession.name}.next:`, {name, workouts});
|
||||
const workout = workouts[index];
|
||||
console.log(`${StartSession.name}.next:`, {workout});
|
||||
const best = await getBestSet(workout);
|
||||
console.log(`${StartSession.name}.next:`, {best});
|
||||
setMinutes(best.minutes);
|
||||
setSeconds(best.seconds);
|
||||
setName(best.name);
|
||||
setReps(best.reps.toString());
|
||||
setWeight(best.weight.toString());
|
||||
setUnit(best.unit);
|
||||
},
|
||||
[name, workouts],
|
||||
);
|
||||
|
||||
return (
|
||||
<View style={{padding: PADDING}}>
|
||||
<MassiveInput
|
||||
label="Reps"
|
||||
keyboardType="numeric"
|
||||
value={reps}
|
||||
onChangeText={setReps}
|
||||
onSubmitEditing={() => weightRef.current?.focus()}
|
||||
selection={selection}
|
||||
onSelectionChange={e => setSelection(e.nativeEvent.selection)}
|
||||
autoFocus
|
||||
innerRef={repsRef}
|
||||
/>
|
||||
<MassiveInput
|
||||
label="Weight"
|
||||
keyboardType="numeric"
|
||||
value={weight}
|
||||
onChangeText={setWeight}
|
||||
onSubmitEditing={handleSubmit}
|
||||
innerRef={weightRef}
|
||||
/>
|
||||
{!!settings.showUnit && (
|
||||
<MassiveInput
|
||||
autoCapitalize="none"
|
||||
label="Unit"
|
||||
value={unit}
|
||||
onChangeText={handleUnit}
|
||||
innerRef={unitRef}
|
||||
/>
|
||||
)}
|
||||
<View style={{flexDirection: 'row', flexWrap: 'wrap'}}>
|
||||
{workouts.map((workout, index) => (
|
||||
<Chip
|
||||
key={workout}
|
||||
selected={workout === name}
|
||||
icon={workout === name ? 'fitness-center' : 'hotel'}
|
||||
onPress={() => select(index)}
|
||||
style={{marginBottom: MARGIN, marginRight: MARGIN}}>
|
||||
{workout} x
|
||||
{counts?.find(count => count.name === workout)?.total || 0}
|
||||
</Chip>
|
||||
))}
|
||||
</View>
|
||||
<Button mode="contained" icon="save" onPress={handleSubmit}>
|
||||
Save
|
||||
</Button>
|
||||
</View>
|
||||
);
|
||||
}
|
101
TimerPage.tsx
101
TimerPage.tsx
|
@ -1,101 +0,0 @@
|
|||
import {useFocusEffect} from '@react-navigation/native';
|
||||
import React, {useCallback, useEffect, useState} from 'react';
|
||||
import {NativeModules, View} from 'react-native';
|
||||
import {Button, Text} from 'react-native-paper';
|
||||
import {MARGIN, PADDING} from './constants';
|
||||
import {
|
||||
getNext,
|
||||
getSettings,
|
||||
settings,
|
||||
updateSettings,
|
||||
} from './settings.service';
|
||||
|
||||
export default function TimerPage() {
|
||||
const [next, setNext] = useState(new Date());
|
||||
const [ms, setMs] = useState(0);
|
||||
const [intervalId, setIntervalId] = useState(0);
|
||||
|
||||
const seconds =
|
||||
ms > 0
|
||||
? Math.floor((ms / 1000) % 60)
|
||||
.toString()
|
||||
.padStart(2, '0')
|
||||
: '00';
|
||||
|
||||
const minutes =
|
||||
ms > 0
|
||||
? Math.floor(ms / 1000 / 60)
|
||||
.toString()
|
||||
.padStart(2, '0')
|
||||
: '00';
|
||||
|
||||
useFocusEffect(
|
||||
useCallback(() => {
|
||||
getNext().then(nextIso =>
|
||||
setNext(nextIso ? new Date(nextIso) : new Date()),
|
||||
);
|
||||
}, []),
|
||||
);
|
||||
|
||||
const tick = (date: Date) => {
|
||||
const remaining = date.getTime() - new Date().getTime();
|
||||
console.log(`${TimerPage.name}.tick`, {remaining});
|
||||
if (remaining <= 0) return 0;
|
||||
setMs(remaining);
|
||||
return remaining;
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
console.log(`${TimerPage.name}.useEffect:`, {next});
|
||||
const date = next || new Date();
|
||||
if (tick(date) <= 0) return;
|
||||
const id = setInterval(() => {
|
||||
if (tick(date) <= 0) clearInterval(id);
|
||||
}, 1000);
|
||||
setIntervalId(oldId => {
|
||||
clearInterval(oldId);
|
||||
return id;
|
||||
});
|
||||
return () => clearInterval(id);
|
||||
}, [next]);
|
||||
|
||||
const stop = () => {
|
||||
NativeModules.AlarmModule.stop();
|
||||
setNext(new Date());
|
||||
updateSettings({...settings, nextAlarm: undefined});
|
||||
getSettings();
|
||||
tick(new Date());
|
||||
setMs(0);
|
||||
};
|
||||
|
||||
const add = async () => {
|
||||
console.log(`${TimerPage.name}.add:`, {intervalId, next});
|
||||
const date = next || new Date();
|
||||
date.setTime(date.getTime() + 1000 * 60);
|
||||
await updateSettings({...settings, nextAlarm: date.toISOString()});
|
||||
setNext(date);
|
||||
NativeModules.AlarmModule.add(ms, !!settings.vibrate, settings.sound);
|
||||
tick(date);
|
||||
const id = setInterval(() => {
|
||||
if (tick(date) <= 0) clearInterval(id);
|
||||
}, 1000);
|
||||
setIntervalId(oldId => {
|
||||
clearInterval(oldId);
|
||||
return id;
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<View style={{padding: PADDING, alignItems: 'center'}}>
|
||||
<Text>
|
||||
{minutes}:{seconds}
|
||||
</Text>
|
||||
<Button style={{marginTop: MARGIN}} onPress={stop}>
|
||||
Stop
|
||||
</Button>
|
||||
<Button style={{marginTop: MARGIN}} onPress={add}>
|
||||
Add 1 min
|
||||
</Button>
|
||||
</View>
|
||||
);
|
||||
}
|
4
count-many.ts
Normal file
4
count-many.ts
Normal file
|
@ -0,0 +1,4 @@
|
|||
export default interface CountMany {
|
||||
name: string;
|
||||
total: number;
|
||||
}
|
|
@ -4,4 +4,5 @@ export type DrawerParamList = {
|
|||
Best: {};
|
||||
Plans: {};
|
||||
Workouts: {};
|
||||
Session: {};
|
||||
};
|
||||
|
|
|
@ -4,7 +4,5 @@ export type HomePageParams = {
|
|||
Sets: {};
|
||||
EditSet: {
|
||||
set: Set;
|
||||
workouts: string[];
|
||||
count: number;
|
||||
};
|
||||
};
|
||||
|
|
0
massive.db
Normal file
0
massive.db
Normal file
10
session-page-params.ts
Normal file
10
session-page-params.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
import {Plan} from './plan';
|
||||
import Set from './set';
|
||||
|
||||
export type SessionPageParams = {
|
||||
SessionList: {};
|
||||
StartSession: {
|
||||
plan: Plan;
|
||||
set: Set;
|
||||
};
|
||||
};
|
|
@ -1,3 +1,4 @@
|
|||
import CountMany from './count-many';
|
||||
import {db} from './db';
|
||||
import Set from './set';
|
||||
|
||||
|
@ -154,6 +155,17 @@ export const countToday = async (name: string): Promise<number> => {
|
|||
return Number(result.rows.item(0)?.total);
|
||||
};
|
||||
|
||||
export const countManyToday = async (): Promise<CountMany[]> => {
|
||||
const select = `
|
||||
SELECT COUNT(*) as total, name FROM sets
|
||||
WHERE created LIKE strftime('%Y-%m-%d%%', 'now', 'localtime')
|
||||
AND NOT hidden
|
||||
GROUP BY name
|
||||
`;
|
||||
const [result] = await db.executeSql(select);
|
||||
return result.rows.raw();
|
||||
};
|
||||
|
||||
export const getDistinctSets = async ({
|
||||
search,
|
||||
limit,
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
export default interface Settings {
|
||||
alarm: number;
|
||||
vibrate: number;
|
||||
newSet?: 'predict' | 'empty';
|
||||
sound?: string;
|
||||
notify?: number;
|
||||
images?: number;
|
||||
showUnit?: number;
|
||||
color?: string;
|
||||
workouts: number;
|
||||
nextAlarm?: string;
|
||||
steps?: number;
|
||||
date?: string;
|
||||
|
|
|
@ -8,7 +8,7 @@ export const SettingsContext = React.createContext<{
|
|||
settings: {
|
||||
alarm: 0,
|
||||
vibrate: 1,
|
||||
workouts: 0,
|
||||
showDate: 0,
|
||||
},
|
||||
setSettings: () => null,
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user