Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

refactor to improve navigation performance

ammarahm-ed471eb158e15b
diff --git a/apps/mobile/src/components/Header/HeaderLeftMenu.js b/apps/mobile/src/components/Header/HeaderLeftMenu.js
index 128c6c607..a03e00210 100644
--- a/apps/mobile/src/components/Header/HeaderLeftMenu.js
+++ b/apps/mobile/src/components/Header/HeaderLeftMenu.js
@@ -1,14 +1,33 @@
-import React from 'react';
+import React, { useEffect, useState } from 'react';
import {notesnook} from '../../../e2e/test.ids';
import {useTracked} from '../../provider';
import { DDS } from '../../services/DeviceDetection';
+import { eSubscribeEvent, eUnSubscribeEvent } from '../../services/EventManager';
import Navigation from '../../services/Navigation';
import {SIZE} from '../../utils/SizeUtils';
import {ActionIcon} from '../ActionIcon';
export const HeaderLeftMenu = () => {
const [state] = useTracked();
- const {colors, headerMenuState, currentScreen, deviceMode} = state;
+ const {colors, deviceMode} = state;
+ const [headerTextState, setHeaderTextState] = useState(
+ Navigation.getHeaderState(),
+ );
+
+ const currentScreen = headerTextState.currentScreen;
+ const headerMenuState = headerTextState.verticalMenu;
+
+ const onHeaderStateChange = (event) => {
+ if (!event) return;
+ setHeaderTextState(event);
+ };
+
+ useEffect(() => {
+ eSubscribeEvent('onHeaderStateChange', onHeaderStateChange);
+ return () => {
+ eUnSubscribeEvent('onHeaderStateChange', onHeaderStateChange);
+ };
+ }, []);
const onLeftButtonPress = () => {
if (headerMenuState) {
@@ -19,7 +38,6 @@ export const HeaderLeftMenu = () => {
};
-
return (
<>
{deviceMode === 'mobile' ||
diff --git a/apps/mobile/src/components/Header/HeaderRightMenu.js b/apps/mobile/src/components/Header/HeaderRightMenu.js
index 875515f69..e6aa4aef1 100644
--- a/apps/mobile/src/components/Header/HeaderRightMenu.js
+++ b/apps/mobile/src/components/Header/HeaderRightMenu.js
@@ -1,8 +1,9 @@
-import React from 'react';
+import React, {useEffect, useState} from 'react';
import {ActivityIndicator, StyleSheet, View} from 'react-native';
-import { notesnook } from '../../../e2e/test.ids';
+import {notesnook} from '../../../e2e/test.ids';
import {useTracked} from '../../provider';
import {DDS} from '../../services/DeviceDetection';
+import {eSubscribeEvent, eUnSubscribeEvent} from '../../services/EventManager';
import Navigation from '../../services/Navigation';
import {dWidth} from '../../utils';
import {SIZE} from '../../utils/SizeUtils';
@@ -11,26 +12,42 @@ import {Button} from '../Button';
export const HeaderRightMenu = () => {
const [state] = useTracked();
- const {colors, containerBottomButton, currentScreen, syncing} = state;
+ const {colors, containerBottomButton, syncing} = state;
+ const [headerTextState, setHeaderTextState] = useState(
+ Navigation.getHeaderState(),
+ );
+ const currentScreen = headerTextState.currentScreen;
+
+ const onHeaderStateChange = (event) => {
+ if (!event) return;
+ setHeaderTextState(event);
+ };
+
+ useEffect(() => {
+ eSubscribeEvent('onHeaderStateChange', onHeaderStateChange);
+ return () => {
+ eUnSubscribeEvent('onHeaderStateChange', onHeaderStateChange);
+ };
+ }, []);
return (
<View style={styles.rightBtnContainer}>
{syncing && <ActivityIndicator size={SIZE.xl} color={colors.accent} />}
- {
- currentScreen !== "settings" && <ActionIcon
- onPress={async () => {
- Navigation.navigate('Search', {
- menu: false,
- });
- }}
- testID={notesnook.ids.default.header.buttons.left}
- name="magnify"
- size={SIZE.xxxl}
- color={colors.pri}
- customStyle={styles.rightBtn}
- />
- }
+ {currentScreen !== 'settings' && (
+ <ActionIcon
+ onPress={async () => {
+ Navigation.navigate('Search', {
+ menu: false,
+ });
+ }}
+ testID={notesnook.ids.default.header.buttons.left}
+ name="magnify"
+ size={SIZE.xxxl}
+ color={colors.pri}
+ customStyle={styles.rightBtn}
+ />
+ )}
{DDS.isLargeTablet() && containerBottomButton.onPress ? (
<Button
diff --git a/apps/mobile/src/components/Header/index.js b/apps/mobile/src/components/Header/index.js
index a0ed1aeb7..e69cc4da9 100644
--- a/apps/mobile/src/components/Header/index.js
+++ b/apps/mobile/src/components/Header/index.js
@@ -3,6 +3,7 @@ import { Platform, StyleSheet, View } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { useTracked } from '../../provider';
import { eSubscribeEvent, eUnSubscribeEvent } from '../../services/EventManager';
+import Navigation from '../../services/Navigation';
import SearchService from '../../services/SearchService';
import { dWidth } from '../../utils';
import { eScrollEvent } from '../../utils/Events';
@@ -15,9 +16,30 @@ import { HeaderTitle } from './HeaderTitle';
export const Header = ({root}) => {
const [state] = useTracked();
- const {colors, currentScreen} = state;
+ const {colors} = state;
const insets = useSafeAreaInsets();
const [hide, setHide] = useState(true);
+ const [headerTextState, setHeaderTextState] = useState(
+ Navigation.getHeaderState(),
+ );
+ const currentScreen = headerTextState.currentScreen;
+
+ const onHeaderStateChange = (event) => {
+ if (!event) return;
+ setHeaderTextState(event);
+ };
+
+ useEffect(() => {
+ eSubscribeEvent('onHeaderStateChange', onHeaderStateChange);
+ return () => {
+ eUnSubscribeEvent('onHeaderStateChange', onHeaderStateChange);
+ };
+ }, []);
+
+
+
+
+
const onScroll = (y) => {
if (y > 150) {
diff --git a/apps/mobile/src/components/SelectionHeader/index.js b/apps/mobile/src/components/SelectionHeader/index.js
index 5d959ee8b..7021bc55e 100644
--- a/apps/mobile/src/components/SelectionHeader/index.js
+++ b/apps/mobile/src/components/SelectionHeader/index.js
@@ -1,35 +1,52 @@
-import React, { useEffect } from 'react';
-import { View } from 'react-native';
-import Animated, { Easing, useValue } from 'react-native-reanimated';
-import { useSafeAreaInsets } from 'react-native-safe-area-context';
-import { useTracked } from '../../provider';
-import { Actions } from '../../provider/Actions';
-import { eSendEvent, ToastEvent } from '../../services/EventManager';
-import { db } from '../../utils/DB';
+import React, {useEffect, useState} from 'react';
+import {View} from 'react-native';
+import Animated, {Easing, useValue} from 'react-native-reanimated';
+import {useSafeAreaInsets} from 'react-native-safe-area-context';
+import {useTracked} from '../../provider';
+import {Actions} from '../../provider/Actions';
+import {
+ eSendEvent,
+ eSubscribeEvent,
+ eUnSubscribeEvent,
+ ToastEvent,
+} from '../../services/EventManager';
+import Navigation from '../../services/Navigation';
+import {db} from '../../utils/DB';
import {
eOpenMoveNoteDialog,
eOpenSimpleDialog,
- refreshNotesPage
+ refreshNotesPage,
} from '../../utils/Events';
-import { SIZE } from '../../utils/SizeUtils';
-import { sleep } from '../../utils/TimeUtils';
-import { ActionIcon } from '../ActionIcon';
-import { TEMPLATE_DELETE } from '../DialogManager/Templates';
+import {SIZE} from '../../utils/SizeUtils';
+import {sleep} from '../../utils/TimeUtils';
+import {ActionIcon} from '../ActionIcon';
+import {TEMPLATE_DELETE} from '../DialogManager/Templates';
import Heading from '../Typography/Heading';
export const SelectionHeader = () => {
// State
const [state, dispatch] = useTracked();
- const {
- colors,
- selectionMode,
- selectedItemsList,
- currentScreen,
- premiumUser,
- } = state;
+ const {colors, selectionMode, selectedItemsList} = state;
const insets = useSafeAreaInsets();
const translateY = useValue(-150);
+ const [headerTextState, setHeaderTextState] = useState(
+ Navigation.getHeaderState(),
+ );
+ const currentScreen = headerTextState.currentScreen;
+
+ const onHeaderStateChange = (event) => {
+ if (!event) return;
+ setHeaderTextState(event);
+ };
+
+ useEffect(() => {
+ eSubscribeEvent('onHeaderStateChange', onHeaderStateChange);
+ return () => {
+ eUnSubscribeEvent('onHeaderStateChange', onHeaderStateChange);
+ };
+ }, []);
+
useEffect(() => {
Animated.timing(translateY, {
duration: 300,
diff --git a/apps/mobile/src/navigation/NavigatorStack.js b/apps/mobile/src/navigation/NavigatorStack.js
index d3c2a2a8f..bc6432a96 100644
--- a/apps/mobile/src/navigation/NavigatorStack.js
+++ b/apps/mobile/src/navigation/NavigatorStack.js
@@ -6,6 +6,7 @@ import Container from '../components/Container';
import {updateEvent} from '../components/DialogManager/recievers';
import {Actions} from '../provider/Actions';
import {eSendEvent} from '../services/EventManager';
+import Navigation from '../services/Navigation';
import SettingsService from '../services/SettingsService';
import {rootNavigatorRef} from '../utils/Refs';
import {sleep} from '../utils/TimeUtils';
@@ -75,12 +76,16 @@ export const NavigatorStack = React.memo(
() => {
React.useEffect(() => {
sleep(2000).then(() => {
- let headerState = {
- heading: SettingsService.get().homepage,
- id: SettingsService.get().homepage.toLowerCase() + '_navigation',
- };
- updateEvent({type: Actions.HEADER_TEXT_STATE, state: headerState});
- eSendEvent('onHeaderStateChange', headerState);
+ Navigation.setHeaderState(
+ SettingsService.get().homepage,
+ {
+ menu: true,
+ },
+ {
+ heading: SettingsService.get().homepage,
+ id: SettingsService.get().homepage.toLowerCase() + '_navigation',
+ },
+ );
});
}, []);
@@ -90,7 +95,7 @@ export const NavigatorStack = React.memo(
onStateChange={() => {
updateEvent({type: Actions.SELECTION_MODE, enabled: false});
updateEvent({type: Actions.CLEAR_SELECTION});
- eSendEvent("navigate")
+ eSendEvent('navigate');
}}
independent={true}
ref={rootNavigatorRef}>
diff --git a/apps/mobile/src/services/Navigation.js b/apps/mobile/src/services/Navigation.js
index b6ba18747..9ba42fb20 100755
--- a/apps/mobile/src/services/Navigation.js
+++ b/apps/mobile/src/services/Navigation.js
@@ -1,15 +1,17 @@
-import { DrawerActions, StackActions } from '@react-navigation/native';
-import { updateEvent } from '../components/DialogManager/recievers';
-import { Actions } from '../provider/Actions';
-import { rootNavigatorRef, sideMenuRef } from '../utils/Refs';
-import { eSendEvent } from './EventManager';
+import {DrawerActions, StackActions} from '@react-navigation/native';
+import {updateEvent} from '../components/DialogManager/recievers';
+import {Actions} from '../provider/Actions';
+import {rootNavigatorRef, sideMenuRef} from '../utils/Refs';
+import {eSendEvent} from './EventManager';
import SettingsService from './SettingsService';
-let currentScreen = "Notes"
+let currentScreen = 'Notes';
let headerState = {
heading: 'Notes',
color: null,
-}
+ verticalMenu:true,
+ currentScreen:'Notes'
+};
function getCurrentScreen() {
return currentScreen;
}
@@ -46,12 +48,18 @@ function navigate(name, params, item) {
* @param {screenParams} item
*/
function setHeaderState(name, params, item) {
- headerState = item;
- currentScreen = name;
if (item) {
- eSendEvent('onHeaderStateChange', item);
+ headerState = item;
+ }
+ currentScreen = name;
+ headerState.currentScreen = name.toLowerCase();
+ headerState.verticalMenu = params.menu;
+
+ if (headerState) {
+ eSendEvent('onHeaderStateChange', headerState);
}
- if (name) {
+
+ /* if (name) {
updateEvent({
type: Actions.CURRENT_SCREEN,
screen: name.toLowerCase(),
@@ -62,7 +70,7 @@ function setHeaderState(name, params, item) {
type: Actions.HEADER_STATE,
state: params.menu,
});
- }
+ } */
}
function goBack() {
@@ -111,5 +119,5 @@ export default {
setHeaderState,
replace,
popToTop,
- getHeaderState
+ getHeaderState,
};