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

337 lines
9.6 KiB
JavaScript
Raw Normal View History

2021-11-18 14:52:07 +05:00
import React, {useEffect, useRef, useState} from 'react';
import {Appearance, Platform, SafeAreaView, 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-11-18 14:52:07 +05:00
import {useTracked} from '../../provider';
2021-06-11 09:16:04 +05:00
import {
useFavoriteStore,
2021-12-02 21:21:00 +05:00
useMessageStore,
2021-06-11 09:16:04 +05:00
useNoteStore,
2021-07-25 11:32:04 +05:00
useSettingStore,
useUserStore
2021-06-11 09:16:04 +05:00
} from '../../provider/stores';
2021-07-25 11:32:04 +05:00
import Backup from '../../services/Backup';
2021-06-11 09:16:04 +05:00
import BiometricService from '../../services/BiometricService';
2021-11-18 14:52:07 +05:00
import {DDS} from '../../services/DeviceDetection';
2021-02-25 12:16:38 +05:00
import {
eSendEvent,
eSubscribeEvent,
eUnSubscribeEvent,
2021-11-09 09:44:48 +05:00
presentSheet,
ToastEvent
2021-02-25 12:16:38 +05:00
} from '../../services/EventManager';
2021-11-15 15:24:48 +05:00
import PremiumService from '../../services/PremiumService';
2021-11-18 14:52:07 +05:00
import {editing} from '../../utils';
2021-12-30 10:46:09 +05:00
import {COLOR_SCHEME_DARK, COLOR_SCHEME_LIGHT} from '../../utils/Colors';
2021-11-18 14:52:07 +05:00
import {db} from '../../utils/database';
2021-12-02 21:21:00 +05:00
import {
eOpenAnnouncementDialog,
eOpenLoginDialog,
eOpenRateDialog
} from '../../utils/Events';
2021-11-18 14:52:07 +05:00
import {MMKV} from '../../utils/mmkv';
import {tabBarRef} from '../../utils/Refs';
import {SIZE} from '../../utils/SizeUtils';
import {sleep} from '../../utils/TimeUtils';
2021-11-24 10:13:42 +05:00
import SettingsBackupAndRestore from '../../views/Settings/backup-restore';
2021-11-18 14:52:07 +05:00
import {Button} from '../Button';
2021-06-11 09:16:04 +05:00
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-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-10-05 12:53:08 +05:00
const _loading = useNoteStore(state => state.loading);
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);
2021-11-18 14:52:07 +05:00
const deviceMode = useSettingStore(state => state.deviceMode);
2021-12-02 21:21:00 +05:00
const isIntroCompleted = useSettingStore(state => state.isIntroCompleted);
2021-08-17 11:49:02 +05:00
const pwdInput = useRef();
2021-06-11 09:16:04 +05:00
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;
}
2021-10-05 12:53:08 +05:00
await restoreEditorState();
if (value === 'show') {
opacityV.setValue(0);
setLoading(false);
return;
}
Animated.timing(opacityV, {
toValue: 0,
duration: 100,
easing: Easing.out(Easing.ease)
}).start();
setLoading(false);
await db.notes.init();
setNotes();
setFavorites();
_setLoading(false);
};
useEffect(() => {
if (!_loading) {
(async () => {
await sleep(500);
if ((await MMKV.getItem('loginSessionHasExpired')) === 'expired') {
eSendEvent(eOpenLoginDialog, 4);
return;
}
2021-12-02 21:21:00 +05:00
2021-10-05 12:53:08 +05:00
let settingsStore = useSettingStore.getState();
if (await Backup.checkBackupRequired(settingsStore.settings.reminder)) {
await Backup.checkAndRun();
return;
}
if (await checkForRateAppRequest()) return;
2021-11-11 13:09:20 +05:00
if (await checkNeedsBackup()) return;
2021-12-02 21:21:00 +05:00
if (await PremiumService.getRemainingTrialDaysStatus()) return;
await useMessageStore.getState().setAnnouncement();
if (isIntroCompleted) {
let dialogs = useMessageStore.getState().dialogs;
if (dialogs.length > 0) {
eSendEvent(eOpenAnnouncementDialog, dialogs[0]);
}
}
2021-10-05 12:53:08 +05:00
})();
}
}, [_loading]);
const restoreEditorState = async () => {
let appState = await MMKV.getItem('appState');
if (appState) {
appState = JSON.parse(appState);
2021-06-10 12:59:05 +05:00
if (
appState.note &&
2021-07-07 11:13:04 +05:00
!appState.note.locked &&
2021-06-10 12:59:05 +05:00
!appState.movedAway &&
Date.now() < appState.timestamp + 3600000
) {
editing.isRestoringState = true;
editing.currentlyEditing = true;
2021-07-07 11:13:04 +05:00
editing.movedAway = false;
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-10-05 12:53:08 +05:00
};
2021-10-05 12:53:08 +05:00
const checkForRateAppRequest = async () => {
let askForRating = await MMKV.getItem('askForRating');
if (askForRating !== 'never' || askForRating !== 'completed') {
askForRating = JSON.parse(askForRating);
if (askForRating?.timestamp < Date.now()) {
eSendEvent(eOpenRateDialog);
return true;
}
}
2021-10-05 12:53:08 +05:00
return false;
};
const checkNeedsBackup = async () => {
let settingsStore = useSettingStore.getState();
2021-07-25 11:32:04 +05:00
let askForBackup = await MMKV.getItem('askForBackup');
if (
settingsStore.settings.reminder === 'off' ||
!settingsStore.settings.reminder
2021-07-25 11:32:04 +05:00
) {
askForBackup = JSON.parse(askForBackup);
if (askForBackup?.timestamp < Date.now()) {
2021-11-09 09:44:48 +05:00
presentSheet({
2021-07-25 11:32:04 +05:00
title: 'Backup & restore',
paragraph: 'Please enable automatic backups to keep your data safe',
noProgress: true,
noIcon: true,
component: <SettingsBackupAndRestore isSheet={true} />
});
2021-10-05 12:53:08 +05:00
return true;
2021-06-10 13:16:22 +05:00
}
}
2021-10-05 12:53:08 +05:00
return false;
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 () => {
if (!(await BiometricService.isBiometryAvailable())) {
ToastEvent.show({
heading: 'Biometrics unavailable',
message: 'Try unlocking the app with your account password'
});
return;
}
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 09:16:04 +05:00
);
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-08-10 12:09:19 +05:00
backgroundColor:
Appearance.getColorScheme() === 'dark'
? COLOR_SCHEME_DARK.bg
: colors.bg,
2021-04-19 12:13:42 +05:00
width: '100%',
height: '100%',
position: 'absolute',
zIndex: 999,
borderRadius: 10
2021-04-19 12:13:42 +05:00
}}>
2021-02-25 12:16:38 +05:00
<Animated.View
style={{
2021-08-10 12:09:19 +05:00
backgroundColor:
Appearance.getColorScheme() === 'dark'
? COLOR_SCHEME_DARK.bg
: 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,
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',
2021-11-18 14:52:07 +05:00
width:
deviceMode !== 'mobile'
? '50%'
: Platform.OS == 'ios'
? '95%'
: '100%',
paddingHorizontal: 12
2021-06-11 09:16:04 +05:00
}}>
2021-11-18 14:52:07 +05:00
<Heading
style={{
alignSelf: 'center'
}}>
Verify your identity
</Heading>
2021-06-11 09:16:04 +05:00
{user ? (
<>
2021-11-18 14:52:07 +05:00
<Paragraph
style={{
alignSelf: 'center'
}}>
2021-06-11 09:16:04 +05:00
To keep your notes secure, please enter password of the
account you are logged in to.
</Paragraph>
2021-11-18 14:52:07 +05:00
<Seperator />
2021-06-11 09:16:04 +05:00
<Input
2021-08-17 11:49:02 +05:00
fwdRef={pwdInput}
2021-06-11 09:16:04 +05:00
secureTextEntry
placeholder="Enter account password"
onChangeText={v => (passwordValue = v)}
onSubmit={onSubmit}
/>
2021-11-18 14:52:07 +05:00
<Seperator half />
2021-06-11 09:16:04 +05:00
<Button
title="Unlock"
type="accent"
onPress={onSubmit}
width="100%"
height={50}
fontSize={SIZE.md}
/>
<Seperator />
</>
) : (
<>
2021-11-18 14:52:07 +05:00
<Paragraph
style={{
alignSelf: 'center'
}}>
2021-06-11 09:16:04 +05:00
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
2021-06-11 09:16:04 +05:00
}}>
2021-12-30 10:46:09 +05:00
<AnimatedProgress style={{
backgroundColor:Appearance.getColorScheme() === 'dark' ? COLOR_SCHEME_DARK.nav : COLOR_SCHEME_LIGHT.nav
}} fill={colors.accent} current={4} total={4} />
2021-06-11 09:16:04 +05:00
</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;