From 70113f1b9490456e8e8bee35f9233f778950b7b7 Mon Sep 17 00:00:00 2001 From: Brandon Presley Date: Fri, 8 Jul 2022 15:51:19 +1200 Subject: [PATCH] Simplify AsyncStorage usage in App.tsx --- App.tsx | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/App.tsx b/App.tsx index 3c991f9..30a457d 100644 --- a/App.tsx +++ b/App.tsx @@ -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({} as any); +const {getItem, setItem} = AsyncStorage; + const App = () => { const [db, setDb] = useState(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(() => {