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

223 lines
6.1 KiB
JavaScript
Raw Normal View History

2021-06-10 12:59:05 +05:00
import React, {useEffect, useState} from 'react';
2021-06-11 09:16:04 +05:00
import {SafeAreaView, View} from 'react-native';
2021-06-10 12:59:05 +05:00
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';
2021-06-11 09:16:04 +05:00
import {
useFavoriteStore,
useNoteStore,
useUserStore,
} from '../../provider/stores';
import BiometricService from '../../services/BiometricService';
2021-06-10 12:59:05 +05:00
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 {db} from '../../utils/DB';
2021-06-15 18:26:46 +05:00
import {eOpenRateDialog} from '../../utils/Events';
2021-06-10 12:59:05 +05:00
import {MMKV} from '../../utils/mmkv';
import {tabBarRef} from '../../utils/Refs';
2021-06-11 09:16:04 +05:00
import {SIZE} from '../../utils/SizeUtils';
import {Button} from '../Button';
import Input from '../Input';
import Seperator from '../Seperator';
2021-04-19 12:13:42 +05:00
import SplashScreen from '../SplashScreen';
2021-06-11 09:16:04 +05:00
import Heading from '../Typography/Heading';
import Paragraph from '../Typography/Paragraph';
2021-02-25 12:16:38 +05:00
2021-06-11 09:16:04 +05:00
let passwordValue = null;
2021-06-11 10:38:35 +05:00
let didVerifyUser = false;
2021-02-25 12:16:38 +05:00
const opacityV = new Animated.Value(1);
const AppLoader = ({onLoad}) => {
2021-06-26 08:47:52 +05:00
const [state] = useTracked();
2021-02-25 12:16:38 +05:00
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);
2021-06-26 08:47:52 +05:00
const user = useUserStore(state => state.user);
2021-06-11 09:16:04 +05:00
const verifyUser = useUserStore(state => state.verifyUser);
const setVerifyUser = useUserStore(state => state.setVerifyUser);
const load = async value => {
2021-06-11 09:16:04 +05:00
if (verifyUser) return;
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-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);
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
};
2021-06-11 09:16:04 +05:00
useEffect(() => {
2021-06-14 14:41:57 +05:00
eSubscribeEvent('load_overlay', load);
2021-06-11 09:16:04 +05:00
if (!verifyUser) {
2021-06-11 12:14:07 +05:00
if (!didVerifyUser) {
onLoad();
2021-06-14 14:41:57 +05:00
} else {
load();
2021-06-11 12:14:07 +05:00
}
2021-06-11 09:16:04 +05:00
}
if (verifyUser) {
onUnlockBiometrics();
}
2021-02-25 12:16:38 +05:00
return () => {
eUnSubscribeEvent('load_overlay', load);
};
2021-06-11 09:16:04 +05:00
}, [verifyUser]);
const onUnlockBiometrics = async () => {
2021-06-11 10:38:35 +05:00
let verified = await BiometricService.validateUser(
2021-06-11 09:16:04 +05:00
'Unlock to access your notes',
'',
);
2021-06-11 10:38:35 +05:00
if (verified) {
didVerifyUser = true;
2021-06-11 09:16:04 +05:00
setVerifyUser(false);
2021-06-11 10:38:35 +05:00
passwordValue = null;
2021-06-11 09:16:04 +05:00
}
};
const onSubmit = async () => {
if (!passwordValue) return;
try {
let verified = await db.user.verifyPassword(passwordValue);
if (verified) {
2021-06-11 10:38:35 +05:00
didVerifyUser = true;
2021-06-11 09:16:04 +05:00
setVerifyUser(false);
2021-06-11 10:38:35 +05:00
passwordValue = null;
2021-06-11 09:16:04 +05:00
}
} catch (e) {}
};
2021-02-25 12:16:38 +05:00
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
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-06-11 09:16:04 +05:00
{verifyUser ? (
<SafeAreaView
style={{
flex: 1,
justifyContent: 'center',
width: '100%',
paddingHorizontal: 12,
}}>
<Heading>Verify your identity</Heading>
{user ? (
<>
<Paragraph>
To keep your notes secure, please enter password of the
account you are logged in to.
</Paragraph>
<Input
secureTextEntry
placeholder="Enter account password"
onChangeText={v => (passwordValue = v)}
onSubmit={onSubmit}
/>
<Button
title="Unlock"
type="accent"
onPress={onSubmit}
width="100%"
height={50}
fontSize={SIZE.md}
/>
<Seperator />
</>
) : (
<>
<Paragraph>
To keep your notes secure, please unlock app the with
biometrics.
</Paragraph>
<Seperator />
</>
)}
<Button
title="Unlock with Biometrics"
width="100%"
height={50}
onPress={onUnlockBiometrics}
icon={'fingerprint'}
type={!user ? 'accent' : 'transparent'}
fontSize={SIZE.md}
/>
</SafeAreaView>
) : (
<View
style={{
height: 10,
flexDirection: 'row',
width: 100,
}}>
<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;