-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
635 lines (588 loc) · 24 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
import AsyncStorage from '@react-native-async-storage/async-storage';
import Icon from 'react-native-vector-icons/MaterialIcons';
import React, { FC, useEffect, useRef, useState } from 'react';
import { CombinedDefaultTheme, CombinedDarkTheme } from './src/themes';
import { DrawerActions, NavigationContainer, useNavigation } from '@react-navigation/native';
import { Provider as PaperProvider, BottomNavigation, useTheme, Searchbar } from 'react-native-paper';
import { View, Appearance, ActivityIndicator, Image } from 'react-native';
import { changeLanguage } from './src/i18n/i18n';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { createContext } from 'react';
import { createDrawerNavigator } from '@react-navigation/drawer';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { useTranslation } from 'react-i18next';
const colorScheme = Appearance.getColorScheme();
import { CatalogScreen as InnerCatalogScreen } from './src/screens/catalog/CatalogScreen';
/**
*A screen component that renders the user's favorite calculators.
*
* @param {Object} navigation - The navigation object used to navigate to other screens.
* @returns {JSX.Element} The rendered screen.
*/
function FavoritesScreen({ navigation }: any): JSX.Element {
const theme = useTheme();
const { t } = useTranslation();
return (
<View style={{ backgroundColor: theme.colors.background, flex: 1 }}>
<InnerCatalogScreen props={{ showOnly: 'favorites', title: t('favorites:title') }} />
</View>
);
}
/**
* A screen component that renders the user's recent use of calculators.
*
* @param {Object} navigation - The navigation object used to navigate to other screens.
* @returns {JSX.Element} The rendered screen.
*/
function RecentScreen({ navigation }: any): JSX.Element {
const theme = useTheme();
const { t } = useTranslation();
return (
<View style={{ backgroundColor: theme.colors.background, flex: 1 }}>
<InnerCatalogScreen props={{ showOnly: 'recent', title: t('recent:title') }} />
</View>
);
}
/**
* A screen component that renders all available calculations.
* @returns {JSX.Element} The rendered screen.
*/
function HomeScreen(): JSX.Element {
const theme = useTheme();
const { t } = useTranslation();
return (
<View style={{ backgroundColor: theme.colors.background, flex: 1 }}>
<InnerCatalogScreen props={{ showOnly: 'all', title: t('all:title') }} />
</View>
);
}
import InnerLegalScreen from './src/screens/legal/LegalScreen';
/**
* A screen component that renders legal information.
* @returns {JSX.Element} The rendered screen.
*/
function LegalScreen(): JSX.Element {
const theme = useTheme();
return (
<View style={{ backgroundColor: theme.colors.background, flex: 1 }}>
<InnerLegalScreen />
</View>
);
}
import InnerAboutScreen from './src/screens/about/AboutScreen';
/**
A screen component that renders information about the app.
* @param {Object} navigation - The navigation object used to navigate to other screens.
* @returns {JSX.Element} The rendered screen.
*/
function AboutScreen(): JSX.Element {
// Get the current theme
const theme = useTheme();
return (
<View style={{ backgroundColor: theme.colors.background, flex: 1 }}>
<InnerAboutScreen />
</View>
);
}
import InnerSettingsScreen from './src/screens/settings/SettingsScreen';
/**
* Renders a settings screen component.
* @returns {JSX.Element} The rendered settings screen component.
*/
function SettingsScreen(): JSX.Element {
// Get the current theme
const theme = useTheme();
return (
<View style={{ backgroundColor: theme.colors.background, flex: 1 }}>
<InnerSettingsScreen />
</View>
);
}
const Tab = createBottomTabNavigator();
/**
* Renders a tab navigation component for the home screen.
* @returns {JSX.Element} The rendered tab navigation component.
*/
function HomeTabs(): JSX.Element {
// Use the translation hook to get the translation function
const { t } = useTranslation();
const navigation = useNavigation(); // Get the navigation object using useNavigation
const initialRouteRef = useRef('');
useEffect(() => {
const fetchInitialRoute = async () => {
try {
const value = await AsyncStorage.getItem('favoriteIds');
const valueObj = JSON.parse(String(value));
console.log(valueObj.length);
if (valueObj.length > 0) {
initialRouteRef.current = t('favorites:title');
} else {
initialRouteRef.current = t('all:title');
}
// Now that we have set the initialRouteRef, navigate to it
navigation.navigate(initialRouteRef.current);
} catch (error) {
console.error('Error reading value from AsyncStorage:', error);
}
};
fetchInitialRoute();
}, []);
return (
<Tab.Navigator
screenOptions={{
headerShown: false,
}}
// Render the bottom tab bar
tabBar={({ navigation, state, descriptors, insets }) => (
<BottomNavigation.Bar
navigationState={state}
safeAreaInsets={insets}
// Handle tab press
onTabPress={({ route, preventDefault }) => {
const event = navigation.emit({
type: 'tabPress',
target: route.key,
canPreventDefault: true,
});
if (event.defaultPrevented) {
preventDefault();
} else {
// Navigate to the selected screen
navigation.navigate(route.name);
}
}}
// Render the icon for each tab
renderIcon={({ route, focused, color }) => {
const { options } = descriptors[route.key];
if (options.tabBarIcon) {
return options.tabBarIcon({
focused,
color,
size: 24,
});
}
return null;
}}
// Get the label for each tab
getLabelText={({ route }: any) => {
const { options } = descriptors[route.key];
const label = options.tabBarLabel !== undefined ? options.tabBarLabel : options.title !== undefined ? options.title : route.title;
return label;
}}
/>
)}
>
{/* Define each screen */}
<Tab.Screen
name={t('favorites:title')}
component={FavoritesScreen}
options={{
tabBarLabel: String(t('favorites:title')),
headerShown: false,
tabBarIcon: ({ color, size }) => (
<Icon
name="star"
color={color}
size={size}
/>
),
}}
/>
<Tab.Screen
name={t('recent:title')}
component={RecentScreen}
options={{
tabBarLabel: String(t('recent:title')),
headerShown: false,
tabBarIcon: ({ color, size }) => (
<Icon
name="history"
color={color}
size={size}
/>
),
}}
/>
<Tab.Screen
name={t('all:title')}
component={HomeScreen}
options={{
tabBarLabel: String(t('all:title')),
headerShown: false,
tabBarIcon: ({ color, size }) => (
<Icon
name="format-list-bulleted"
color={color}
size={size}
/>
),
}}
/>
</Tab.Navigator>
);
}
import { Appbar } from 'react-native-paper';
import { Drawer as PaperDrawer } from 'react-native-paper';
import { DebugScreen } from './src/screens/debug/DebugScreen';
export const SearchContext = createContext('');
const Drawer = createDrawerNavigator();
/**
* The MainDrawer component is responsible for rendering the custom drawer for the application. It is used in conjunction with the React Navigation Drawer component and the React Native Paper library. It displays a logo, search bar, and menu items for navigating through the app.
* @param {object} navigation - An object containing navigation functions for the component.
* @returns {JSX.Element} The rendered custom drawer.
*/
function MainDrawer({ navigation }: any): JSX.Element {
// Get the current theme using the useTheme hook from React Native Paper
const theme = useTheme();
// Get the translation function using the useTranslation hook from i18next
const { t } = useTranslation();
// Initialize the search query state with an empty string
const [searchQuery, setSearchQuery] = useState('');
// Initialize the background color state with the theme's elevation level 5 color
const [backgroundColor, setBackgroundColor] = useState(theme.colors.elevation.level5);
// Initialize the active state with the "main" value
const [active, setActive] = React.useState('main');
// Set the width of the drawer
const width = 300;
// Calculate the height of the drawer image based on the aspect ratio and width
const imageAspectRatio = 1.58; // assuming a 4:3 aspect ratio for the image
const imageWidth = width;
const imageHeight = imageWidth / imageAspectRatio;
// Load the logo image based on the current theme
const logo = theme.dark ? require('./assets/logo_bg_dark.jpg') : require('./assets/logo_bg_light.jpg');
// Use a custom drawer content component to override the default drawer content
const CustomDrawerContent = (props: any) => {
//console.log(props.descriptors.name);
return (
<View
style={{
flex: 1,
backgroundColor: theme.colors.elevation.level1,
// paddingTop: 10,
}}
>
<PaperDrawer.Section showDivider={false}>
<Image
source={logo}
resizeMode={'cover'}
style={{
borderTopLeftRadius: 10,
borderTopRightRadius: 10,
height: imageHeight,
width: '100%',
marginBottom: 20,
}}
/>
<PaperDrawer.Item
style={{ marginBottom: 5, paddingLeft: 10 }}
active={active === 'main'}
label={t('main:title')}
onPress={() => {
setActive('main');
navigation.navigate(t('main:title'));
}}
/>
<PaperDrawer.Item
style={{ marginBottom: 5, paddingLeft: 10 }}
active={active === 'settings'}
label={t('settings:title')}
onPress={() => {
setActive('settings');
navigation.navigate(t('settings:title'));
}}
/>
<PaperDrawer.Item
style={{ marginBottom: 5, paddingLeft: 10 }}
active={active === 'legal'}
label={t('legal:title')}
onPress={() => {
setActive('legal');
navigation.navigate(t('legal:title'));
}}
/>
<PaperDrawer.Item
style={{ marginBottom: 5, paddingLeft: 10 }}
active={active === 'about'}
label={t('about:title')}
onPress={() => {
setActive('about');
navigation.navigate(t('about:title'));
}}
/>
{/* <PaperDrawer.Item
style={{ marginBottom: 5, paddingLeft: 10 }}
active={active === "DEBUG"}
label={"DEBUG"}
onPress={() => {
navigation.navigate("DEBUG");
}}
/> */}
</PaperDrawer.Section>
</View>
);
};
return (
<SearchContext.Provider value={searchQuery}>
<Drawer.Navigator
initialRouteName="Home"
drawerContent={(props) => <CustomDrawerContent {...props} />}
screenOptions={{
drawerStyle: {
width: 300,
},
}}
>
<Drawer.Screen
name={t('main:title')}
component={HomeTabs}
options={{
header: () => (
<Appbar.Header
statusBarHeight={0} // HOWTO: https://stackoverflow.com/questions/74421646/how-to-remove-extra-space-above-react-navigation-header
mode="small"
style={{
marginTop: 7,
marginBottom: 7,
marginLeft: 3,
marginRight: 10,
}}
elevated={true}
>
<Appbar.Action
icon="menu"
onPress={() => navigation.dispatch(DrawerActions.toggleDrawer())}
/>
<Searchbar
placeholder={String(t('search:title'))}
onChangeText={(text) => setSearchQuery(text)}
value={searchQuery}
onFocus={() => {
setBackgroundColor(theme.colors.background);
navigation.navigate(t('main:title'));
}}
onBlur={() => {
setBackgroundColor(theme.colors.elevation.level5);
}}
style={{
flex: 1,
marginLeft: 10,
backgroundColor: backgroundColor,
}}
mode="bar"
inputMode="text"
/>
</Appbar.Header>
),
}}
/>
<Drawer.Screen
name={t('settings:title')}
component={SettingsScreen}
options={{
header: () => (
<Appbar.Header
mode="small"
style={{
marginTop: 7,
marginBottom: 7,
marginLeft: 3,
marginRight: 10,
}}
elevated={true}
>
<Appbar.Action
icon="menu"
onPress={() => navigation.dispatch(DrawerActions.toggleDrawer())}
/>
<Appbar.Content title={t('settings:title')} />
</Appbar.Header>
),
}}
/>
<Drawer.Screen
name={t('legal:title')}
component={LegalScreen}
options={{
header: () => (
<Appbar.Header
mode="small"
style={{
marginTop: 7,
marginBottom: 7,
marginLeft: 3,
marginRight: 10,
}}
elevated={true}
>
<Appbar.Action
icon="menu"
onPress={() => navigation.dispatch(DrawerActions.toggleDrawer())}
/>
<Appbar.Content title={t('legal:title')} />
</Appbar.Header>
),
}}
/>
<Drawer.Screen
name={t('about:title')}
component={AboutScreen}
options={{
header: () => (
<Appbar.Header
mode="small"
style={{
marginTop: 7,
marginBottom: 7,
marginLeft: 3,
marginRight: 10,
}}
elevated={true}
>
<Appbar.Action
icon="menu"
onPress={() => navigation.dispatch(DrawerActions.toggleDrawer())}
/>
<Appbar.Content title={t('about:title')} />
</Appbar.Header>
),
}}
/>
<Drawer.Screen
name={'DEBUG'}
component={DebugScreen}
options={{
header: () => (
<Appbar.Header
mode="small"
style={{
marginTop: 7,
marginBottom: 7,
marginLeft: 3,
marginRight: 10,
}}
elevated={true}
>
<Appbar.Action
icon="menu"
onPress={() => navigation.dispatch(DrawerActions.toggleDrawer())}
/>
<Appbar.Content title={'DEBUG'} />
</Appbar.Header>
),
}}
/>
</Drawer.Navigator>
</SearchContext.Provider>
);
}
import { OpenedScreen } from './src/screens/opened/OpenedScreen';
const Stack = createNativeStackNavigator();
// Define the types for the navigation props
interface StackHeaderProps {
navigation: any; // Adjust the type according to your navigation library
title?: string;
}
/**
* The root component of the application.
* @returns {JSX.Element} The rendered application.
*/
function App(): JSX.Element {
// State for active theme
const [activeTheme, setActiveTheme] = useState(colorScheme === 'dark' ? CombinedDarkTheme : CombinedDefaultTheme);
useEffect(() => {
// Set theme based on device appearance setting
setActiveTheme(colorScheme === 'dark' ? CombinedDarkTheme : CombinedDefaultTheme);
// Set language preference
const setLanguage = async () => {
await AsyncStorage.getItem('language').then((result) => {
// console.log("lang from asyncstorage");
// console.log(result);
if (result) {
changeLanguage(result);
} else {
// based on system language
changeLanguage('system');
}
});
};
setLanguage();
// Set formatting preset
const setFormat = async () => {
await AsyncStorage.getItem('format').then((result) => {
if (!result) {
AsyncStorage.setItem('format', 'longAndCategory');
}
});
};
setFormat();
// Init Favorites
const initFavorites = async () => {
AsyncStorage.getItem('favoriteIds').then((result) => {
// console.log("On app init favorites:");
// console.log(result);
if (!result) {
// Ceate empty list
AsyncStorage.setItem('favoriteIds', JSON.stringify([]));
}
});
};
initFavorites();
// Init Recent
const initRecent = async () => {
AsyncStorage.getItem('recentIds').then((result) => {
// console.log("On app init recent:");
// console.log(result);
if (!result) {
// Ceate empty list
AsyncStorage.setItem('recentIds', JSON.stringify([]));
}
});
};
initRecent();
}, []);
// Component for Stack Navigator header
const StackHeader: FC<StackHeaderProps> = ({ navigation, title = "Calc's name" }) => {
return (
<Appbar.Header
mode="small"
style={{
marginTop: 7,
marginBottom: 7,
marginLeft: 3,
marginRight: 10,
}}
elevated={true}
>
<Appbar.BackAction onPress={() => navigation.goBack()} />
<Appbar.Content title={title} />
</Appbar.Header>
);
};
return (
<PaperProvider theme={activeTheme}>
<NavigationContainer>
<Stack.Navigator
screenOptions={{
header: (props) => <StackHeader {...props} />,
}}
>
<Stack.Screen
name="MainDrawer"
component={MainDrawer}
options={{ headerShown: false }}
/>
<Stack.Screen
name="OpenedScreen"
component={OpenedScreen}
options={() => ({
title: '',
headerTitle: '',
headerShown: false,
})}
/>
</Stack.Navigator>
</NavigationContainer>
</PaperProvider>
);
}
export default App;