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';
|
2021-06-05 21:10:20 +05:00
|
|
|
import { useFavoriteStore, useNoteStore } from '../../provider/stores';
|
2021-06-10 12:52:25 +05:00
|
|
|
import { DDS } from '../../services/DeviceDetection';
|
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-06-07 11:53:27 +05:00
|
|
|
import { editing } 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 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);
|
2021-06-05 21:10:20 +05:00
|
|
|
const setNotes = useNoteStore(state => state.setNotes);
|
|
|
|
|
const setFavorites = useFavoriteStore(state => state.setFavorites);
|
|
|
|
|
const _setLoading = useNoteStore(state => state.setLoading)
|
2021-04-10 08:48:11 +05:00
|
|
|
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;
|
|
|
|
|
}
|
2021-04-17 15:27:16 +05:00
|
|
|
let appState = await MMKV.getItem('appState');
|
|
|
|
|
if (appState) {
|
|
|
|
|
appState = JSON.parse(appState);
|
2021-06-07 12:38:20 +05:00
|
|
|
if (appState.note && !appState.movedAway && Date.now() < appState.timestamp + 3600000) {
|
2021-06-10 12:52:25 +05:00
|
|
|
console.log('loading app state')
|
2021-04-17 15:27:16 +05:00
|
|
|
editing.isRestoringState = true;
|
|
|
|
|
//setNoteOnly(appState.note);
|
|
|
|
|
editing.currentlyEditing = true;
|
2021-06-08 12:50:41 +05:00
|
|
|
if (!DDS.isTab) {
|
|
|
|
|
tabBarRef.current?.goToPage(1);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-17 15:27:16 +05:00
|
|
|
eSendEvent('loadingNote', appState.note);
|
|
|
|
|
}
|
2021-06-10 12:52:25 +05:00
|
|
|
}
|
|
|
|
|
console.log('here again')
|
2021-04-17 15:27:16 +05:00
|
|
|
|
2021-03-01 14:53:37 +05:00
|
|
|
if (value === 'show') {
|
2021-06-10 12:52:25 +05:00
|
|
|
console.log('returning')
|
2021-04-03 14:06:40 +05:00
|
|
|
opacityV.setValue(0);
|
2021-03-01 14:53:37 +05:00
|
|
|
setLoading(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-10 12:52:25 +05:00
|
|
|
console.log('here now')
|
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-04-17 15:27:16 +05:00
|
|
|
|
2021-05-24 16:01:48 +05:00
|
|
|
setTimeout(async ()=>{
|
|
|
|
|
setLoading(false);
|
2021-06-10 12:52:25 +05:00
|
|
|
console.log('loading notes')
|
2021-05-24 16:01:48 +05:00
|
|
|
await db.notes.init();
|
2021-06-05 21:10:20 +05:00
|
|
|
setNotes();
|
2021-06-10 12:52:25 +05:00
|
|
|
console.log('setting notes')
|
2021-06-05 21:10:20 +05:00
|
|
|
setFavorites();
|
|
|
|
|
_setLoading(false);
|
2021-05-24 16:01:48 +05:00
|
|
|
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;
|