2021-10-02 10:09:12 +05:00
|
|
|
import NetInfo from '@react-native-community/netinfo';
|
2022-01-22 09:32:12 +05:00
|
|
|
import { EV, EVENTS } from 'notes-core/common';
|
2022-02-28 13:48:59 +05:00
|
|
|
import { useEffect, useRef } from 'react';
|
2021-10-18 15:02:03 +05:00
|
|
|
import {
|
|
|
|
|
Appearance,
|
|
|
|
|
AppState,
|
|
|
|
|
Linking,
|
|
|
|
|
NativeEventEmitter,
|
|
|
|
|
NativeModules,
|
2022-02-28 13:48:59 +05:00
|
|
|
Platform
|
2021-10-18 15:02:03 +05:00
|
|
|
} from 'react-native';
|
2021-10-02 10:09:12 +05:00
|
|
|
import * as RNIap from 'react-native-iap';
|
2022-01-22 09:32:12 +05:00
|
|
|
import { enabled } from 'react-native-privacy-snapshot';
|
2022-02-28 13:48:59 +05:00
|
|
|
import { doInBackground, editing } from '..';
|
|
|
|
|
import { Walkthrough } from '../../components/walkthroughs';
|
2021-10-02 10:09:12 +05:00
|
|
|
import {
|
2022-03-03 18:03:03 +05:00
|
|
|
EditorWebView,
|
|
|
|
|
getNote,
|
|
|
|
|
getWebviewInit,
|
|
|
|
|
updateNoteInEditor
|
|
|
|
|
} from '../../screens/editor/Functions';
|
|
|
|
|
import tiny from '../../screens/editor/tiny/tiny';
|
2022-02-28 23:25:18 +05:00
|
|
|
import Backup from '../../services/backup';
|
|
|
|
|
import BiometricService from '../../services/biometrics';
|
2021-10-02 10:09:12 +05:00
|
|
|
import {
|
|
|
|
|
eSendEvent,
|
|
|
|
|
eSubscribeEvent,
|
|
|
|
|
eUnSubscribeEvent,
|
2021-11-09 09:44:48 +05:00
|
|
|
presentSheet,
|
2021-10-02 10:09:12 +05:00
|
|
|
ToastEvent
|
2022-02-28 23:25:18 +05:00
|
|
|
} from '../../services/event-manager';
|
2021-10-02 10:09:12 +05:00
|
|
|
import {
|
|
|
|
|
clearMessage,
|
|
|
|
|
setEmailVerifyMessage,
|
2022-01-02 02:02:18 +05:00
|
|
|
setLoginMessage,
|
|
|
|
|
setRecoveryKeyMessage
|
2022-02-28 23:25:18 +05:00
|
|
|
} from '../../services/message';
|
|
|
|
|
import PremiumService from '../../services/premium';
|
|
|
|
|
import SettingsService from '../../services/settings';
|
|
|
|
|
import Sync from '../../services/sync';
|
2021-10-02 10:09:12 +05:00
|
|
|
import {
|
2022-03-03 18:03:03 +05:00
|
|
|
clearAllStores,
|
|
|
|
|
initialize,
|
|
|
|
|
useAttachmentStore,
|
|
|
|
|
useNoteStore,
|
|
|
|
|
useSettingStore,
|
|
|
|
|
useUserStore
|
|
|
|
|
} from '../../stores/stores';
|
2022-02-28 13:48:59 +05:00
|
|
|
import { updateStatusBarColor } from '../color-scheme';
|
|
|
|
|
import { db } from '../database';
|
|
|
|
|
import { MMKV } from '../database/mmkv';
|
2022-03-03 18:03:03 +05:00
|
|
|
import { eClearEditor, eCloseProgressDialog, eOpenLoginDialog, refreshNotesPage } from '../events';
|
2022-02-28 13:48:59 +05:00
|
|
|
import { sleep } from '../time';
|
2021-10-02 10:09:12 +05:00
|
|
|
|
2021-10-18 15:02:03 +05:00
|
|
|
const SodiumEventEmitter = new NativeEventEmitter(NativeModules.Sodium);
|
|
|
|
|
|
2021-10-02 10:09:12 +05:00
|
|
|
export const useAppEvents = () => {
|
|
|
|
|
const loading = useNoteStore(state => state.loading);
|
|
|
|
|
const setLastSynced = useUserStore(state => state.setLastSynced);
|
|
|
|
|
const setUser = useUserStore(state => state.setUser);
|
|
|
|
|
const setSyncing = useUserStore(state => state.setSyncing);
|
2021-12-09 00:14:15 +05:00
|
|
|
const syncedOnLaunch = useRef(false);
|
2021-10-02 10:09:12 +05:00
|
|
|
const refValues = useRef({
|
|
|
|
|
subsriptionSuccessListener: null,
|
|
|
|
|
subsriptionErrorListener: null,
|
|
|
|
|
isUserReady: false,
|
|
|
|
|
prevState: null,
|
|
|
|
|
showingDialog: false,
|
2021-12-09 00:14:15 +05:00
|
|
|
removeInternetStateListener: null,
|
|
|
|
|
isReconnecting: false
|
2021-10-02 10:09:12 +05:00
|
|
|
});
|
|
|
|
|
|
2022-01-22 12:57:05 +05:00
|
|
|
const onMediaDownloaded = ({ hash, groupId, src }) => {
|
2021-10-30 14:35:38 +05:00
|
|
|
if (groupId?.startsWith('monograph')) return;
|
2021-10-02 10:09:12 +05:00
|
|
|
tiny.call(
|
|
|
|
|
EditorWebView,
|
|
|
|
|
`
|
|
|
|
|
(function(){
|
2022-01-22 12:57:05 +05:00
|
|
|
let image = ${JSON.stringify({ hash, src })};
|
2021-10-30 14:35:38 +05:00
|
|
|
tinymce.activeEditor._replaceImage(image);
|
2021-10-02 10:09:12 +05:00
|
|
|
})();
|
|
|
|
|
`
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-03 18:03:03 +05:00
|
|
|
const onLoadingAttachmentProgress = data => {
|
2022-01-22 12:57:05 +05:00
|
|
|
useAttachmentStore.getState().setLoading(data.total === data.current ? null : data);
|
2021-10-18 15:02:03 +05:00
|
|
|
};
|
|
|
|
|
|
2022-03-03 18:03:03 +05:00
|
|
|
const onFileEncryptionProgress = ({ total, progress }) => {
|
2021-10-18 15:02:03 +05:00
|
|
|
console.log('encryption progress: ', (progress / total).toFixed(2));
|
2022-01-22 12:57:05 +05:00
|
|
|
useAttachmentStore.getState().setEncryptionProgress((progress / total).toFixed(2));
|
2021-10-02 10:09:12 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-03-03 18:03:03 +05:00
|
|
|
let subs = [
|
|
|
|
|
Appearance.addChangeListener(SettingsService.setTheme),
|
|
|
|
|
Linking.addEventListener('url', onUrlRecieved),
|
|
|
|
|
SodiumEventEmitter.addListener('onSodiumProgress', onFileEncryptionProgress)
|
|
|
|
|
];
|
|
|
|
|
|
2021-10-02 10:09:12 +05:00
|
|
|
EV.subscribe(EVENTS.appRefreshRequested, onSyncComplete);
|
2022-03-03 18:03:03 +05:00
|
|
|
EV.subscribe(EVENTS.databaseSyncRequested, onRequestPartialSync);
|
2021-10-02 10:09:12 +05:00
|
|
|
EV.subscribe(EVENTS.userLoggedOut, onLogout);
|
|
|
|
|
EV.subscribe(EVENTS.userEmailConfirmed, onEmailVerified);
|
|
|
|
|
EV.subscribe(EVENTS.userSessionExpired, onSessionExpired);
|
|
|
|
|
EV.subscribe(EVENTS.userCheckStatus, PremiumService.onUserStatusCheck);
|
|
|
|
|
EV.subscribe(EVENTS.userSubscriptionUpdated, onAccountStatusChange);
|
|
|
|
|
EV.subscribe(EVENTS.mediaAttachmentDownloaded, onMediaDownloaded);
|
2022-03-03 18:03:03 +05:00
|
|
|
EV.subscribe(EVENTS.attachmentsLoading, onLoadingAttachmentProgress);
|
2021-10-18 15:02:03 +05:00
|
|
|
|
2021-10-02 10:09:12 +05:00
|
|
|
eSubscribeEvent('userLoggedIn', setCurrentUser);
|
2021-12-09 00:14:15 +05:00
|
|
|
|
2021-10-02 10:09:12 +05:00
|
|
|
return () => {
|
|
|
|
|
eUnSubscribeEvent('userLoggedIn', setCurrentUser);
|
|
|
|
|
EV.unsubscribe(EVENTS.userSessionExpired, onSessionExpired);
|
|
|
|
|
EV.unsubscribe(EVENTS.appRefreshRequested, onSyncComplete);
|
2022-03-03 18:03:03 +05:00
|
|
|
EV.unsubscribe(EVENTS.databaseSyncRequested, onRequestPartialSync);
|
2021-10-02 10:09:12 +05:00
|
|
|
EV.unsubscribe(EVENTS.userLoggedOut, onLogout);
|
|
|
|
|
EV.unsubscribe(EVENTS.userEmailConfirmed, onEmailVerified);
|
|
|
|
|
EV.unsubscribe(EVENTS.mediaAttachmentDownloaded, onMediaDownloaded);
|
2022-03-03 18:03:03 +05:00
|
|
|
EV.subscribe(EVENTS.attachmentsLoading, onLoadingAttachmentProgress);
|
2021-10-02 10:09:12 +05:00
|
|
|
EV.unsubscribe(EVENTS.userCheckStatus, PremiumService.onUserStatusCheck);
|
|
|
|
|
EV.unsubscribe(EVENTS.userSubscriptionUpdated, onAccountStatusChange);
|
2022-03-03 18:03:03 +05:00
|
|
|
EV.unsubscribeAll();
|
2021-10-02 10:09:12 +05:00
|
|
|
|
2022-03-03 18:03:03 +05:00
|
|
|
subs.forEach(sub => sub?.remove());
|
2021-10-02 10:09:12 +05:00
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const onSessionExpired = async () => {
|
2022-03-03 18:03:03 +05:00
|
|
|
await SettingsService.set({
|
|
|
|
|
sessionExpired: true
|
|
|
|
|
});
|
2022-02-07 14:44:48 +05:00
|
|
|
eSendEvent('session_expired');
|
2021-10-02 10:09:12 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-03-03 22:31:23 +05:00
|
|
|
let sub;
|
2021-10-02 10:09:12 +05:00
|
|
|
if (!loading) {
|
2022-03-03 22:31:23 +05:00
|
|
|
sub = AppState.addEventListener('change', onAppStateChanged);
|
2021-10-02 10:09:12 +05:00
|
|
|
(async () => {
|
|
|
|
|
try {
|
|
|
|
|
let url = await Linking.getInitialURL();
|
|
|
|
|
if (url?.startsWith('https://app.notesnook.com/account/verified')) {
|
|
|
|
|
await onEmailVerified();
|
|
|
|
|
}
|
|
|
|
|
await setCurrentUser();
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
})();
|
2022-01-22 12:57:05 +05:00
|
|
|
refValues.current.removeInternetStateListener =
|
|
|
|
|
NetInfo.addEventListener(onInternetStateChanged);
|
2021-10-02 10:09:12 +05:00
|
|
|
}
|
|
|
|
|
return () => {
|
|
|
|
|
refValues.current?.removeInternetStateListener &&
|
|
|
|
|
refValues.current?.removeInternetStateListener();
|
2022-03-03 22:31:23 +05:00
|
|
|
sub?.remove();
|
2021-10-02 10:09:12 +05:00
|
|
|
unsubIAP();
|
|
|
|
|
};
|
|
|
|
|
}, [loading]);
|
|
|
|
|
|
|
|
|
|
const onInternetStateChanged = async state => {
|
2021-12-09 00:14:15 +05:00
|
|
|
if (!syncedOnLaunch.current) return;
|
2021-10-02 10:09:12 +05:00
|
|
|
reconnectSSE(state);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onSyncComplete = async () => {
|
2022-03-09 17:05:34 +05:00
|
|
|
console.log('sync complete');
|
2021-10-02 10:09:12 +05:00
|
|
|
initialize();
|
|
|
|
|
setLastSynced(await db.lastSynced());
|
2022-03-14 16:26:36 +05:00
|
|
|
setSyncing(false);
|
2021-10-02 10:09:12 +05:00
|
|
|
if (getNote()) {
|
|
|
|
|
await updateNoteInEditor();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onUrlRecieved = async res => {
|
|
|
|
|
let url = res ? res.url : '';
|
|
|
|
|
try {
|
|
|
|
|
if (url.startsWith('https://app.notesnook.com/account/verified')) {
|
|
|
|
|
await onEmailVerified();
|
|
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onEmailVerified = async () => {
|
|
|
|
|
let user = await db.user.getUser();
|
|
|
|
|
setUser(user);
|
|
|
|
|
if (!user) return;
|
2022-03-03 15:09:30 +05:00
|
|
|
SettingsService.set({
|
|
|
|
|
userEmailConfirmed: true
|
|
|
|
|
});
|
2021-10-02 10:09:12 +05:00
|
|
|
await PremiumService.setPremiumStatus();
|
2022-02-18 14:45:38 +05:00
|
|
|
Walkthrough.present('emailconfirmed', false, true);
|
2021-10-02 10:09:12 +05:00
|
|
|
if (user?.isEmailConfirmed) {
|
|
|
|
|
clearMessage();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const attachIAPListeners = async () => {
|
|
|
|
|
await RNIap.initConnection()
|
|
|
|
|
.catch(e => {
|
|
|
|
|
console.log(e);
|
|
|
|
|
})
|
|
|
|
|
.then(async () => {
|
|
|
|
|
refValues.current.subsriptionSuccessListener =
|
|
|
|
|
RNIap.purchaseUpdatedListener(onSuccessfulSubscription);
|
|
|
|
|
refValues.current.subsriptionErrorListener =
|
|
|
|
|
RNIap.purchaseErrorListener(onSubscriptionError);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onAccountStatusChange = async userStatus => {
|
|
|
|
|
if (!PremiumService.get() && userStatus.type === 5) {
|
|
|
|
|
PremiumService.subscriptions.clear();
|
2022-02-18 14:45:38 +05:00
|
|
|
Walkthrough.present('prouser', false, true);
|
2021-10-02 10:09:12 +05:00
|
|
|
}
|
|
|
|
|
await PremiumService.setPremiumStatus();
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-08 15:17:03 +05:00
|
|
|
const onRequestPartialSync = async (full, force) => {
|
2022-03-09 17:05:34 +05:00
|
|
|
console.log('auto sync request', full, force);
|
2021-10-02 10:09:12 +05:00
|
|
|
try {
|
2022-03-08 15:17:03 +05:00
|
|
|
if (full || force) {
|
|
|
|
|
Sync.run('global', force, full);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-10-02 10:09:12 +05:00
|
|
|
setSyncing(true);
|
|
|
|
|
let res = await doInBackground(async () => {
|
|
|
|
|
try {
|
|
|
|
|
await db.sync(false);
|
|
|
|
|
return true;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return e.message;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (res !== true) throw new Error(res);
|
|
|
|
|
setLastSynced(await db.lastSynced());
|
|
|
|
|
} catch (e) {
|
|
|
|
|
setSyncing(false);
|
|
|
|
|
let status = await NetInfo.fetch();
|
|
|
|
|
if (status.isConnected && status.isInternetReachable) {
|
2022-03-08 15:17:03 +05:00
|
|
|
ToastEvent.error(e, 'Sync failed', 'global');
|
2021-10-02 10:09:12 +05:00
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
setSyncing(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onLogout = async reason => {
|
|
|
|
|
setUser(null);
|
|
|
|
|
clearAllStores();
|
|
|
|
|
|
|
|
|
|
SettingsService.init();
|
|
|
|
|
setSyncing(false);
|
|
|
|
|
setLoginMessage();
|
|
|
|
|
await PremiumService.setPremiumStatus();
|
2022-03-03 15:09:30 +05:00
|
|
|
SettingsService.set({
|
|
|
|
|
introCompleted: true
|
|
|
|
|
});
|
2021-11-09 09:44:48 +05:00
|
|
|
presentSheet({
|
2021-10-02 10:09:12 +05:00
|
|
|
title: reason ? reason : 'User logged out',
|
|
|
|
|
paragraph: `You have been logged out of your account.`,
|
|
|
|
|
action: async () => {
|
|
|
|
|
eSendEvent(eCloseProgressDialog);
|
|
|
|
|
await sleep(300);
|
|
|
|
|
eSendEvent(eOpenLoginDialog);
|
|
|
|
|
},
|
|
|
|
|
icon: 'logout',
|
2022-01-22 09:32:12 +05:00
|
|
|
actionText: 'Login'
|
2021-10-02 10:09:12 +05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
initialize();
|
|
|
|
|
}, 1000);
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-22 12:57:05 +05:00
|
|
|
const unsubIAP = () => {
|
2021-10-02 10:09:12 +05:00
|
|
|
if (refValues.current?.subsriptionSuccessListener) {
|
|
|
|
|
refValues.current.subsriptionSuccessListener?.remove();
|
|
|
|
|
refValues.current.subsriptionSuccessListener = null;
|
|
|
|
|
}
|
|
|
|
|
if (refValues.current?.subsriptionErrorListener) {
|
|
|
|
|
refValues.current.subsriptionErrorListener?.remove();
|
|
|
|
|
refValues.current.subsriptionErrorListener = null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const setCurrentUser = async login => {
|
2022-03-16 10:23:34 +05:00
|
|
|
console.log(`setCurrentUser: ${login}`);
|
2022-01-25 10:43:17 +05:00
|
|
|
let user;
|
2021-10-02 10:09:12 +05:00
|
|
|
try {
|
2022-01-25 10:43:17 +05:00
|
|
|
user = await db.user.getUser();
|
2022-03-09 19:47:17 +05:00
|
|
|
await PremiumService.setPremiumStatus();
|
2022-03-16 10:23:34 +05:00
|
|
|
if (login) {
|
|
|
|
|
await Sync.run();
|
|
|
|
|
eSendEvent(eCloseProgressDialog);
|
|
|
|
|
}
|
2022-03-14 16:26:36 +05:00
|
|
|
setLastSynced(await db.lastSynced());
|
2022-03-03 18:03:03 +05:00
|
|
|
if (!user) {
|
|
|
|
|
return setLoginMessage();
|
|
|
|
|
}
|
2021-11-08 11:57:24 +05:00
|
|
|
|
2022-03-03 18:03:03 +05:00
|
|
|
let userEmailConfirmed = SettingsService.get().userEmailConfirmed;
|
|
|
|
|
setUser(user);
|
|
|
|
|
if (SettingsService.get().sessionExpired) {
|
2021-12-09 00:14:15 +05:00
|
|
|
syncedOnLaunch.current = true;
|
2021-10-02 10:09:12 +05:00
|
|
|
return;
|
|
|
|
|
}
|
2021-12-14 23:46:50 +05:00
|
|
|
|
2022-03-03 18:03:03 +05:00
|
|
|
clearMessage();
|
|
|
|
|
attachIAPListeners();
|
2022-01-02 02:02:18 +05:00
|
|
|
|
2022-03-16 10:23:34 +05:00
|
|
|
if (!login) {
|
|
|
|
|
user = await db.user.fetchUser();
|
|
|
|
|
setUser(user);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-09 19:47:17 +05:00
|
|
|
await PremiumService.setPremiumStatus();
|
2022-03-03 18:03:03 +05:00
|
|
|
if (user.isEmailConfirmed && !userEmailConfirmed) {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
onEmailVerified();
|
|
|
|
|
}, 1000);
|
2022-03-03 15:09:30 +05:00
|
|
|
SettingsService.set({
|
2022-03-03 18:03:03 +05:00
|
|
|
userEmailConfirmed: true
|
2022-03-03 15:09:30 +05:00
|
|
|
});
|
2022-01-02 02:02:18 +05:00
|
|
|
}
|
2022-03-03 18:03:03 +05:00
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
|
|
user = await db.user.getUser();
|
|
|
|
|
if (user?.isEmailConfirmed && !SettingsService.get().recoveryKeySaved) {
|
|
|
|
|
setRecoveryKeyMessage();
|
|
|
|
|
}
|
|
|
|
|
if (!user.isEmailConfirmed) setEmailVerifyMessage();
|
|
|
|
|
|
|
|
|
|
if (PremiumService.get() && user) {
|
|
|
|
|
if (SettingsService.get().reminder === 'off') {
|
|
|
|
|
await SettingsService.set({ reminder: 'daily' });
|
2021-12-08 22:51:10 +05:00
|
|
|
}
|
2022-03-03 18:03:03 +05:00
|
|
|
if (Backup.checkBackupRequired()) {
|
|
|
|
|
sleep(2000).then(() => Backup.checkAndRun());
|
2021-10-02 10:09:12 +05:00
|
|
|
}
|
|
|
|
|
}
|
2022-03-03 18:03:03 +05:00
|
|
|
refValues.current.isUserReady = true;
|
2022-03-16 10:23:34 +05:00
|
|
|
|
2022-03-03 18:03:03 +05:00
|
|
|
syncedOnLaunch.current = true;
|
2021-10-02 10:09:12 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onSuccessfulSubscription = async subscription => {
|
|
|
|
|
await PremiumService.subscriptions.set(subscription);
|
|
|
|
|
await PremiumService.subscriptions.verify(subscription);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onSubscriptionError = async error => {
|
|
|
|
|
ToastEvent.show({
|
|
|
|
|
heading: 'Failed to subscribe',
|
|
|
|
|
type: 'error',
|
|
|
|
|
message: error.message,
|
|
|
|
|
context: 'local'
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onAppStateChanged = async state => {
|
2021-12-14 23:46:50 +05:00
|
|
|
console.log('onAppStateChanged');
|
2021-10-02 10:09:12 +05:00
|
|
|
if (state === 'active') {
|
|
|
|
|
updateStatusBarColor();
|
|
|
|
|
if (
|
|
|
|
|
SettingsService.get().appLockMode !== 'background' &&
|
|
|
|
|
!SettingsService.get().privacyScreen
|
|
|
|
|
) {
|
|
|
|
|
enabled(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (SettingsService.get().appLockMode === 'background') {
|
2022-02-19 11:12:35 +05:00
|
|
|
if (useSettingStore.getState().requestBiometrics) {
|
|
|
|
|
console.log('requesting biometrics');
|
|
|
|
|
useSettingStore.getState().setRequestBiometrics(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-01-22 12:57:05 +05:00
|
|
|
if (refValues.current?.prevState === 'background' && !refValues.current?.showingDialog) {
|
2021-10-02 10:09:12 +05:00
|
|
|
refValues.current.showingDialog = true;
|
|
|
|
|
refValues.current.prevState = 'active';
|
2022-01-25 10:25:23 +05:00
|
|
|
useUserStore.getState().setVerifyUser(true);
|
|
|
|
|
|
2022-01-22 12:57:05 +05:00
|
|
|
let result = await BiometricService.validateUser('Unlock to access your notes');
|
2021-10-02 10:09:12 +05:00
|
|
|
if (result) {
|
2022-01-25 18:29:13 +05:00
|
|
|
useUserStore.getState().setVerifyUser(false);
|
2021-10-02 10:09:12 +05:00
|
|
|
refValues.current.showingDialog = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
refValues.current.prevState = 'active';
|
2021-12-14 23:46:50 +05:00
|
|
|
console.log('reconnect sse');
|
2021-10-02 10:09:12 +05:00
|
|
|
await reconnectSSE();
|
2021-12-14 23:46:50 +05:00
|
|
|
|
2021-10-02 10:09:12 +05:00
|
|
|
await checkIntentState();
|
|
|
|
|
if (getWebviewInit()) {
|
|
|
|
|
await MMKV.removeItem('appState');
|
|
|
|
|
}
|
|
|
|
|
let user = await db.user.getUser();
|
|
|
|
|
if (user && !user.isEmailConfirmed) {
|
|
|
|
|
try {
|
|
|
|
|
let user = await db.user.fetchUser();
|
|
|
|
|
if (user?.isEmailConfirmed) {
|
|
|
|
|
onEmailVerified();
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
refValues.current.prevState = 'background';
|
2022-01-22 12:57:05 +05:00
|
|
|
if (getNote()?.locked && SettingsService.get().appLockMode === 'background') {
|
2021-10-02 10:09:12 +05:00
|
|
|
eSendEvent(eClearEditor);
|
|
|
|
|
}
|
|
|
|
|
await storeAppState();
|
|
|
|
|
if (
|
|
|
|
|
SettingsService.get().privacyScreen ||
|
|
|
|
|
SettingsService.get().appLockMode === 'background'
|
|
|
|
|
) {
|
|
|
|
|
enabled(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async function reconnectSSE(connection) {
|
2021-12-09 00:14:15 +05:00
|
|
|
if (refValues.current?.isReconnecting) return;
|
2021-10-02 10:09:12 +05:00
|
|
|
if (!refValues.current?.isUserReady) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-03-03 18:03:03 +05:00
|
|
|
if (SettingsService.get().sessionExpired) {
|
2021-12-09 22:40:50 +05:00
|
|
|
refValues.current.isReconnecting = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-12-09 00:14:15 +05:00
|
|
|
refValues.current.isReconnecting = true;
|
2021-10-02 10:09:12 +05:00
|
|
|
let state = connection;
|
2021-12-14 23:46:50 +05:00
|
|
|
console.log('SSE:', 'TRYING TO RECONNECT');
|
2021-10-02 10:09:12 +05:00
|
|
|
try {
|
|
|
|
|
if (!state) {
|
|
|
|
|
state = await NetInfo.fetch();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let user = await db.user.getUser();
|
|
|
|
|
if (user && state.isConnected && state.isInternetReachable) {
|
2021-12-14 23:46:50 +05:00
|
|
|
await db.connectSSE();
|
2021-10-02 10:09:12 +05:00
|
|
|
}
|
2021-12-09 00:14:15 +05:00
|
|
|
refValues.current.isReconnecting = false;
|
|
|
|
|
} catch (e) {
|
2021-12-14 23:46:50 +05:00
|
|
|
refValues.current.isReconnecting = false;
|
2021-12-09 00:14:15 +05:00
|
|
|
}
|
2021-10-02 10:09:12 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function storeAppState() {
|
|
|
|
|
if (editing.currentlyEditing) {
|
|
|
|
|
if (getNote()?.locked) return;
|
|
|
|
|
let state = JSON.stringify({
|
|
|
|
|
editing: editing.currentlyEditing,
|
|
|
|
|
note: getNote(),
|
|
|
|
|
movedAway: editing.movedAway,
|
|
|
|
|
timestamp: Date.now()
|
|
|
|
|
});
|
|
|
|
|
await MMKV.setItem('appState', state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function checkIntentState() {
|
|
|
|
|
try {
|
2021-12-09 01:33:23 +05:00
|
|
|
let notesAddedFromIntent = await MMKV.getItem('notesAddedFromIntent');
|
|
|
|
|
let shareExtensionOpened = await MMKV.getItem('shareExtensionOpened');
|
|
|
|
|
if (notesAddedFromIntent) {
|
2021-10-02 10:09:12 +05:00
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
|
await db.init();
|
|
|
|
|
await db.notes.init();
|
|
|
|
|
}
|
|
|
|
|
useNoteStore.getState().setNotes();
|
|
|
|
|
eSendEvent(refreshNotesPage);
|
|
|
|
|
MMKV.removeItem('notesAddedFromIntent');
|
|
|
|
|
initialize();
|
|
|
|
|
eSendEvent(refreshNotesPage);
|
|
|
|
|
}
|
2021-12-09 01:33:23 +05:00
|
|
|
if (notesAddedFromIntent || shareExtensionOpened) {
|
2022-01-22 09:32:12 +05:00
|
|
|
eSendEvent('loadingNote', getNote());
|
2022-01-05 18:10:53 +05:00
|
|
|
eSendEvent('webviewreset', true);
|
2021-12-09 01:33:23 +05:00
|
|
|
MMKV.removeItem('shareExtensionOpened');
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(e);
|
|
|
|
|
}
|
2021-10-02 10:09:12 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
};
|