load all initial data at once
This commit is contained in:
parent
f1318c6aed
commit
7672dd109d
3 changed files with 45 additions and 45 deletions
22
App.tsx
22
App.tsx
|
@ -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
|
||||||
|
|
|
@ -209,21 +209,25 @@ export default class AsyncStorageManager {
|
||||||
* @return {Promise<void>}
|
* @return {Promise<void>}
|
||||||
*/
|
*/
|
||||||
async loadPreferences() {
|
async loadPreferences() {
|
||||||
const prefKeys: Array<string> = [];
|
return new Promise((resolve: () => void) => {
|
||||||
// Get all available keys
|
const prefKeys: Array<string> = [];
|
||||||
Object.keys(AsyncStorageManager.PREFERENCES).forEach((key: string) => {
|
// Get all available keys
|
||||||
prefKeys.push(key);
|
Object.keys(AsyncStorageManager.PREFERENCES).forEach((key: string) => {
|
||||||
});
|
prefKeys.push(key);
|
||||||
// Get corresponding values
|
});
|
||||||
const resultArray = await AsyncStorage.multiGet(prefKeys);
|
// Get corresponding values
|
||||||
// Save those values for later use
|
AsyncStorage.multiGet(prefKeys).then((resultArray) => {
|
||||||
resultArray.forEach((item: [string, string | null]) => {
|
// Save those values for later use
|
||||||
const key = item[0];
|
resultArray.forEach((item: [string, string | null]) => {
|
||||||
let val = item[1];
|
const key = item[0];
|
||||||
if (val === null) {
|
let val = item[1];
|
||||||
val = AsyncStorageManager.PREFERENCES[key].default;
|
if (val === null) {
|
||||||
}
|
val = AsyncStorageManager.PREFERENCES[key].default;
|
||||||
this.currentPreferences[key] = val;
|
}
|
||||||
|
this.currentPreferences[key] = val;
|
||||||
|
});
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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();
|
||||||
resolve(token);
|
} 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);
|
resolve();
|
||||||
} else {
|
})
|
||||||
reject();
|
.catch(resolve);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
.catch((): void => reject());
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue