mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-24 07:29:30 +01:00
refactor code
This commit is contained in:
@@ -1,44 +1,42 @@
|
||||
import * as NetInfo from '@react-native-community/netinfo';
|
||||
import { EV } from 'notes-core/common';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import {
|
||||
Appearance, AppState,
|
||||
Platform,
|
||||
StatusBar
|
||||
} from 'react-native';
|
||||
import {EV} from 'notes-core/common';
|
||||
import React, {useEffect, useState} from 'react';
|
||||
import {Appearance, AppState, Platform, StatusBar} from 'react-native';
|
||||
import DeviceInfo from 'react-native-device-info';
|
||||
import Orientation from 'react-native-orientation';
|
||||
import { enabled } from 'react-native-privacy-snapshot';
|
||||
import {enabled} from 'react-native-privacy-snapshot';
|
||||
import ReceiveSharingIntent from 'react-native-receive-sharing-intent';
|
||||
import { SafeAreaProvider } from 'react-native-safe-area-context';
|
||||
import {SafeAreaProvider} from 'react-native-safe-area-context';
|
||||
import SplashScreen from 'react-native-splash-screen';
|
||||
import { useTracked } from './src/provider';
|
||||
import { Actions } from './src/provider/Actions';
|
||||
import { defaultState } from './src/provider/DefaultState';
|
||||
import {useTracked} from './src/provider';
|
||||
import {Actions} from './src/provider/Actions';
|
||||
import {defaultState} from './src/provider/DefaultState';
|
||||
import Backup from './src/services/Backup';
|
||||
import { DDS } from './src/services/DeviceDetection';
|
||||
import {DDS} from './src/services/DeviceDetection';
|
||||
import {
|
||||
eSendEvent,
|
||||
eSubscribeEvent,
|
||||
eUnSubscribeEvent,
|
||||
ToastEvent
|
||||
ToastEvent,
|
||||
} from './src/services/EventManager';
|
||||
import { setLoginMessage } from './src/services/Message';
|
||||
import { AndroidModule, sortSettings } from './src/utils';
|
||||
import { COLOR_SCHEME } from './src/utils/Colors';
|
||||
import { getColorScheme } from './src/utils/ColorUtils';
|
||||
import { db } from './src/utils/DB';
|
||||
import IntentService from './src/services/IntentService';
|
||||
import {setLoginMessage} from './src/services/Message';
|
||||
import SettingsService from './src/services/SettingsService';
|
||||
import {AndroidModule, sortSettings} from './src/utils';
|
||||
import {COLOR_SCHEME} from './src/utils/Colors';
|
||||
import {getColorScheme} from './src/utils/ColorUtils';
|
||||
import {db} from './src/utils/DB';
|
||||
import {
|
||||
eDispatchAction,
|
||||
eOnLoadNote,
|
||||
eResetApp,
|
||||
eStartSyncer
|
||||
eStartSyncer,
|
||||
} from './src/utils/Events';
|
||||
import { MMKV } from './src/utils/mmkv';
|
||||
import { tabBarRef } from './src/utils/Refs';
|
||||
import { getDeviceSize, scale, updateSize } from './src/utils/SizeUtils';
|
||||
import { sleep } from './src/utils/TimeUtils';
|
||||
import { getNote, setIntent } from './src/views/Editor/Functions';
|
||||
import {MMKV} from './src/utils/mmkv';
|
||||
import {tabBarRef} from './src/utils/Refs';
|
||||
import {getDeviceSize, scale, updateSize} from './src/utils/SizeUtils';
|
||||
import {sleep} from './src/utils/TimeUtils';
|
||||
import {getNote, setIntent} from './src/views/Editor/Functions';
|
||||
let firstLoad = true;
|
||||
let note = null;
|
||||
let prevIntent = {
|
||||
@@ -52,21 +50,13 @@ const onAppFocused = async () => {
|
||||
StatusBar.setTranslucent(true);
|
||||
StatusBar.setBackgroundColor(COLOR_SCHEME.bg);
|
||||
}
|
||||
let settings = await MMKV.getStringAsync('settings');
|
||||
if (settings) {
|
||||
settings = JSON.parse(settings);
|
||||
}
|
||||
if (settings.privacyScreen) {
|
||||
if (SettingsService.get().privacyScreen) {
|
||||
enabled(false);
|
||||
}
|
||||
};
|
||||
|
||||
const onAppBlur = async () => {
|
||||
let settings = await MMKV.getStringAsync('settings');
|
||||
if (settings) {
|
||||
settings = JSON.parse(settings);
|
||||
}
|
||||
if (settings.privacyScreen) {
|
||||
if (SettingsService.get().privacyScreen) {
|
||||
enabled(true);
|
||||
}
|
||||
};
|
||||
@@ -110,17 +100,6 @@ const App = () => {
|
||||
resetApp();
|
||||
}, 1000);
|
||||
};
|
||||
|
||||
const updateTheme = async () => {
|
||||
let settings;
|
||||
settings = await MMKV.getStringAsync('settings');
|
||||
if (settings) {
|
||||
settings = JSON.parse(settings);
|
||||
let newColors = await getColorScheme(settings.useSystemTheme);
|
||||
dispatch({type: Actions.THEME, colors: newColors});
|
||||
}
|
||||
};
|
||||
|
||||
const syncChanges = async () => {
|
||||
dispatch({type: Actions.ALL});
|
||||
},
|
||||
@@ -150,7 +129,7 @@ const App = () => {
|
||||
};
|
||||
|
||||
const onSystemThemeChanged = async () => {
|
||||
updateTheme().then((r) => r);
|
||||
await SettingsService.setTheme();
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
@@ -212,52 +191,12 @@ const App = () => {
|
||||
console.log(error);
|
||||
ToastEvent.show('Error initializing database.');
|
||||
}
|
||||
checkForIntent();
|
||||
IntentService.check();
|
||||
SplashScreen.hide();
|
||||
}, 100);
|
||||
})();
|
||||
}, []);
|
||||
|
||||
const checkForIntent = () => {
|
||||
ReceiveSharingIntent.getReceivedFiles(
|
||||
(d) => {
|
||||
let data = d[0];
|
||||
if (data.text || data.weblink) {
|
||||
let text = data.text;
|
||||
let weblink = data.weblink;
|
||||
let delta = null;
|
||||
|
||||
if (weblink && text) {
|
||||
delta = [{insert: `${text + ' ' + weblink}`}];
|
||||
text = data.text + ' ' + data.weblink;
|
||||
} else if (text && !weblink) {
|
||||
delta = [{insert: `${text}`}];
|
||||
text = data.text;
|
||||
} else if (weblink) {
|
||||
delta = [{insert: `${weblink}`}];
|
||||
text = weblink;
|
||||
}
|
||||
|
||||
prevIntent.text = text;
|
||||
prevIntent.weblink = weblink;
|
||||
setIntent();
|
||||
eSendEvent(eOnLoadNote, {
|
||||
type: 'intent',
|
||||
data: delta,
|
||||
text: text,
|
||||
});
|
||||
if (DDS.isPhone || DDS.isSmallTab) {
|
||||
tabBarRef.current?.goToPage(1);
|
||||
}
|
||||
}
|
||||
ReceiveSharingIntent.clearReceivedFiles();
|
||||
},
|
||||
(error) => {
|
||||
console.log(error, 'INTENT ERROR');
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
async function backupData() {
|
||||
await sleep(1000);
|
||||
let settings = await MMKV.getStringAsync('settings');
|
||||
@@ -281,33 +220,9 @@ const App = () => {
|
||||
}
|
||||
firstLoad = false;
|
||||
}
|
||||
initAppSettings().catch((e) => console.log(e));
|
||||
SettingsService.init().then((r) => r);
|
||||
}
|
||||
|
||||
const initAppSettings = async () => {
|
||||
let settings;
|
||||
scale.fontScale = 1;
|
||||
settings = await MMKV.getStringAsync('settings');
|
||||
if (!settings) {
|
||||
settings = defaultState.settings;
|
||||
await MMKV.setStringAsync('settings', JSON.stringify(settings));
|
||||
} else {
|
||||
settings = JSON.parse(settings);
|
||||
}
|
||||
if (settings.fontScale) {
|
||||
scale.fontScale = settings.fontScale;
|
||||
}
|
||||
if (settings.privacyScreen) {
|
||||
AndroidModule.setSecureMode(true);
|
||||
} else {
|
||||
AndroidModule.setSecureMode(false);
|
||||
}
|
||||
sortSettings.sort = settings.sort;
|
||||
sortSettings.sortOrder = settings.sortOrder;
|
||||
dispatch({type: Actions.SETTINGS, settings: {...settings}});
|
||||
updateSize();
|
||||
await updateTheme();
|
||||
};
|
||||
return (
|
||||
<>
|
||||
<SafeAreaProvider>
|
||||
|
||||
@@ -44,38 +44,6 @@ export const Initialize = () => {
|
||||
}}>
|
||||
<NavigationStack component={MobileStack} />
|
||||
</View>
|
||||
|
||||
{/*
|
||||
<AnimatedScreenContainer
|
||||
ref={editorRef}
|
||||
onResponderTerminationRequest={true}
|
||||
onStartShouldSetResponderCapture={false}
|
||||
onStartShouldSetResponder={false}
|
||||
onMoveShouldSetResponder={false}
|
||||
onMoveShouldSetResponderCapture={false}
|
||||
style={{
|
||||
width: '100%',
|
||||
height: '100%',
|
||||
alignSelf: 'flex-end',
|
||||
position: 'absolute',
|
||||
backgroundColor: colors.bg,
|
||||
elevation: 10,
|
||||
opacity: 0,
|
||||
transform: [
|
||||
{
|
||||
translateY: 5000,
|
||||
},
|
||||
],
|
||||
}}>
|
||||
<Screen
|
||||
active={1}
|
||||
style={{
|
||||
width: '100%',
|
||||
height: '100%',
|
||||
}}>
|
||||
<Editor noMenu={false} />
|
||||
</Screen>
|
||||
</AnimatedScreenContainer> */}
|
||||
</View>
|
||||
<Toast />
|
||||
<ContextMenu />
|
||||
|
||||
@@ -40,7 +40,6 @@ export const Button = ({
|
||||
}) => {
|
||||
const [state] = useTracked();
|
||||
const {colors} = state;
|
||||
console.log(colors);
|
||||
|
||||
return (
|
||||
<PressableButton
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import React, {createRef} from 'react';
|
||||
import {Text, TouchableOpacity, View} from 'react-native';
|
||||
import {eSubscribeEvent, eUnSubscribeEvent} from '../../services/EventManager';
|
||||
import {dWidth, setSetting, SORT, sortSettings} from '../../utils';
|
||||
import {dWidth, SORT, sortSettings} from '../../utils';
|
||||
import ActionSheet from '../ActionSheet';
|
||||
import {DDS} from "../../services/DeviceDetection";
|
||||
import {eCloseSortDialog, eOpenSortDialog} from "../../utils/Events";
|
||||
@@ -13,6 +13,7 @@ import {updateEvent} from "../DialogManager/Recievers";
|
||||
import {Actions} from "../../provider/Actions";
|
||||
import Icon from "react-native-vector-icons/MaterialCommunityIcons";
|
||||
import Seperator from "../Seperator";
|
||||
import SettingsService from '../../services/SettingsService';
|
||||
|
||||
|
||||
|
||||
@@ -108,7 +109,7 @@ class SortDialog extends React.Component {
|
||||
<TouchableOpacity
|
||||
onPress={async () => {
|
||||
let value = this.state.settings.sortOrder === "asc" ? "des" : "asc";
|
||||
await setSetting(this.state.settings, 'sortOrder', value);
|
||||
await SettingsService.set('sortOrder', value)
|
||||
sortSettings.sortOrder = value;
|
||||
await this.getSettings();
|
||||
}}
|
||||
@@ -139,7 +140,7 @@ class SortDialog extends React.Component {
|
||||
key={item}
|
||||
color={this.state.settings.sort === item ? colors.shade : "transparent"}
|
||||
onPress={async () => {
|
||||
await setSetting(this.state.settings, 'sort', item);
|
||||
await SettingsService.set('sort', item);
|
||||
await this.getSettings();
|
||||
sortSettings.sort = item;
|
||||
updateEvent({type: Actions.NOTES})
|
||||
|
||||
56
apps/mobile/src/services/IntentService.js
Normal file
56
apps/mobile/src/services/IntentService.js
Normal file
@@ -0,0 +1,56 @@
|
||||
import ReceiveSharingIntent from 'react-native-receive-sharing-intent';
|
||||
import { eOnLoadNote } from '../utils/Events';
|
||||
import { tabBarRef } from '../utils/Refs';
|
||||
import { setIntent } from '../views/Editor/Functions';
|
||||
import { DDS } from './DeviceDetection';
|
||||
import { eSendEvent } from './EventManager';
|
||||
|
||||
let previousIntent = {
|
||||
text: null,
|
||||
weblink: null,
|
||||
};
|
||||
|
||||
function check() {
|
||||
ReceiveSharingIntent.getReceivedFiles(
|
||||
(d) => {
|
||||
console.log('INTENT SERVICE', d);
|
||||
let data = d[0];
|
||||
if (data.text || data.weblink) {
|
||||
let text = data.text;
|
||||
let weblink = data.weblink;
|
||||
let delta = null;
|
||||
|
||||
if (weblink && text) {
|
||||
delta = [{insert: `${text + ' ' + weblink}`}];
|
||||
text = data.text + ' ' + data.weblink;
|
||||
} else if (text && !weblink) {
|
||||
delta = [{insert: `${text}`}];
|
||||
text = data.text;
|
||||
} else if (weblink) {
|
||||
delta = [{insert: `${weblink}`}];
|
||||
text = weblink;
|
||||
}
|
||||
|
||||
previousIntent.text = text;
|
||||
previousIntent.weblink = weblink;
|
||||
setIntent();
|
||||
eSendEvent(eOnLoadNote, {
|
||||
type: 'intent',
|
||||
data: delta,
|
||||
text: text,
|
||||
});
|
||||
if (DDS.isPhone || DDS.isSmallTab) {
|
||||
tabBarRef.current?.goToPage(1);
|
||||
}
|
||||
}
|
||||
ReceiveSharingIntent.clearReceivedFiles();
|
||||
},
|
||||
(error) => {
|
||||
console.log(error, 'INTENT ERROR');
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
export default {
|
||||
check,
|
||||
};
|
||||
57
apps/mobile/src/services/SettingsService.js
Normal file
57
apps/mobile/src/services/SettingsService.js
Normal file
@@ -0,0 +1,57 @@
|
||||
import {updateEvent} from '../components/DialogManager/recievers';
|
||||
import {Actions} from '../provider/Actions';
|
||||
import {defaultState} from '../provider/DefaultState';
|
||||
import {AndroidModule, sortSettings} from '../utils';
|
||||
import {getColorScheme} from '../utils/ColorUtils';
|
||||
import {MMKV} from '../utils/mmkv';
|
||||
import {scale, updateSize} from '../utils/SizeUtils';
|
||||
|
||||
let settings = defaultState.settings;
|
||||
|
||||
async function init() {
|
||||
scale.fontScale = 1;
|
||||
settings = await MMKV.getStringAsync('settings');
|
||||
if (!settings) {
|
||||
settings = defaultState.settings;
|
||||
await MMKV.setStringAsync('settings', JSON.stringify(settings));
|
||||
} else {
|
||||
settings = JSON.parse(settings);
|
||||
}
|
||||
if (settings.fontScale) {
|
||||
scale.fontScale = settings.fontScale;
|
||||
}
|
||||
if (settings.privacyScreen) {
|
||||
AndroidModule.setSecureMode(true);
|
||||
} else {
|
||||
AndroidModule.setSecureMode(false);
|
||||
}
|
||||
sortSettings.sort = settings.sort;
|
||||
sortSettings.sortOrder = settings.sortOrder;
|
||||
updateSize();
|
||||
updateEvent({type: Actions.SETTINGS, settings: {...settings}});
|
||||
await setTheme();
|
||||
}
|
||||
|
||||
const setTheme = async () => {
|
||||
if (settings) {
|
||||
let newColors = await getColorScheme(settings.useSystemTheme);
|
||||
updateEvent({type: Actions.THEME, colors: newColors});
|
||||
}
|
||||
};
|
||||
|
||||
async function set(name, value) {
|
||||
settings[name] = value;
|
||||
await MMKV.setStringAsync('settings', JSON.stringify(s));
|
||||
updateEvent({type: Actions.SETTINGS, settings: s});
|
||||
}
|
||||
|
||||
function get() {
|
||||
return settings;
|
||||
}
|
||||
|
||||
export default {
|
||||
init,
|
||||
setTheme,
|
||||
set,
|
||||
get,
|
||||
};
|
||||
@@ -21,12 +21,12 @@ import {DDS} from '../../services/DeviceDetection';
|
||||
import {eSendEvent, ToastEvent} from '../../services/EventManager';
|
||||
import NavigationService from '../../services/Navigation';
|
||||
import PremiumService from '../../services/PremiumService';
|
||||
import SettingsService from '../../services/SettingsService';
|
||||
import {
|
||||
AndroidModule,
|
||||
dWidth,
|
||||
MenuItemsList,
|
||||
setSetting,
|
||||
SUBSCRIPTION_STATUS,
|
||||
SUBSCRIPTION_STATUS_STRINGS,
|
||||
} from '../../utils';
|
||||
import {
|
||||
@@ -151,7 +151,7 @@ export const Settings = ({navigation}) => {
|
||||
|
||||
const switchTheme = async () => {
|
||||
await PremiumService.verify(async () => {
|
||||
await setSetting(settings, 'useSystemTheme', !settings.useSystemTheme);
|
||||
await SettingsService.set('useSystemTheme', !settings.useSystemTheme);
|
||||
|
||||
if (!settings.useSystemTheme) {
|
||||
await MMKV.setStringAsync(
|
||||
@@ -659,7 +659,7 @@ export const Settings = ({navigation}) => {
|
||||
<MenuItem
|
||||
key={item.name}
|
||||
onPress={async () => {
|
||||
await setSetting(settings, 'homepage', item.name);
|
||||
await SettingsService.set('homepage', item.name);
|
||||
}}
|
||||
style={{
|
||||
backgroundColor:
|
||||
@@ -739,7 +739,7 @@ export const Settings = ({navigation}) => {
|
||||
<TouchableOpacity
|
||||
activeOpacity={1}
|
||||
onPress={async () => {
|
||||
await setSetting(settings, 'fontScale', item.value);
|
||||
await SettingsService.set('fontScale', item.value);
|
||||
eSendEvent(eResetApp);
|
||||
}}
|
||||
key={item.title}
|
||||
@@ -770,8 +770,7 @@ export const Settings = ({navigation}) => {
|
||||
<CustomButton
|
||||
title="Force portrait mode"
|
||||
onPress={async () => {
|
||||
await setSetting(
|
||||
settings,
|
||||
await SettingsService.set(
|
||||
'forcePortraitOnTablet',
|
||||
!settings.forcePortraitOnTablet,
|
||||
);
|
||||
@@ -922,7 +921,7 @@ export const Settings = ({navigation}) => {
|
||||
activeOpacity={1}
|
||||
onPress={async () => {
|
||||
await PremiumService.verify(async () => {
|
||||
await setSetting(settings, 'reminder', item.value);
|
||||
await SettingsService.set('reminder', item.value);
|
||||
});
|
||||
}}
|
||||
key={item.value}
|
||||
@@ -968,8 +967,7 @@ export const Settings = ({navigation}) => {
|
||||
);
|
||||
return;
|
||||
}
|
||||
await setSetting(
|
||||
settings,
|
||||
await SettingsService.set(
|
||||
'encryptedBackup',
|
||||
!settings.encryptedBackup,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user