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

View file

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

View file

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