load all initial data at once

This commit is contained in:
Arnaud Vergnet 2020-10-07 10:42:08 +02:00
parent f1318c6aed
commit 7672dd109d
3 changed files with 45 additions and 45 deletions

22
App.tsx
View file

@ -80,9 +80,7 @@ export default class App extends React.Component<{}, StateType> {
this.urlHandler = new URLHandler(this.onInitialURLParsed, this.onDetectURL); this.urlHandler = new URLHandler(this.onInitialURLParsed, this.onDetectURL);
this.urlHandler.listen(); this.urlHandler.listen();
setSafeBounceHeight(Platform.OS === 'ios' ? 100 : 20); setSafeBounceHeight(Platform.OS === 'ios' ? 100 : 20);
this.loadAssetsAsync().finally(() => { this.loadAssetsAsync();
this.onLoadFinished();
});
} }
/** /**
@ -149,7 +147,7 @@ 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 = () => {
// Only show intro if this is the first time starting the app // Only show intro if this is the first time starting the app
ThemeManager.getInstance().setUpdateThemeCallback(this.onUpdateTheme); ThemeManager.getInstance().setUpdateThemeCallback(this.onUpdateTheme);
// Status bar goes dark if set too fast on ios // Status bar goes dark if set too fast on ios
@ -176,19 +174,21 @@ export default class App extends React.Component<{}, StateType> {
), ),
}); });
SplashScreen.hide(); SplashScreen.hide();
} };
/** /**
* Loads every async data * Loads every async data
* *
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
loadAssetsAsync = async () => { loadAssetsAsync() {
await AsyncStorageManager.getInstance().loadPreferences(); Promise.all([
await ConnectionManager.getInstance() AsyncStorageManager.getInstance().loadPreferences(),
.recoverLogin() ConnectionManager.getInstance().recoverLogin(),
.catch(() => {}); ])
}; .then(this.onLoadFinished)
.catch(this.onLoadFinished);
}
/** /**
* Renders the app based on loading state * Renders the app based on loading state

View file

@ -209,13 +209,14 @@ export default class AsyncStorageManager {
* @return {Promise<void>} * @return {Promise<void>}
*/ */
async loadPreferences() { async loadPreferences() {
return new Promise((resolve: () => void) => {
const prefKeys: Array<string> = []; const prefKeys: Array<string> = [];
// Get all available keys // Get all available keys
Object.keys(AsyncStorageManager.PREFERENCES).forEach((key: string) => { Object.keys(AsyncStorageManager.PREFERENCES).forEach((key: string) => {
prefKeys.push(key); prefKeys.push(key);
}); });
// Get corresponding values // Get corresponding values
const resultArray = await AsyncStorage.multiGet(prefKeys); AsyncStorage.multiGet(prefKeys).then((resultArray) => {
// Save those values for later use // Save those values for later use
resultArray.forEach((item: [string, string | null]) => { resultArray.forEach((item: [string, string | null]) => {
const key = item[0]; const key = item[0];
@ -225,6 +226,9 @@ export default class AsyncStorageManager {
} }
this.currentPreferences[key] = val; this.currentPreferences[key] = val;
}); });
resolve();
});
});
} }
/** /**

View file

@ -73,25 +73,21 @@ export default class ConnectionManager {
* @returns Promise<string> * @returns Promise<string>
*/ */
async recoverLogin(): Promise<string> { async recoverLogin(): Promise<string> {
return new Promise( return new Promise((resolve: () => void) => {
(resolve: (token: string) => void, reject: () => void) => {
const token = this.getToken(); const token = this.getToken();
if (token != null) { if (token != null) {
resolve(token); resolve();
} else { } else {
Keychain.getInternetCredentials(SERVER_NAME) Keychain.getInternetCredentials(SERVER_NAME)
.then((data: Keychain.UserCredentials | false) => { .then((data: Keychain.UserCredentials | false) => {
if (data && data.password != null) { if (data && data.password != null) {
this.token = data.password; this.token = data.password;
resolve(this.token);
} else {
reject();
} }
resolve();
}) })
.catch((): void => reject()); .catch(resolve);
} }
}, });
);
} }
/** /**