forked from vergnet/application-amicale
		
	Fix initial preferences loading
This commit is contained in:
		
							parent
							
								
									7d0df0e7ce
								
							
						
					
					
						commit
						c3304c6f06
					
				
					 3 changed files with 10 additions and 4 deletions
				
			
		
							
								
								
									
										9
									
								
								App.tsx
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								App.tsx
									
									
									
									
									
								
							|  | @ -29,6 +29,7 @@ import { NavigationContainerRef } from '@react-navigation/core'; | ||||||
| import { | import { | ||||||
|   defaultPreferences, |   defaultPreferences, | ||||||
|   PreferenceKeys, |   PreferenceKeys, | ||||||
|  |   PreferencesType, | ||||||
|   retrievePreferences, |   retrievePreferences, | ||||||
| } from './src/utils/asyncStorage'; | } from './src/utils/asyncStorage'; | ||||||
| import PreferencesProvider from './src/components/providers/PreferencesProvider'; | import PreferencesProvider from './src/components/providers/PreferencesProvider'; | ||||||
|  | @ -46,6 +47,7 @@ LogBox.ignoreLogs([ | ||||||
| 
 | 
 | ||||||
| type StateType = { | type StateType = { | ||||||
|   isLoading: boolean; |   isLoading: boolean; | ||||||
|  |   initialPreferences: PreferencesType; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| export default class App extends React.Component<{}, StateType> { | export default class App extends React.Component<{}, StateType> { | ||||||
|  | @ -61,6 +63,7 @@ export default class App extends React.Component<{}, StateType> { | ||||||
|     super(props); |     super(props); | ||||||
|     this.state = { |     this.state = { | ||||||
|       isLoading: true, |       isLoading: true, | ||||||
|  |       initialPreferences: defaultPreferences, | ||||||
|     }; |     }; | ||||||
|     initLocales(); |     initLocales(); | ||||||
|     this.navigatorRef = React.createRef(); |     this.navigatorRef = React.createRef(); | ||||||
|  | @ -103,9 +106,11 @@ export default class App extends React.Component<{}, StateType> { | ||||||
|   /** |   /** | ||||||
|    * Async loading is done, finish processing startup data |    * Async loading is done, finish processing startup data | ||||||
|    */ |    */ | ||||||
|   onLoadFinished = () => { |   onLoadFinished = (values: Array<PreferencesType | void>) => { | ||||||
|  |     const [preferences] = values; | ||||||
|     this.setState({ |     this.setState({ | ||||||
|       isLoading: false, |       isLoading: false, | ||||||
|  |       initialPreferences: { ...(preferences as PreferencesType) }, | ||||||
|     }); |     }); | ||||||
|     SplashScreen.hide(); |     SplashScreen.hide(); | ||||||
|   }; |   }; | ||||||
|  | @ -133,7 +138,7 @@ export default class App extends React.Component<{}, StateType> { | ||||||
|       return null; |       return null; | ||||||
|     } |     } | ||||||
|     return ( |     return ( | ||||||
|       <PreferencesProvider initialPreferences={defaultPreferences}> |       <PreferencesProvider initialPreferences={this.state.initialPreferences}> | ||||||
|         <MainApp |         <MainApp | ||||||
|           ref={this.navigatorRef} |           ref={this.navigatorRef} | ||||||
|           defaultHomeData={this.defaultHomeData} |           defaultHomeData={this.defaultHomeData} | ||||||
|  |  | ||||||
|  | @ -320,7 +320,6 @@ type PropsType = { | ||||||
| export default function MainNavigator(props: PropsType) { | export default function MainNavigator(props: PropsType) { | ||||||
|   const { preferences } = usePreferences(); |   const { preferences } = usePreferences(); | ||||||
|   const showIntro = getPreferenceBool(PreferenceKeys.showIntro, preferences); |   const showIntro = getPreferenceBool(PreferenceKeys.showIntro, preferences); | ||||||
|   console.log(preferences); |  | ||||||
| 
 | 
 | ||||||
|   return ( |   return ( | ||||||
|     <MainStackComponent |     <MainStackComponent | ||||||
|  |  | ||||||
|  | @ -74,8 +74,10 @@ export function retrievePreferences( | ||||||
|   defaults: PreferencesType |   defaults: PreferencesType | ||||||
| ): Promise<PreferencesType> { | ): Promise<PreferencesType> { | ||||||
|   return new Promise((resolve: (preferences: PreferencesType) => void) => { |   return new Promise((resolve: (preferences: PreferencesType) => void) => { | ||||||
|     AsyncStorage.multiGet(Object.values(keys)) |     AsyncStorage.multiGet(keys) | ||||||
|       .then((result) => { |       .then((result) => { | ||||||
|  |         console.log(result); | ||||||
|  | 
 | ||||||
|         const preferences = { ...defaults }; |         const preferences = { ...defaults }; | ||||||
|         result.forEach((item) => { |         result.forEach((item) => { | ||||||
|           let [key, value] = item; |           let [key, value] = item; | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue