Simplify AsyncStorage usage in App.tsx

This commit is contained in:
Brandon Presley 2022-07-08 15:51:19 +12:00
parent d73a88ed15
commit 70113f1b94
1 changed files with 10 additions and 12 deletions

22
App.tsx
View File

@ -1,4 +1,4 @@
import {useAsyncStorage} from '@react-native-async-storage/async-storage';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {createMaterialTopTabNavigator} from '@react-navigation/material-top-tabs';
import {
DarkTheme,
@ -14,8 +14,8 @@ import {
} from 'react-native-paper';
import {SQLiteDatabase} from 'react-native-sqlite-storage';
import Ionicon from 'react-native-vector-icons/Ionicons';
import {createPlans, createSets, getDb} from './db';
import BestPage from './BestPage';
import {createPlans, createSets, getDb} from './db';
import HomePage from './HomePage';
import PlanPage from './PlanPage';
import SettingsPage from './SettingsPage';
@ -30,25 +30,23 @@ export type RootStackParamList = {
export const DatabaseContext = React.createContext<SQLiteDatabase>({} as any);
const {getItem, setItem} = AsyncStorage;
const App = () => {
const [db, setDb] = useState<SQLiteDatabase | null>(null);
const dark = useColorScheme() === 'dark';
const {getItem: getMinutes, setItem: setMinutes} = useAsyncStorage('minutes');
const {getItem: getSeconds, setItem: setSeconds} = useAsyncStorage('seconds');
const {getItem: getAlarmEnabled, setItem: setAlarmEnabled} =
useAsyncStorage('alarmEnabled');
const init = async () => {
const gotDb = await getDb();
await gotDb.executeSql(createPlans);
await gotDb.executeSql(createSets);
setDb(gotDb);
const minutes = await getMinutes();
if (minutes === null) await setMinutes('3');
const seconds = await getSeconds();
if (seconds === null) await setSeconds('30');
const alarmEnabled = await getAlarmEnabled();
if (alarmEnabled === null) await setAlarmEnabled('false');
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');
};
useEffect(() => {