Files
notesnook/apps/mobile/src/components/AppLoader/index.js

129 lines
3.7 KiB
JavaScript
Raw Normal View History

2021-04-19 12:13:42 +05:00
import React, { useEffect, useState } from 'react';
import { View } from 'react-native';
import Animated, { Easing } from 'react-native-reanimated';
2021-02-25 12:16:38 +05:00
import AnimatedProgress from 'react-native-reanimated-progress-bar';
2021-04-19 12:13:42 +05:00
import { useTracked } from '../../provider';
import { Actions } from '../../provider/Actions';
2021-02-25 12:16:38 +05:00
import {
eSendEvent,
eSubscribeEvent,
2021-04-19 12:13:42 +05:00
eUnSubscribeEvent
2021-02-25 12:16:38 +05:00
} from '../../services/EventManager';
2021-05-24 16:01:48 +05:00
import { editing, InteractionManager } from '../../utils';
2021-04-19 12:13:42 +05:00
import { changeContainerScale, ContainerScale } from '../../utils/Animations';
import { db } from '../../utils/DB';
import { eOpenRateDialog, eOpenSideMenu } from '../../utils/Events';
import { MMKV } from '../../utils/mmkv';
import { tabBarRef } from '../../utils/Refs';
import { sleep } from '../../utils/TimeUtils';
import SplashScreen from '../SplashScreen';
2021-02-25 12:16:38 +05:00
const scaleV = new Animated.Value(0.95);
const opacityV = new Animated.Value(1);
const AppLoader = ({onLoad}) => {
const [state, dispatch] = useTracked();
const colors = state.colors;
const [loading, setLoading] = useState(true);
const [opacity, setOpacity] = useState(true);
const load = async value => {
2021-04-19 12:13:42 +05:00
console.log('loading called here');
2021-03-01 14:53:37 +05:00
if (value === 'hide') {
setLoading(true);
opacityV.setValue(1);
return;
}
let appState = await MMKV.getItem('appState');
if (appState) {
appState = JSON.parse(appState);
if (!appState.movedAway && Date.now() < appState.timestamp + 3600000) {
editing.isRestoringState = true;
//setNoteOnly(appState.note);
editing.currentlyEditing = true;
tabBarRef.current?.goToPage(1);
eSendEvent('loadingNote', appState.note);
}
}
2021-03-01 14:53:37 +05:00
if (value === 'show') {
opacityV.setValue(0);
2021-03-01 14:53:37 +05:00
setLoading(false);
return;
}
2021-02-25 12:16:38 +05:00
eSendEvent(eOpenSideMenu);
setOpacity(false);
2021-05-24 16:01:48 +05:00
setTimeout(() => {
Animated.timing(opacityV, {
toValue: 0,
duration: 100,
easing: Easing.out(Easing.ease),
}).start();
changeContainerScale(ContainerScale, 1, 600);
2021-05-24 16:01:48 +05:00
setTimeout(async ()=>{
setLoading(false);
await db.notes.init();
dispatch({type: Actions.NOTES});
dispatch({type: Actions.FAVORITES});
dispatch({type: Actions.LOADING, loading: false});
eSendEvent(eOpenSideMenu);
let askForRating = await MMKV.getItem('askForRating');
if (askForRating !== 'never' || askForRating !== 'completed') {
askForRating = JSON.parse(askForRating);
if (askForRating?.timestamp < Date.now()) {
eSendEvent(eOpenRateDialog);
}
}
},100)
},0);
2021-02-25 12:16:38 +05:00
};
useEffect(() => {
eSubscribeEvent('load_overlay', load);
onLoad();
return () => {
eUnSubscribeEvent('load_overlay', load);
};
}, []);
2021-04-19 12:13:42 +05:00
return loading ? (
<Animated.View
style={{
backgroundColor: opacity ? colors.bg : 'rgba(0,0,0,0)',
width: '100%',
height: '100%',
position: 'absolute',
zIndex: 999,
borderRadius: 10,
}}>
2021-02-25 12:16:38 +05:00
<Animated.View
2021-04-19 12:13:42 +05:00
onTouchStart={() => {
setLoading(false);
}}
2021-02-25 12:16:38 +05:00
style={{
2021-04-19 12:13:42 +05:00
backgroundColor: colors.bg,
2021-02-25 12:16:38 +05:00
width: '100%',
height: '100%',
2021-04-19 12:13:42 +05:00
justifyContent: 'center',
alignItems: 'center',
2021-02-25 12:16:38 +05:00
borderRadius: 10,
2021-04-19 12:13:42 +05:00
opacity: opacityV,
2021-02-25 12:16:38 +05:00
}}>
2021-04-19 12:13:42 +05:00
<View
2021-02-25 12:16:38 +05:00
style={{
2021-04-19 12:13:42 +05:00
height: 10,
flexDirection: 'row',
width: 100,
2021-02-25 12:16:38 +05:00
}}>
2021-04-19 12:13:42 +05:00
<AnimatedProgress fill={colors.accent} current={4} total={4} />
</View>
2021-02-25 12:16:38 +05:00
</Animated.View>
2021-04-19 12:13:42 +05:00
</Animated.View>
) : (
<SplashScreen />
2021-02-25 12:16:38 +05:00
);
};
export default AppLoader;