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 { Walkthrough } from '../../components/walkthroughs';
|
2022-06-21 14:32:05 +05:00
|
|
|
import { editorController, editorState } from '../../screens/editor/tiptap/utils';
|
2022-02-28 23:25:18 +05:00
|
|
|
import BiometricService from '../../services/biometrics';
|
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';
|
2022-02-28 13:48:59 +05:00
|
|
|
import { updateStatusBarColor } from '../color-scheme';
|
|
|
|
|
import { db } from '../database';
|
|
|
|
|
import { MMKV } from '../database/mmkv';
|
2022-05-20 12:40:14 +05:00
|
|
|
import { eClearEditor, eCloseProgressDialog, refreshNotesPage } from '../events';
|
2022-04-15 23:17:33 +05:00
|
|
|
import Sync from '../../services/sync';
|
2022-04-24 05:59:14 +05:00
|
|
|
import { clearAllStores, initialize } from '../../stores';
|
|
|
|
|
import { useUserStore } from '../../stores/use-user-store';
|
|
|
|
|
import { useMessageStore } from '../../stores/use-message-store';
|
|
|
|
|
import { useSettingStore } from '../../stores/use-setting-store';
|
|
|
|
|
import { useAttachmentStore } from '../../stores/use-attachment-store';
|
|
|
|
|
import { useNoteStore } from '../../stores/use-notes-store';
|
2022-05-20 12:40:14 +05:00
|
|
|
import {
|
|
|
|
|
eSendEvent,
|
|
|
|
|
eSubscribeEvent,
|
|
|
|
|
eUnSubscribeEvent,
|
|
|
|
|
ToastEvent
|
|
|
|
|
} from '../../services/event-manager';
|
|
|
|
|
import { useEditorStore } from '../../stores/use-editor-store';
|
2022-07-01 16:08:59 +05:00
|
|
|
import { useDragState } from '../../screens/settings/editor/state';
|
2022-07-11 22:18:41 +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-03-03 18:03:03 +05:00
|
|
|
const onLoadingAttachmentProgress = data => {
|
2022-07-09 17:04:53 +05:00
|
|
|
console.log('loading', 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
|
|
|
};
|
2022-04-01 10:04:26 +05:00
|
|
|
const onSyncProgress = ({ type, total, current }) => {
|
|
|
|
|
console.log(type, total, current);
|
|
|
|
|
if (type !== 'download') return;
|
|
|
|
|
if (total < 10 || current % 10 === 0) {
|
|
|
|
|
initialize();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!loading) {
|
|
|
|
|
const eventManager = db?.eventManager;
|
|
|
|
|
eventManager?.subscribe(EVENTS.syncProgress, onSyncProgress);
|
2022-04-24 05:59:14 +05:00
|
|
|
let sub = eventManager?.subscribe(EVENTS.databaseSyncRequested, onRequestPartialSync);
|
2022-04-01 10:04:26 +05:00
|
|
|
eventManager?.subscribe(EVENTS.syncCompleted, onSyncComplete);
|
2022-04-24 05:59:14 +05:00
|
|
|
console.log('SUBSCRIBED', sub);
|
2022-04-01 10:04:26 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
const eventManager = db?.eventManager;
|
2022-04-24 05:59:14 +05:00
|
|
|
console.log('UNSUBSCRIBED');
|
2022-04-01 10:04:26 +05:00
|
|
|
eventManager?.unsubscribe(EVENTS.syncCompleted, onSyncComplete);
|
|
|
|
|
eventManager?.unsubscribe(EVENTS.syncProgress, onSyncProgress);
|
|
|
|
|
eventManager?.unsubscribe(EVENTS.databaseSyncRequested, onRequestPartialSync);
|
|
|
|
|
};
|
|
|
|
|
}, [loading]);
|
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);
|
|
|
|
|
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);
|
2022-03-03 18:03:03 +05:00
|
|
|
EV.subscribe(EVENTS.attachmentsLoading, onLoadingAttachmentProgress);
|
2022-03-16 13:21:05 +05:00
|
|
|
eSubscribeEvent('userLoggedIn', onUserUpdated);
|
2021-12-09 00:14:15 +05:00
|
|
|
|
2021-10-02 10:09:12 +05:00
|
|
|
return () => {
|
2022-03-16 13:21:05 +05:00
|
|
|
eUnSubscribeEvent('userLoggedIn', onUserUpdated);
|
2022-04-01 10:04:26 +05:00
|
|
|
|
2021-10-02 10:09:12 +05:00
|
|
|
EV.unsubscribe(EVENTS.appRefreshRequested, onSyncComplete);
|
2022-04-01 10:04:26 +05:00
|
|
|
EV.unsubscribe(EVENTS.userSessionExpired, onSessionExpired);
|
2021-10-02 10:09:12 +05:00
|
|
|
EV.unsubscribe(EVENTS.userLoggedOut, onLogout);
|
|
|
|
|
EV.unsubscribe(EVENTS.userEmailConfirmed, onEmailVerified);
|
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();
|
|
|
|
|
}
|
2022-03-16 13:21:05 +05:00
|
|
|
await onUserUpdated();
|
2021-10-02 10:09:12 +05:00
|
|
|
} 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);
|
2022-04-01 10:04:26 +05:00
|
|
|
eSendEvent(eCloseProgressDialog, 'sync_progress');
|
2022-03-26 16:05:58 +05:00
|
|
|
let id = useEditorStore.getState().currentEditingNote;
|
|
|
|
|
let note = id && db.notes.note(id).data;
|
|
|
|
|
if (note) {
|
|
|
|
|
//await updateNoteInEditor();
|
2021-10-02 10:09:12 +05:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
2022-04-01 11:24:15 +05:00
|
|
|
if (full || force) {
|
|
|
|
|
await Sync.run('global', force, full);
|
2021-10-02 10:09:12 +05:00
|
|
|
setSyncing(false);
|
2022-04-01 11:24:15 +05:00
|
|
|
} else {
|
|
|
|
|
await Sync.run('global', false, false);
|
2021-10-02 10:09:12 +05:00
|
|
|
setSyncing(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onLogout = async reason => {
|
2022-04-24 05:59:14 +05:00
|
|
|
console.log('LOGOUT', reason);
|
2021-10-02 10:09:12 +05:00
|
|
|
};
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-16 13:21:05 +05:00
|
|
|
const onUserUpdated = async login => {
|
|
|
|
|
console.log(`onUserUpdated: ${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-14 16:26:36 +05:00
|
|
|
setLastSynced(await db.lastSynced());
|
2022-07-01 16:08:59 +05:00
|
|
|
await useDragState.getState().init();
|
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-07-09 09:45:16 +05:00
|
|
|
if (user?.isEmailConfirmed && !userEmailConfirmed) {
|
2022-03-03 18:03:03 +05:00
|
|
|
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-04-12 13:30:35 +05:00
|
|
|
} catch (e) {
|
|
|
|
|
ToastEvent.error(e, 'An error occured', 'global');
|
|
|
|
|
}
|
2022-03-03 18:03:03 +05:00
|
|
|
|
|
|
|
|
user = await db.user.getUser();
|
2022-03-18 14:40:15 +05:00
|
|
|
if (
|
|
|
|
|
user?.isEmailConfirmed &&
|
|
|
|
|
!SettingsService.get().recoveryKeySaved &&
|
|
|
|
|
!useMessageStore.getState().message?.visible
|
|
|
|
|
) {
|
2022-03-03 18:03:03 +05:00
|
|
|
setRecoveryKeyMessage();
|
|
|
|
|
}
|
2022-07-09 09:45:16 +05:00
|
|
|
if (!user?.isEmailConfirmed) setEmailVerifyMessage();
|
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();
|
2022-03-26 16:05:58 +05:00
|
|
|
await MMKV.removeItem('appState');
|
2021-10-02 10:09:12 +05:00
|
|
|
let user = await db.user.getUser();
|
2022-07-09 09:45:16 +05:00
|
|
|
if (user && !user?.isEmailConfirmed) {
|
2021-10-02 10:09:12 +05:00
|
|
|
try {
|
|
|
|
|
let user = await db.user.fetchUser();
|
|
|
|
|
if (user?.isEmailConfirmed) {
|
|
|
|
|
onEmailVerified();
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
refValues.current.prevState = 'background';
|
2022-03-26 16:05:58 +05:00
|
|
|
let id = useEditorStore.getState().currentEditingNote;
|
|
|
|
|
let note = id && db.notes.note(id).data;
|
|
|
|
|
if (note?.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() {
|
2022-03-26 16:05:58 +05:00
|
|
|
if (editorState().currentlyEditing) {
|
|
|
|
|
let id = useEditorStore.getState().currentEditingNote;
|
|
|
|
|
let note = id && db.notes.note(id).data;
|
|
|
|
|
if (note?.locked) return;
|
2021-10-02 10:09:12 +05:00
|
|
|
let state = JSON.stringify({
|
2022-03-26 16:05:58 +05:00
|
|
|
editing: editorState().currentlyEditing,
|
|
|
|
|
note: note,
|
|
|
|
|
movedAway: editorState().movedAway,
|
2021-10-02 10:09:12 +05:00
|
|
|
timestamp: Date.now()
|
|
|
|
|
});
|
2022-04-13 03:00:04 +05:00
|
|
|
MMKV.setString('appState', state);
|
2021-10-02 10:09:12 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function checkIntentState() {
|
2022-07-11 22:18:41 +05:00
|
|
|
console.log(checkIntentState, 'CHECK INTENT STATE');
|
2021-10-02 10:09:12 +05:00
|
|
|
try {
|
2022-04-13 03:00:04 +05:00
|
|
|
let notesAddedFromIntent = MMKV.getString('notesAddedFromIntent');
|
|
|
|
|
let shareExtensionOpened = MMKV.getString('shareExtensionOpened');
|
2021-12-09 01:33:23 +05:00
|
|
|
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);
|
|
|
|
|
}
|
2022-07-11 22:18:41 +05:00
|
|
|
console.log('CHECK INTENT STATE', notesAddedFromIntent || shareExtensionOpened);
|
2021-12-09 01:33:23 +05:00
|
|
|
if (notesAddedFromIntent || shareExtensionOpened) {
|
2022-03-26 16:05:58 +05:00
|
|
|
let id = useEditorStore.getState().currentEditingNote;
|
|
|
|
|
let note = id && db.notes.note(id).data;
|
|
|
|
|
eSendEvent('loadingNote', note);
|
2022-07-11 22:18:41 +05:00
|
|
|
eSendEvent('webview_reset');
|
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;
|
|
|
|
|
};
|