Browse Source

Fix initial preferences loading

Arnaud Vergnet 2 years ago
parent
commit
c3304c6f06
3 changed files with 10 additions and 4 deletions
  1. 7
    2
      App.tsx
  2. 0
    1
      src/navigation/MainNavigator.tsx
  3. 3
    1
      src/utils/asyncStorage.ts

+ 7
- 2
App.tsx View File

@@ -29,6 +29,7 @@ import { NavigationContainerRef } from '@react-navigation/core';
29 29
 import {
30 30
   defaultPreferences,
31 31
   PreferenceKeys,
32
+  PreferencesType,
32 33
   retrievePreferences,
33 34
 } from './src/utils/asyncStorage';
34 35
 import PreferencesProvider from './src/components/providers/PreferencesProvider';
@@ -46,6 +47,7 @@ LogBox.ignoreLogs([
46 47
 
47 48
 type StateType = {
48 49
   isLoading: boolean;
50
+  initialPreferences: PreferencesType;
49 51
 };
50 52
 
51 53
 export default class App extends React.Component<{}, StateType> {
@@ -61,6 +63,7 @@ export default class App extends React.Component<{}, StateType> {
61 63
     super(props);
62 64
     this.state = {
63 65
       isLoading: true,
66
+      initialPreferences: defaultPreferences,
64 67
     };
65 68
     initLocales();
66 69
     this.navigatorRef = React.createRef();
@@ -103,9 +106,11 @@ export default class App extends React.Component<{}, StateType> {
103 106
   /**
104 107
    * Async loading is done, finish processing startup data
105 108
    */
106
-  onLoadFinished = () => {
109
+  onLoadFinished = (values: Array<PreferencesType | void>) => {
110
+    const [preferences] = values;
107 111
     this.setState({
108 112
       isLoading: false,
113
+      initialPreferences: { ...(preferences as PreferencesType) },
109 114
     });
110 115
     SplashScreen.hide();
111 116
   };
@@ -133,7 +138,7 @@ export default class App extends React.Component<{}, StateType> {
133 138
       return null;
134 139
     }
135 140
     return (
136
-      <PreferencesProvider initialPreferences={defaultPreferences}>
141
+      <PreferencesProvider initialPreferences={this.state.initialPreferences}>
137 142
         <MainApp
138 143
           ref={this.navigatorRef}
139 144
           defaultHomeData={this.defaultHomeData}

+ 0
- 1
src/navigation/MainNavigator.tsx View File

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

+ 3
- 1
src/utils/asyncStorage.ts View File

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

Loading…
Cancel
Save