Fix initial preferences loading

This commit is contained in:
Arnaud Vergnet 2021-05-18 18:43:14 +02:00
parent 7d0df0e7ce
commit c3304c6f06
3 changed files with 10 additions and 4 deletions

View file

@ -29,6 +29,7 @@ import { NavigationContainerRef } from '@react-navigation/core';
import {
defaultPreferences,
PreferenceKeys,
PreferencesType,
retrievePreferences,
} from './src/utils/asyncStorage';
import PreferencesProvider from './src/components/providers/PreferencesProvider';
@ -46,6 +47,7 @@ LogBox.ignoreLogs([
type StateType = {
isLoading: boolean;
initialPreferences: PreferencesType;
};
export default class App extends React.Component<{}, StateType> {
@ -61,6 +63,7 @@ export default class App extends React.Component<{}, StateType> {
super(props);
this.state = {
isLoading: true,
initialPreferences: defaultPreferences,
};
initLocales();
this.navigatorRef = React.createRef();
@ -103,9 +106,11 @@ export default class App extends React.Component<{}, StateType> {
/**
* Async loading is done, finish processing startup data
*/
onLoadFinished = () => {
onLoadFinished = (values: Array<PreferencesType | void>) => {
const [preferences] = values;
this.setState({
isLoading: false,
initialPreferences: { ...(preferences as PreferencesType) },
});
SplashScreen.hide();
};
@ -133,7 +138,7 @@ export default class App extends React.Component<{}, StateType> {
return null;
}
return (
<PreferencesProvider initialPreferences={defaultPreferences}>
<PreferencesProvider initialPreferences={this.state.initialPreferences}>
<MainApp
ref={this.navigatorRef}
defaultHomeData={this.defaultHomeData}

View file

@ -320,7 +320,6 @@ type PropsType = {
export default function MainNavigator(props: PropsType) {
const { preferences } = usePreferences();
const showIntro = getPreferenceBool(PreferenceKeys.showIntro, preferences);
console.log(preferences);
return (
<MainStackComponent

View file

@ -74,8 +74,10 @@ export function retrievePreferences(
defaults: PreferencesType
): Promise<PreferencesType> {
return new Promise((resolve: (preferences: PreferencesType) => void) => {
AsyncStorage.multiGet(Object.values(keys))
AsyncStorage.multiGet(keys)
.then((result) => {
console.log(result);
const preferences = { ...defaults };
result.forEach((item) => {
let [key, value] = item;