Files
notesnook/apps/mobile/App.js

382 lines
10 KiB
JavaScript
Raw Normal View History

2020-11-16 12:36:41 +05:00
import * as NetInfo from '@react-native-community/netinfo';
2020-12-20 23:01:35 +05:00
import {EV} from 'notes-core/common';
2020-12-16 14:57:58 +05:00
import React, {useEffect, useState} from 'react';
2020-11-24 16:59:12 +05:00
import {
Appearance,
AppState,
Linking,
NativeModules,
Platform,
2020-12-16 14:57:58 +05:00
StatusBar,
2020-11-24 16:59:12 +05:00
} from 'react-native';
2020-12-09 22:16:22 +05:00
import * as RNIap from 'react-native-iap';
2020-12-16 14:57:58 +05:00
import {enabled} from 'react-native-privacy-snapshot';
import {SafeAreaProvider} from 'react-native-safe-area-context';
2020-12-10 09:54:48 +05:00
import SplashScreen from 'react-native-splash-screen';
2020-12-16 14:57:58 +05:00
import {useTracked} from './src/provider';
import {Actions} from './src/provider/Actions';
2020-12-20 23:01:35 +05:00
import Backup from './src/services/Backup';
2020-12-16 14:57:58 +05:00
import {DDS} from './src/services/DeviceDetection';
2020-10-24 13:47:31 +05:00
import {
2020-11-24 16:59:12 +05:00
eSendEvent,
2020-10-24 13:47:31 +05:00
eSubscribeEvent,
2020-12-16 14:57:58 +05:00
eUnSubscribeEvent,
2020-10-24 13:47:31 +05:00
} from './src/services/EventManager';
2020-12-10 09:54:48 +05:00
import IntentService from './src/services/IntentService';
2020-12-16 14:57:58 +05:00
import {
clearMessage,
setEmailVerifyMessage,
setLoginMessage,
} from './src/services/Message';
2020-12-10 09:54:48 +05:00
import Navigation from './src/services/Navigation';
import PremiumService from './src/services/PremiumService';
2020-12-20 23:03:02 +05:00
import SettingsService from './src/services/SettingsService';
2020-12-20 23:01:35 +05:00
import Sync from './src/services/Sync';
2020-12-16 14:57:58 +05:00
import {editing} from './src/utils';
import {COLOR_SCHEME} from './src/utils/Colors';
import {db} from './src/utils/DB';
2020-12-06 09:53:17 +05:00
import {
2020-12-09 22:16:22 +05:00
eClosePremiumDialog,
2020-12-06 09:53:17 +05:00
eDispatchAction,
eOnLoadNote,
2020-12-16 20:02:24 +05:00
eOpenLoginDialog,
2020-12-09 22:16:22 +05:00
eOpenPendingDialog,
2020-12-16 20:02:24 +05:00
eOpenProgressDialog,
2020-12-14 14:03:35 +05:00
eOpenSideMenu,
2020-12-16 14:57:58 +05:00
eStartSyncer,
refreshNotesPage,
2020-12-06 09:53:17 +05:00
} from './src/utils/Events';
2020-12-16 14:57:58 +05:00
import {MMKV} from './src/utils/mmkv';
import {tabBarRef} from './src/utils/Refs';
import {sleep} from './src/utils/TimeUtils';
import {getNote} from './src/views/Editor/Functions';
2020-11-24 16:59:12 +05:00
const {ReceiveSharingIntent} = NativeModules;
2020-11-17 16:18:43 +05:00
2020-11-24 16:59:12 +05:00
let AppRootView = require('./initializer.root').RootView;
2020-11-23 11:30:55 +05:00
let Sentry = null;
2020-12-20 23:01:35 +05:00
let appIsInitialized = false;
let intentOnAppLoadProcessed = false;
2020-12-09 22:16:22 +05:00
let hasPurchased = false;
2020-12-20 23:01:35 +05:00
function updateStatusBarColor() {
StatusBar.setBarStyle(
COLOR_SCHEME.night ? 'light-content' : 'dark-content',
true,
);
if (Platform.OS === 'android') {
StatusBar.setBackgroundColor('transparent', true);
StatusBar.setTranslucent(true, true);
}
}
const onAppStateChanged = async (state) => {
if (state === 'active') {
2020-12-20 23:01:35 +05:00
updateStatusBarColor();
if (SettingsService.get().privacyScreen) {
enabled(false);
}
2020-12-20 23:01:35 +05:00
if (appIsInitialized) {
2020-11-23 15:57:31 +05:00
await MMKV.removeItem('appState');
}
2020-12-06 09:53:17 +05:00
try {
2020-12-20 23:01:35 +05:00
if (intentOnAppLoadProcessed) {
2020-12-06 09:53:17 +05:00
if (Platform.OS === 'android') {
2020-12-20 23:01:35 +05:00
let intent = await ReceiveSharingIntent.getFileNames();
if (intent) {
IntentService.setIntent(intent);
IntentService.check(loadIntent);
2020-12-06 09:53:17 +05:00
}
2020-11-24 16:59:12 +05:00
}
}
2020-12-20 23:01:35 +05:00
} catch (e) {}
} else {
2020-12-20 23:01:35 +05:00
if (editing.currentlyEditing && appIsInitialized) {
2020-11-23 15:57:31 +05:00
let state = JSON.stringify({
editing: editing.currentlyEditing,
note: getNote(),
});
await MMKV.setItem('appState', state);
2020-11-23 11:30:55 +05:00
}
if (SettingsService.get().privacyScreen) {
enabled(true);
}
2020-11-16 12:18:59 +05:00
}
2020-11-16 12:13:02 +05:00
};
2020-12-20 23:01:35 +05:00
function loadIntent(event) {
if (event) {
eSendEvent(eOnLoadNote, event);
tabBarRef.current?.goToPage(1);
Navigation.closeDrawer();
} else {
eSendEvent('nointent');
SplashScreen.hide();
sleep(300).then(() => eSendEvent(eOpenSideMenu));
}
}
2020-11-16 12:36:41 +05:00
const onNetworkStateChanged = (netInfo) => {
2020-12-14 14:03:35 +05:00
/* let message = 'Internet connection restored';
2020-11-16 12:36:41 +05:00
let type = 'success';
if (!netInfo.isConnected || !netInfo.isInternetReachable) {
message = 'No internet connection';
type = 'error';
}
db.user?.get().then((user) => {
2020-12-20 23:01:35 +05:00
if (user && intentOnAppLoadProcessed) {
2020-11-16 12:36:41 +05:00
ToastEvent.show(message, type);
}
2020-12-12 10:04:34 +05:00
}); */
2020-11-16 12:36:41 +05:00
};
2019-11-15 01:17:59 +05:00
const App = () => {
2020-10-24 13:47:31 +05:00
const [, dispatch] = useTracked(),
2020-11-24 16:59:12 +05:00
[init, setInit] = useState(true),
2020-11-23 11:30:55 +05:00
[intent, setIntent] = useState(false);
2020-11-16 12:36:41 +05:00
2020-12-20 23:01:35 +05:00
let subsriptionSuccessListerner;
let subsriptionErrorListener;
useEffect(() => {
eSubscribeEvent(eDispatchAction, (type) => {
dispatch(type);
});
attachIAPListeners();
AppState.addEventListener('change', onAppStateChanged);
eSubscribeEvent('nointent', loadMainApp);
Appearance.addChangeListener(SettingsService.setTheme);
let unsub = NetInfo.addEventListener(onNetworkStateChanged);
Linking.addEventListener('url', onUrlRecieved);
EV.subscribe('db:refresh', onSyncComplete);
EV.subscribe('db:sync', partialSync);
EV.subscribe('user:loggedOut', onLogout);
EV.subscribe('user:checkStatus', PremiumService.onUserStatusCheck);
return () => {
EV.subscribe('db:refresh', onSyncComplete);
EV.unsubscribe('user:loggedOut', onLogout);
EV.unsubscribe('db:sync', partialSync);
EV.unsubscribe('user:checkStatus', PremiumService.onUserStatusCheck);
eUnSubscribeEvent(eStartSyncer, startSyncer);
eUnSubscribeEvent(eDispatchAction, (type) => {
dispatch(type);
});
eUnSubscribeEvent('nointent', loadMainApp);
AppState.removeEventListener('change', onAppStateChanged);
Appearance.removeChangeListener(SettingsService.setTheme);
Linking.removeEventListener('url', onUrlRecieved);
unsub();
unsubIAP();
};
}, []);
useEffect(() => {
SettingsService.init();
dispatch({
type: Actions.DEVICE_MODE,
state: DDS.isLargeTablet()
? 'tablet'
: DDS.isSmallTab
? 'smallTablet'
: 'mobile',
});
db.init().catch(console.log).finally(runAfterInit);
}, []);
2020-10-12 11:24:56 +05:00
2020-12-20 23:01:35 +05:00
const onSyncComplete = async () => {
dispatch({type: Actions.ALL});
2020-11-16 12:36:41 +05:00
};
2020-12-20 23:01:35 +05:00
const onUrlRecieved = async (res) => {
if (intentOnAppLoadProcessed) {
2020-12-06 09:53:17 +05:00
let url = res ? res.url : '';
2020-12-07 12:14:00 +05:00
2020-12-06 09:53:17 +05:00
try {
2020-12-16 14:57:58 +05:00
if (Platform.OS === 'ios' && url.startsWith('ShareMedia://dataUrl')) {
2020-12-20 23:01:35 +05:00
let intent = await ReceiveSharingIntent.getFileNames(url);
intent = IntentService.iosSortedData(intent);
2020-12-16 14:57:58 +05:00
2020-12-20 23:01:35 +05:00
if (intent) {
IntentService.setIntent(intent);
IntentService.check(loadIntent);
2020-12-16 14:57:58 +05:00
}
} else if (url.startsWith('https://notesnook.com/verify')) {
let user = await db.user.fetchUser();
dispatch({type: Actions.USER, user: user});
if (user.isEmailConfirmed) {
clearMessage(dispatch);
}
2020-12-06 09:53:17 +05:00
}
2020-12-20 23:01:35 +05:00
} catch (e) {}
2020-12-06 09:53:17 +05:00
}
};
2020-12-09 22:16:22 +05:00
const attachIAPListeners = () => {
if (Platform.OS === 'ios') {
RNIap.getReceiptIOS()
.then((r) => {
hasPurchased = true;
processReceipt(r);
})
.catch(console.log)
.finally(() => {
subsriptionSuccessListerner = RNIap.purchaseUpdatedListener(
onSuccessfulSubscription,
);
subsriptionErrorListener = RNIap.purchaseErrorListener(
onSubscriptionError,
);
});
} else {
subsriptionSuccessListerner = RNIap.purchaseUpdatedListener(
onSuccessfulSubscription,
);
subsriptionErrorListener = RNIap.purchaseErrorListener(
onSubscriptionError,
);
}
};
2020-12-20 23:01:35 +05:00
const partialSync = async () => {
2020-12-10 15:26:02 +05:00
try {
dispatch({type: Actions.SYNCING, syncing: true});
await db.sync(false);
2020-12-16 14:57:58 +05:00
dispatch({type: Actions.LAST_SYNC, lastSync: await db.lastSynced()});
2020-12-10 15:26:02 +05:00
} catch (e) {
} finally {
dispatch({type: Actions.SYNCING, syncing: false});
}
};
2020-12-16 20:02:24 +05:00
const onLogout = (reason) => {
eSendEvent(eOpenProgressDialog, {
title: 'User Logged Out',
paragraph: reason,
action: () => {
eSendEvent(eOpenLoginDialog);
},
actionText: 'Login Again',
noProgress: true,
});
};
2020-12-09 22:16:22 +05:00
unsubIAP = () => {
if (subsriptionSuccessListerner) {
subsriptionSuccessListerner?.remove();
subsriptionSuccessListerner = null;
}
if (subsriptionErrorListener) {
subsriptionErrorListener?.remove();
subsriptionErrorListener = null;
}
};
2020-12-09 13:09:55 +05:00
2020-12-09 22:16:22 +05:00
const loadMainApp = () => {
2020-11-24 16:59:12 +05:00
dispatch({type: Actions.ALL});
2020-11-23 11:30:55 +05:00
AppRootView = require('./initializer.root').RootView;
2020-12-20 23:01:35 +05:00
setCurrentUser().then(console.log).catch(console.log);
Backup.checkAndRun().then((r) => r);
sleep(500).then(() => (appIsInitialized = true));
2020-11-24 16:59:12 +05:00
db.notes.init().then(() => {
dispatch({type: Actions.NOTES});
dispatch({type: Actions.FAVORITES});
2020-12-16 20:02:24 +05:00
eSendEvent(refreshNotesPage);
2020-11-24 16:59:12 +05:00
dispatch({type: Actions.LOADING, loading: false});
SettingsService.setAppLoaded();
2020-11-24 16:59:12 +05:00
});
2020-12-09 22:16:22 +05:00
SplashScreen.hide();
2020-12-14 14:03:35 +05:00
sleep(300).then(() => eSendEvent(eOpenSideMenu));
//Sentry = require('@sentry/react-native');
2020-11-24 16:59:12 +05:00
// Sentry.init({
// dsn:
// 'https://317a5c31caf64d1e9b27abf15eb1a554@o477952.ingest.sentry.io/5519681',
// });
2020-11-23 11:30:55 +05:00
};
2020-12-20 23:01:35 +05:00
const setCurrentUser = async () => {
2020-11-23 11:30:55 +05:00
try {
2020-12-16 14:57:58 +05:00
let user = await db.user.fetchUser();
2020-11-23 11:30:55 +05:00
if (user) {
2020-12-16 14:57:58 +05:00
clearMessage(dispatch);
if (!user.isEmailConfirmed) {
setEmailVerifyMessage(dispatch);
}
2020-12-07 14:34:14 +05:00
dispatch({type: Actions.USER, user: user});
2020-12-20 23:01:35 +05:00
await Sync.run();
2020-11-23 11:30:55 +05:00
await startSyncer();
} else {
setLoginMessage(dispatch);
}
2020-12-20 23:01:35 +05:00
} catch (e) {}
2020-11-15 12:16:22 +05:00
};
2020-11-23 11:30:55 +05:00
const runAfterInit = () => {
2020-12-07 16:04:22 +05:00
let isIntent = false;
2020-11-24 16:59:12 +05:00
IntentService.getIntent()
.then(() => {
AppRootView = require('./initializer.intent').IntentView;
setInit(false);
2020-12-20 23:01:35 +05:00
intentOnAppLoadProcessed = true;
2020-11-24 16:59:12 +05:00
dispatch({type: Actions.ALL});
setIntent(true);
2020-12-07 16:04:22 +05:00
isIntent = true;
2020-11-24 16:59:12 +05:00
ReceiveSharingIntent.clearFileNames();
})
2020-12-07 16:04:22 +05:00
.catch((e) => console.log)
.finally(() => {
if (!isIntent) {
ReceiveSharingIntent.clearFileNames();
2020-12-20 23:01:35 +05:00
intentOnAppLoadProcessed = true;
2020-12-07 16:04:22 +05:00
loadMainApp();
}
2020-11-24 16:59:12 +05:00
});
2020-11-23 11:30:55 +05:00
};
2020-12-09 22:16:22 +05:00
const onSuccessfulSubscription = (subscription) => {
if (hasPurchased) {
return;
}
const receipt = subscription.transactionReceipt;
processReceipt(receipt);
setTimeout(() => {
eSendEvent(eClosePremiumDialog);
eSendEvent(eOpenPendingDialog);
}, 500);
};
2020-12-20 23:01:35 +05:00
const onSubscriptionError = (error) => {};
2020-12-09 22:16:22 +05:00
2020-12-10 13:06:59 +05:00
const processReceipt = (receipt) => {
2020-12-10 15:26:02 +05:00
return;
2020-12-10 13:06:59 +05:00
if (receipt) {
if (Platform.OS === 'ios') {
fetch('http://192.168.10.5:8100/webhooks/assn', {
method: 'POST',
body: JSON.stringify({
2020-12-10 15:26:02 +05:00
receipt_data: receipt,
2020-12-10 13:06:59 +05:00
}),
headers: {
'Content-Type': 'application/json',
},
2020-12-09 22:16:22 +05:00
})
2020-12-10 13:06:59 +05:00
.then((r) => {
console.log(r.status, 'STATUS');
})
.catch((e) => {
console.log(e, 'ERROR');
});
}
2020-12-09 22:16:22 +05:00
}
2020-12-10 13:06:59 +05:00
};
2020-12-09 22:16:22 +05:00
2020-10-24 13:47:31 +05:00
return (
2020-11-23 11:30:55 +05:00
<SafeAreaProvider>
{intent ? <AppRootView /> : null}
2020-12-09 22:16:22 +05:00
{init && !intent ? <AppRootView /> : null}
2020-11-23 11:30:55 +05:00
</SafeAreaProvider>
2020-10-24 13:47:31 +05:00
);
2019-11-15 01:17:59 +05:00
};
export default App;