Commit 10c3b3c7 authored by Martin Santangelo's avatar Martin Santangelo

(feat) optimize app init speed

parent bb7275ee
......@@ -81,8 +81,9 @@ sessionService.onLogin(async () => {
});
logService.info('[App] Getting minds settings and onboarding progress');
// load minds settings and onboarding progresss on login
const results = await Promise.all([mindsService.getSettings(), stores.onboarding.getProgress(), boostedContentService.load()]);
// load minds settings and boosted content
await Promise.all([mindsService.getSettings(), boostedContentService.load()]);
logService.info('[App] updatting features');
// reload fatures on login
......@@ -92,16 +93,12 @@ sessionService.onLogin(async () => {
pushService.registerToken();
// get onboarding progress
const onboarding = results[1];
if (onboarding && onboarding.show_onboarding) {
sessionService.setInitialScreen('OnboardingScreen');
}
logService.info('[App] navigating to initial screen', sessionService.initialScreen);
NavigationService.navigate(sessionService.initialScreen);
// check onboarding progress and navigate if necessary
stores.onboarding.getProgress();
// check update
if (Platform.OS !== 'ios' && !GOOGLE_PLAY_STORE) {
setTimeout(async () => {
......@@ -199,8 +196,9 @@ export default class App extends Component<Props, State> {
*/
async componentDidMount() {
try {
// load app setting before start
const results = await Promise.all([settingsStore.init(), await Linking.getInitialURL()]),
const results = await Promise.all([settingsStore.init(), await Linking.getInitialURL()]);
deepLinkUrl = results[1];
......@@ -210,6 +208,7 @@ export default class App extends Component<Props, State> {
if (!this.handlePasswordResetDeepLink()) {
logService.info('[App] initializing session');
const token = await sessionService.init();
if (!token) {
......
......@@ -14,6 +14,7 @@ describe('Boosted content service', () => {
const fakeBoosts = [{guid: 1}, {guid: 2}, {guid: 3}];
boostedContentService.feedsService.getEntities.mockResolvedValue(fakeBoosts);
boostedContentService.feedsService.fetchLocal.mockResolvedValue(true);
// load the boosts
await boostedContentService.load();
......@@ -22,7 +23,7 @@ describe('Boosted content service', () => {
expect(boostedContentService.feedsService.setEndpoint).toBeCalledWith('api/v2/boost/feed');
expect(boostedContentService.feedsService.setOffset).toBeCalledWith(0);
expect(boostedContentService.feedsService.setLimit).toBeCalledWith(12);
expect(boostedContentService.feedsService.fetchRemoteOrLocal).toBeCalled();
expect(boostedContentService.feedsService.fetchLocal).toBeCalled();
// should fetch the boosts entities
expect(boostedContentService.feedsService.getEntities).toBeCalled();
......
......@@ -21,19 +21,34 @@ class BoostedContentService {
*/
load = async(): Promise<any> => {
try {
await this.feedsService
const done = await this.feedsService
.setLimit(12)
.setOffset(0)
.setPaginated(false)
.setEndpoint('api/v2/boost/feed')
.fetchRemoteOrLocal();
.fetchLocal();
if (!done) {
await this.update();
} else {
this.boosts = await this.feedsService.getEntities();
// refresh boost without the wait
this.update();
}
this.boosts = await this.feedsService.getEntities();
} catch (err) {
logService.exception('[BoostedContentService]', err);
}
}
/**
* Update boosted content from server
*/
async update() {
await this.feedsService.fetchRemote();
this.boosts = await this.feedsService.getEntities();
}
/**
* Fetch one boost
*/
......
......@@ -14,6 +14,12 @@ class MindsService {
*/
loadDefault = () => require("../../../settings/default.json");
async update() {
const settings = await api.get('api/v1/minds/config');
AsyncStorage.setItem('@MindsSettings', JSON.stringify(settings));
this.settings = this.settings;
}
/**
* Get settings
*/
......@@ -21,31 +27,14 @@ class MindsService {
let settings;
if (!this.settings) {
try {
settings = await api.get('api/v1/minds/config');
settings = JSON.parse(await AsyncStorage.getItem('@MindsSettings'));
if (!settings) throw Error('No settings stored');
} catch {
settings = this.loadDefault();
AsyncStorage.setItem('@MindsSettings', JSON.stringify(settings));
} catch (err) {
try {
settings = JSON.parse(await AsyncStorage.getItem('@MindsSettings'));
if (!settings) throw Error('No settings stored');
} catch {
settings = this.loadDefault();
AsyncStorage.setItem('@MindsSettings', JSON.stringify(settings));
}
}
if (settings) {
this.settings = settings;
} else {
return await new Promise(resolve => {
Alert.alert(
i18n.t('error'),
i18n.t('mindsSettings.error'),
[
{ text: i18n.t('retry'), onPress: async () => resolve(await this.getSettings()) }
]
);
});
}
this.settings = settings;
this.update();
}
return this.settings;
......
......@@ -4,6 +4,7 @@ import number from '../common/helpers/number';
import OffsetListStore from '../common/stores/OffsetListStore';
import logService from '../common/services/log.service';
import UserModel from '../channel/UserModel';
import NavigationService from '../navigation/NavigationService';
/**
* Onboarding store
......@@ -36,6 +37,9 @@ class OnboardingStore {
try {
const progress = await onboardingService.getProgress();
this.setProgress(progress);
if (progress && progress.show_onboarding) {
NavigationService.push('OnboardingScreen');
}
return progress;
} catch (err) {
logService.exception(err);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment