2022-01-22 12:57:05 +05:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
2022-01-25 10:25:23 +05:00
|
|
|
import { Appearance, NativeModules, Platform, SafeAreaView } from 'react-native';
|
2022-01-25 18:29:13 +05:00
|
|
|
import { NavigationBar } from 'react-native-bars';
|
2022-01-25 10:25:23 +05:00
|
|
|
import RNBootSplash from 'react-native-bootsplash';
|
|
|
|
|
import { checkVersion } from 'react-native-check-version';
|
|
|
|
|
import Animated from 'react-native-reanimated';
|
2022-01-22 12:57:05 +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,
|
2021-07-24 11:03:50 +05:00
|
|
|
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';
|
2022-01-22 12:57:05 +05:00
|
|
|
import { DDS } from '../../services/DeviceDetection';
|
2021-02-25 12:16:38 +05:00
|
|
|
import {
|
|
|
|
|
eSendEvent,
|
|
|
|
|
eSubscribeEvent,
|
2021-08-04 09:18:04 +05:00
|
|
|
eUnSubscribeEvent,
|
2021-11-09 09:44:48 +05:00
|
|
|
presentSheet,
|
2021-08-04 09:18:04 +05:00
|
|
|
ToastEvent
|
2021-02-25 12:16:38 +05:00
|
|
|
} from '../../services/EventManager';
|
2022-01-25 10:25:23 +05:00
|
|
|
import { setRateAppMessage } from '../../services/Message';
|
2021-11-15 15:24:48 +05:00
|
|
|
import PremiumService from '../../services/PremiumService';
|
2022-01-25 10:25:23 +05:00
|
|
|
import { editing } from '../../utils';
|
|
|
|
|
import { COLOR_SCHEME_DARK } from '../../utils/Colors';
|
2022-01-22 12:57:05 +05:00
|
|
|
import { db } from '../../utils/database';
|
2022-01-25 10:25:23 +05:00
|
|
|
import { eOpenAnnouncementDialog, eOpenLoginDialog } from '../../utils/Events';
|
2022-01-22 12:57:05 +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';
|
2022-01-22 12:57:05 +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';
|
2022-01-22 12:57:05 +05:00
|
|
|
import { Update } from '../Update';
|
2021-10-05 12:39:56 +05:00
|
|
|
|
2021-06-11 09:16:04 +05:00
|
|
|
let passwordValue = null;
|
2021-06-11 10:38:35 +05:00
|
|
|
let didVerifyUser = false;
|
2022-01-25 10:25:23 +05:00
|
|
|
|
2022-01-22 12:57:05 +05:00
|
|
|
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;
|
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-08-17 11:49:02 +05:00
|
|
|
const pwdInput = useRef();
|
2022-01-25 10:25:23 +05:00
|
|
|
const [requireIntro, setRequireIntro] = useState({
|
|
|
|
|
updated: false,
|
|
|
|
|
value: false
|
|
|
|
|
});
|
2021-06-11 09:16:04 +05:00
|
|
|
|
2022-01-25 10:25:23 +05:00
|
|
|
const load = async () => {
|
|
|
|
|
if (verifyUser) {
|
2021-03-01 14:53:37 +05:00
|
|
|
return;
|
|
|
|
|
}
|
2021-10-05 12:53:08 +05:00
|
|
|
await restoreEditorState();
|
|
|
|
|
await db.notes.init();
|
|
|
|
|
setNotes();
|
|
|
|
|
setFavorites();
|
|
|
|
|
_setLoading(false);
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-25 10:25:23 +05:00
|
|
|
const hideSplashScreen = async () => {
|
|
|
|
|
await sleep(requireIntro.value ? 500 : 0);
|
|
|
|
|
await RNBootSplash.hide({ fade: true });
|
|
|
|
|
setTimeout(async () => {
|
|
|
|
|
NativeModules.RNBars.setStatusBarStyle(!colors.night ? 'light-content' : 'dark-content');
|
|
|
|
|
await sleep(5);
|
|
|
|
|
NativeModules.RNBars.setStatusBarStyle(colors.night ? 'light-content' : 'dark-content');
|
|
|
|
|
}, 500);
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-05 12:53:08 +05:00
|
|
|
useEffect(() => {
|
2022-01-25 10:25:23 +05:00
|
|
|
if (requireIntro.updated) {
|
|
|
|
|
hideSplashScreen();
|
|
|
|
|
}
|
|
|
|
|
}, [requireIntro, verifyUser]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
(async () => {
|
|
|
|
|
let introCompleted = await MMKV.getItem('introCompleted');
|
|
|
|
|
setRequireIntro({
|
|
|
|
|
updated: true,
|
|
|
|
|
value: !introCompleted
|
|
|
|
|
});
|
|
|
|
|
})();
|
2021-10-05 12:53:08 +05:00
|
|
|
if (!_loading) {
|
|
|
|
|
(async () => {
|
|
|
|
|
await sleep(500);
|
|
|
|
|
if ((await MMKV.getItem('loginSessionHasExpired')) === 'expired') {
|
2022-02-07 14:44:48 +05:00
|
|
|
eSendEvent('session_expired');
|
2021-10-05 12:53:08 +05:00
|
|
|
return;
|
|
|
|
|
}
|
2021-12-02 21:21:00 +05:00
|
|
|
|
2022-01-04 13:08:02 +05:00
|
|
|
if (await checkAppUpdateAvailable()) return;
|
2021-10-05 12:53:08 +05:00
|
|
|
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();
|
2022-01-25 10:25:23 +05:00
|
|
|
if (!requireIntro) {
|
2021-12-02 21:21:00 +05:00
|
|
|
let dialogs = useMessageStore.getState().dialogs;
|
|
|
|
|
if (dialogs.length > 0) {
|
|
|
|
|
eSendEvent(eOpenAnnouncementDialog, dialogs[0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-05 12:53:08 +05:00
|
|
|
})();
|
|
|
|
|
}
|
|
|
|
|
}, [_loading]);
|
|
|
|
|
|
2022-01-04 13:08:02 +05:00
|
|
|
const checkAppUpdateAvailable = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const version = await checkVersion();
|
|
|
|
|
if (!version.needsUpdate) return false;
|
|
|
|
|
presentSheet({
|
2022-01-05 12:59:50 +05:00
|
|
|
component: ref => <Update version={version} fwdRef={ref} />
|
2022-01-04 13:08:02 +05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-05 12:53:08 +05:00
|
|
|
const restoreEditorState = async () => {
|
2021-04-17 15:27:16 +05:00
|
|
|
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
|
|
|
|
|
) {
|
2021-04-17 15:27:16 +05:00
|
|
|
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);
|
|
|
|
|
}
|
2021-04-17 15:27:16 +05:00
|
|
|
eSendEvent('loadingNote', appState.note);
|
|
|
|
|
}
|
2021-06-10 12:59:05 +05:00
|
|
|
}
|
2021-10-05 12:53:08 +05:00
|
|
|
};
|
2021-04-17 15:27:16 +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()) {
|
2022-01-05 12:59:50 +05:00
|
|
|
if (!useMessageStore.getState().message.visible) {
|
|
|
|
|
setRateAppMessage();
|
|
|
|
|
}
|
2021-10-05 12:53:08 +05:00
|
|
|
return true;
|
|
|
|
|
}
|
2021-07-24 11:03:50 +05:00
|
|
|
}
|
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');
|
2022-01-22 12:57:05 +05:00
|
|
|
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',
|
|
|
|
|
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 () => {
|
2021-08-04 09:18:04 +05:00
|
|
|
if (!(await BiometricService.isBiometryAvailable())) {
|
|
|
|
|
ToastEvent.show({
|
|
|
|
|
heading: 'Biometrics unavailable',
|
|
|
|
|
message: 'Try unlocking the app with your account password'
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-01-22 12:57:05 +05:00
|
|
|
let verified = await BiometricService.validateUser('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
|
|
|
|
2022-01-25 10:25:23 +05:00
|
|
|
return verifyUser ? (
|
2021-04-19 12:13:42 +05:00
|
|
|
<Animated.View
|
|
|
|
|
style={{
|
2022-01-22 12:57:05 +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,
|
2021-07-24 11:03:50 +05:00
|
|
|
borderRadius: 10
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2021-02-25 12:16:38 +05:00
|
|
|
<Animated.View
|
|
|
|
|
style={{
|
2021-08-10 12:09:19 +05:00
|
|
|
backgroundColor:
|
2022-01-22 12:57:05 +05:00
|
|
|
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',
|
2022-01-25 10:25:23 +05:00
|
|
|
borderRadius: 10
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2022-01-25 10:25:23 +05:00
|
|
|
<SafeAreaView
|
|
|
|
|
style={{
|
|
|
|
|
flex: 1,
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
width: deviceMode !== 'mobile' ? '50%' : Platform.OS == 'ios' ? '95%' : '100%',
|
|
|
|
|
paddingHorizontal: 12
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Heading
|
2021-06-11 09:16:04 +05:00
|
|
|
style={{
|
2022-01-25 10:25:23 +05:00
|
|
|
alignSelf: 'center'
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2022-01-25 10:25:23 +05:00
|
|
|
Verify your identity
|
|
|
|
|
</Heading>
|
2021-11-18 14:52:07 +05:00
|
|
|
|
2022-01-25 10:25:23 +05:00
|
|
|
{user ? (
|
|
|
|
|
<>
|
|
|
|
|
<Paragraph
|
|
|
|
|
style={{
|
|
|
|
|
alignSelf: 'center'
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
To keep your notes secure, please enter password of the account you are logged in
|
|
|
|
|
to.
|
|
|
|
|
</Paragraph>
|
|
|
|
|
<Seperator />
|
|
|
|
|
<Input
|
|
|
|
|
fwdRef={pwdInput}
|
|
|
|
|
secureTextEntry
|
|
|
|
|
placeholder="Enter account password"
|
|
|
|
|
onChangeText={v => (passwordValue = v)}
|
|
|
|
|
onSubmit={onSubmit}
|
|
|
|
|
/>
|
|
|
|
|
<Seperator half />
|
|
|
|
|
<Button
|
|
|
|
|
title="Unlock"
|
|
|
|
|
type="accent"
|
|
|
|
|
onPress={onSubmit}
|
|
|
|
|
width="100%"
|
|
|
|
|
height={50}
|
|
|
|
|
fontSize={SIZE.md}
|
|
|
|
|
/>
|
|
|
|
|
<Seperator />
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
<Paragraph
|
|
|
|
|
style={{
|
|
|
|
|
alignSelf: 'center'
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
To keep your notes secure, please unlock app the with biometrics.
|
|
|
|
|
</Paragraph>
|
|
|
|
|
<Seperator />
|
|
|
|
|
</>
|
|
|
|
|
)}
|
2021-06-11 09:16:04 +05:00
|
|
|
|
2022-01-25 10:25:23 +05:00
|
|
|
<Button
|
|
|
|
|
title="Unlock with Biometrics"
|
|
|
|
|
width="100%"
|
|
|
|
|
height={50}
|
|
|
|
|
onPress={onUnlockBiometrics}
|
|
|
|
|
icon={'fingerprint'}
|
|
|
|
|
type={!user ? 'accent' : 'transparent'}
|
|
|
|
|
fontSize={SIZE.md}
|
|
|
|
|
/>
|
|
|
|
|
</SafeAreaView>
|
2021-02-25 12:16:38 +05:00
|
|
|
</Animated.View>
|
2021-04-19 12:13:42 +05:00
|
|
|
</Animated.View>
|
2022-01-25 10:25:23 +05:00
|
|
|
) : requireIntro.value && !_loading ? (
|
2021-04-19 12:13:42 +05:00
|
|
|
<SplashScreen />
|
2022-01-25 10:25:23 +05:00
|
|
|
) : null;
|
2021-02-25 12:16:38 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default AppLoader;
|