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

126 lines
3.4 KiB
JavaScript
Raw Normal View History

2021-06-10 12:59:05 +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-06-10 12:59:05 +05:00
import {useTracked} from '../../provider';
import {useFavoriteStore, useNoteStore} from '../../provider/stores';
import {DDS} from '../../services/DeviceDetection';
2021-02-25 12:16:38 +05:00
import {
eSendEvent,
eSubscribeEvent,
2021-06-10 12:59:05 +05:00
eUnSubscribeEvent,
2021-02-25 12:16:38 +05:00
} from '../../services/EventManager';
2021-06-10 12:59:05 +05:00
import {editing} from '../../utils';
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';
2021-04-19 12:13:42 +05:00
import SplashScreen from '../SplashScreen';
2021-02-25 12:16:38 +05:00
const opacityV = new Animated.Value(1);
const AppLoader = ({onLoad}) => {
const [state, dispatch] = useTracked();
const colors = state.colors;
const [loading, setLoading] = useState(true);
2021-06-05 21:10:20 +05:00
const setNotes = useNoteStore(state => state.setNotes);
const setFavorites = useFavoriteStore(state => state.setFavorites);
2021-06-10 12:59:05 +05:00
const _setLoading = useNoteStore(state => state.setLoading);
const load = async value => {
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);
2021-06-10 12:59:05 +05:00
if (
appState.note &&
!appState.movedAway &&
Date.now() < appState.timestamp + 3600000
) {
editing.isRestoringState = true;
editing.currentlyEditing = true;
2021-06-08 12:50:41 +05:00
if (!DDS.isTab) {
tabBarRef.current?.goToPage(1);
}
eSendEvent('loadingNote', appState.note);
}
2021-06-10 12:59:05 +05:00
}
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);
2021-06-10 13:16:22 +05:00
Animated.timing(opacityV, {
toValue: 0,
duration: 100,
easing: Easing.out(Easing.ease),
}).start();
setLoading(false);
await db.notes.init();
setNotes();
setFavorites();
_setLoading(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);
}
}
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={{
2021-06-10 13:16:22 +05:00
backgroundColor: colors.bg,
2021-04-19 12:13:42 +05:00
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;