forked from Kennygunderman/state-of-health-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
228 lines (208 loc) · 8.84 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import React from 'react';
import { FontAwesome5, Ionicons, MaterialCommunityIcons } from '@expo/vector-icons';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import {
AppState, LogBox, StatusBar, TouchableOpacity,
} from 'react-native';
import Toast from 'react-native-toast-message';
import { Provider } from 'react-redux';
import ToastConfig from './src/components/toast/ToastConfig';
import Screens from './src/constants/Screens';
import {
MACROS_TITLE,
NEW_FOOD_ITEM_TEXT,
PREVIOUS_ENTRIES_TITLE,
PREVIOUS_WORKOUTS_TITLE,
QUICK_ADD_FOOD_SCREEN_TITLE,
} from './src/constants/Strings';
import AccountScreen from './src/screens/Account/AccountScreen';
import AddExerciseScreen from './src/screens/AddExercise/AddExerciseScreen';
import AddFoodScreen from './src/screens/AddFoodScreen';
import LogInScreen from './src/screens/Auth/LogInScreen';
import RegisterScreen from './src/screens/Auth/RegisterScreen';
import CreateExerciseScreen from './src/screens/CreateExerciseScreen';
import CreateFoodScreen from './src/screens/CreateFoodScreen';
import FoodDetailScreen from './src/screens/FoodDetailScreen';
import MealsScreen from './src/screens/Meals/MealsScreen';
import PreviousDailyExerciseEntriesScreen
from './src/screens/PreviousDailyExercises/PreviousDailyExerciseEntriesScreen';
import PreviousDailyMealEntriesScreen from './src/screens/PreviousDailyMealEntriesScreen';
import WorkoutsScreen from './src/screens/Workouts/WorkoutsScreen';
import WorkoutTemplateDetailScreen from './src/screens/WorkoutTemplateDetailScreen';
import store, { useThunkDispatch } from './src/store';
import { addDailyMealEntry } from './src/store/dailyMealEntries/DailyMealEntriesActions';
import { syncUserData } from './src/store/user/UserActions';
import { setCurrentDate } from './src/store/userInfo/UserInfoActions';
import { darkTheme, useStyleTheme } from './src/styles/Theme';
import { getCurrentDate } from './src/utility/DateUtility';
const Stack = createNativeStackNavigator();
const Tab = createBottomTabNavigator();
LogBox.ignoreAllLogs(true);
const Home = () => {
const macrosIcon = (color: string) => (
<FontAwesome5
name="utensils"
color={color}
size={16}
style={{ marginBottom: -3 }}
/>
);
const barbellIcon = (color: string) => (
<Ionicons
name="barbell"
color={color}
size={24}
style={{ marginBottom: -3 }}
/>
);
const accountIcon = (color: string) => (
<MaterialCommunityIcons
name="account"
size={24}
color={color}
style={{ marginBottom: -3 }}
/>
);
return (
<Tab.Navigator
sceneContainerStyle={{ backgroundColor: useStyleTheme().colors.background }}
screenOptions={({ route }) => ({
tabBarIcon: ({ color }) => {
if (route.name === Screens.MEALS) {
return macrosIcon(color);
}
if (route.name === Screens.WORKOUTS) {
return barbellIcon(color);
}
if (route.name === Screens.ACCOUNT) {
return accountIcon(color);
}
},
headerShown: false,
tabBarActiveTintColor: useStyleTheme().colors.white,
tabBarInactiveTintColor: useStyleTheme().colors.grey,
tabBarStyle: {
borderTopWidth: 0,
backgroundColor: useStyleTheme().colors.navBar,
},
})}
>
<Tab.Screen name={Screens.MEALS} options={{ title: MACROS_TITLE }} component={MealsScreen} />
<Tab.Screen name={Screens.WORKOUTS} component={WorkoutsScreen} />
<Tab.Screen name={Screens.ACCOUNT} component={AccountScreen} />
</Tab.Navigator>
);
};
const AppStateChanged = () => {
const dispatch = useThunkDispatch();
async function onApplicationStateChange(appState: string) {
if (appState === 'active') {
const currentDate = getCurrentDate();
dispatch(setCurrentDate(currentDate));
dispatch(addDailyMealEntry(currentDate));
dispatch(syncUserData());
}
}
dispatch(syncUserData());
AppState.addEventListener('change', onApplicationStateChange);
// eslint-disable-next-line react/jsx-no-useless-fragment
return (<></>);
};
const App = () => {
const backButton = (onPress: () => void) => (
<TouchableOpacity onPress={onPress}>
<Ionicons name="chevron-back" size={24} color={useStyleTheme().colors.white} />
</TouchableOpacity>
);
const closeButton = (onPress: () => void) => (
<TouchableOpacity onPress={onPress}>
<Ionicons name="close" size={24} color={useStyleTheme().colors.white} />
</TouchableOpacity>
);
return (
<Provider store={store}>
<StatusBar barStyle="light-content" />
<AppStateChanged />
<NavigationContainer theme={darkTheme}>
<Stack.Navigator
initialRouteName={Screens.MEALS}
screenOptions={({ navigation, route }) => ({
headerLeft: () => {
if (route.name === Screens.MEALS) {
return;
}
return backButton(() => navigation.goBack());
},
headerShown: route.name !== Screens.MEALS,
headerStyle: {
backgroundColor: useStyleTheme().colors.background,
},
headerShadowVisible: false,
headerTintColor: useStyleTheme().colors.white,
})}
>
<Stack.Screen
name="Home"
component={Home}
options={{ headerShown: false }}
/>
<Stack.Screen
name={Screens.ADD_FOOD}
options={{
title: QUICK_ADD_FOOD_SCREEN_TITLE,
headerStyle: {
backgroundColor: useStyleTheme().colors.secondary,
},
}}
component={AddFoodScreen}
/>
<Stack.Group screenOptions={({ navigation }) => ({
headerLeft: () => (
closeButton(() => navigation.goBack())
),
presentation: 'modal',
})}
>
<Stack.Screen name={Screens.LOG_IN} component={LogInScreen} />
<Stack.Screen name={Screens.REGISTER} component={RegisterScreen} />
</Stack.Group>
<Stack.Screen name={Screens.FOOD_DETAIL_SCREEN} component={FoodDetailScreen} />
<Stack.Screen
name={Screens.CREATE_FOOD}
options={{ title: NEW_FOOD_ITEM_TEXT }}
component={CreateFoodScreen}
/>
<Stack.Screen
name={Screens.PREVIOUS_DAILY_MEAL_ENTRIES}
options={{ title: PREVIOUS_ENTRIES_TITLE }}
component={PreviousDailyMealEntriesScreen}
/>
<Stack.Screen
options={{
headerStyle: {
backgroundColor: useStyleTheme().colors.secondary,
},
}}
name={Screens.ADD_EXERCISE}
component={AddExerciseScreen}
/>
<Stack.Screen name={Screens.WORKOUT_TEMPLATE_DETAIL} component={WorkoutTemplateDetailScreen} />
<Stack.Screen name={Screens.CREATE_EXERCISE} component={CreateExerciseScreen} />
<Stack.Screen
name={Screens.PREVIOUS_DAILY_EXERCISE_ENTRIES}
options={{ title: PREVIOUS_WORKOUTS_TITLE }}
component={PreviousDailyExerciseEntriesScreen}
/>
</Stack.Navigator>
<Toast
config={ToastConfig}
position="top"
topOffset={50}
/>
</NavigationContainer>
</Provider>
);
};
export default App;