Massive/App.tsx

119 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-07-08 03:51:19 +00:00
import AsyncStorage from '@react-native-async-storage/async-storage';
2022-07-04 04:03:48 +00:00
import {createMaterialTopTabNavigator} from '@react-navigation/material-top-tabs';
2022-06-30 06:50:52 +00:00
import {
2022-07-03 01:50:01 +00:00
DarkTheme,
DefaultTheme,
NavigationContainer,
} from '@react-navigation/native';
import React, {useEffect, useState} from 'react';
2022-07-05 12:06:16 +00:00
import {StatusBar, useColorScheme} from 'react-native';
import {
DarkTheme as DarkThemePaper,
DefaultTheme as DefaultThemePaper,
2022-07-09 03:19:02 +00:00
IconButton,
Provider,
} from 'react-native-paper';
import {SQLiteDatabase} from 'react-native-sqlite-storage';
import Ionicon from 'react-native-vector-icons/Ionicons';
import BestPage from './BestPage';
2022-07-08 03:51:19 +00:00
import {createPlans, createSets, getDb} from './db';
import HomePage from './HomePage';
import PlanPage from './PlanPage';
import SettingsPage from './SettingsPage';
2022-07-03 01:50:01 +00:00
2022-07-04 04:03:48 +00:00
const Tab = createMaterialTopTabNavigator<RootStackParamList>();
2022-07-03 01:50:01 +00:00
export type RootStackParamList = {
Home: {};
Settings: {};
Best: {};
Plan: {};
2022-07-03 01:50:01 +00:00
};
export const DatabaseContext = React.createContext<SQLiteDatabase>({} as any);
2022-06-30 06:50:52 +00:00
2022-07-08 03:51:19 +00:00
const {getItem, setItem} = AsyncStorage;
2022-07-01 01:42:42 +00:00
const App = () => {
const [db, setDb] = useState<SQLiteDatabase | null>(null);
2022-07-01 01:42:42 +00:00
const dark = useColorScheme() === 'dark';
2022-07-03 01:50:01 +00:00
useEffect(() => {
2022-07-09 01:27:19 +00:00
const init = async () => {
const gotDb = await getDb();
await gotDb.executeSql(createPlans);
await gotDb.executeSql(createSets);
setDb(gotDb);
const minutes = await getItem('minutes');
if (minutes === null) await setItem('minutes', '3');
const seconds = await getItem('seconds');
if (seconds === null) await setItem('seconds', '30');
const alarmEnabled = await getItem('alarmEnabled');
if (alarmEnabled === null) await setItem('alarmEnabled', 'false');
2022-07-10 05:53:38 +00:00
if (!(await getItem('predictiveSets')))
await setItem('predictiveSets', 'true');
if (!(await getItem('maxSets'))) await setItem('maxSets', '3');
2022-07-09 01:27:19 +00:00
};
init();
2022-07-03 01:50:01 +00:00
}, []);
2022-06-30 06:50:52 +00:00
return (
<Provider
theme={dark ? DarkThemePaper : DefaultThemePaper}
settings={{icon: props => <Ionicon {...props} />}}>
<NavigationContainer theme={dark ? DarkTheme : DefaultTheme}>
<StatusBar barStyle={dark ? 'light-content' : 'dark-content'} />
{db && (
<DatabaseContext.Provider value={db}>
<Tab.Navigator>
2022-07-09 03:19:02 +00:00
<Tab.Screen
options={{
tabBarLabel: ({focused}) => (
<IconButton icon={focused ? 'home' : 'home-outline'} />
),
}}
name="Home"
component={HomePage}
/>
<Tab.Screen
options={{
tabBarLabel: ({focused}) => (
<IconButton
icon={focused ? 'calendar' : 'calendar-outline'}
/>
),
}}
name="Plan"
component={PlanPage}
/>
<Tab.Screen
options={{
tabBarLabel: ({focused}) => (
<IconButton
icon={focused ? 'bar-chart' : 'bar-chart-outline'}
/>
),
}}
name="Best"
component={BestPage}
/>
<Tab.Screen
options={{
tabBarLabel: ({focused}) => (
<IconButton
icon={focused ? 'settings' : 'settings-outline'}
/>
),
}}
name="Settings"
component={SettingsPage}
/>
</Tab.Navigator>
</DatabaseContext.Provider>
)}
</NavigationContainer>
</Provider>
2022-06-30 06:50:52 +00:00
);
};
export default App;