Skip to content

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

mobile: revise app shortcut handling implementation

kashaf-ansari-deva40814a3ab57
diff --git a/apps/mobile/app/app.tsx b/apps/mobile/app/app.tsx
index 6224cf8e6..b8fd22ba3 100644
--- a/apps/mobile/app/app.tsx
+++ b/apps/mobile/app/app.tsx
@@ -44,30 +44,32 @@ import { useUserStore } from "./stores/use-user-store";
import RNBootSplash from "react-native-bootsplash";
import AppLocked from "./components/app-lock";
import { useSettingStore } from "./stores/use-setting-store";
-import { registerAppShortcuts } from "./hooks/use-shortcut-manager";
-import Shortcuts, { ShortcutItem } from "react-native-actions-shortcuts";
+import {
+ initShortcutListener,
+ registerAppShortcuts
+} from "./hooks/use-shortcut-manager";
+import Shortcuts from "react-native-actions-shortcuts";
I18nManager.allowRTL(false);
I18nManager.forceRTL(false);
I18nManager.swapLeftAndRightInRTL(false);
-declare global {
- var __pendingShortcut: ShortcutItem | null | undefined;
-}
const { appLockEnabled, appLockMode } = SettingsService.get();
if (appLockEnabled || appLockMode !== "none") {
useUserStore.getState().lockApp(true);
}
-RNBootSplash.hide({
- fade: true
-});
+initShortcutListener();
Linking.getInitialURL().then((url) => {
- useSettingStore.setState({
- initialUrl: url
- });
+ useSettingStore.setState({ initialUrl: url });
});
+
Shortcuts.getInitialShortcut().then((shortcut) => {
- globalThis.__pendingShortcut = shortcut;
+ useSettingStore.setState({
+ pendingShortcut: shortcut ?? null,
+ pendingShortcutLoaded: true
+ });
+ RNBootSplash.hide({ fade: true });
});
+
const App = (props: { configureMode: "note-preview" }) => {
useAppEvents();
//@ts-ignore
diff --git a/apps/mobile/app/components/fluid-panels/index.tsx b/apps/mobile/app/components/fluid-panels/index.tsx
index c2c0d0372..072be6f7e 100644
--- a/apps/mobile/app/components/fluid-panels/index.tsx
+++ b/apps/mobile/app/components/fluid-panels/index.tsx
@@ -51,9 +51,10 @@ interface TabProps extends ViewProps {
onScroll: (offset: number) => void;
enabled: boolean;
onDrawerStateChange: (state: boolean) => void;
+ initialPage?: FluidTabPage;
}
-type FluidTabPage = "home" | "editor";
+export type FluidTabPage = "home" | "editor";
export interface TabsRef {
goToPage: (page: FluidTabPage, animated?: boolean) => void;
@@ -77,15 +78,19 @@ export const FluidPanels = forwardRef<TabsRef, TabProps>(function FluidTabs(
onChangeTab,
onScroll,
enabled,
- onDrawerStateChange
+ onDrawerStateChange,
+ initialPage
}: TabProps,
ref
) {
const deviceMode = useSettingStore((state) => state.deviceMode);
const fullscreen = useSettingStore((state) => state.fullscreen);
- const translateX = useSharedValue(widths ? widths.sidebar : 0);
+ const editorStartPosition = widths.sidebar + widths.list;
+ const translateX = useSharedValue(
+ initialPage === "editor" ? editorStartPosition : widths ? widths.sidebar : 0
+ );
const startX = useSharedValue(0);
- const currentTab = useSharedValue(1);
+ const currentTab = useSharedValue(initialPage === "editor" ? 2 : 1);
const previousTab = useSharedValue(1);
const isDrawerOpen = useSharedValue(false);
const gestureStartValue = useSharedValue({
diff --git a/apps/mobile/app/hooks/use-shortcut-manager.ts b/apps/mobile/app/hooks/use-shortcut-manager.ts
index 7f9fcb614..224ac9a09 100644
--- a/apps/mobile/app/hooks/use-shortcut-manager.ts
+++ b/apps/mobile/app/hooks/use-shortcut-manager.ts
@@ -17,19 +17,19 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Shortcuts, { ShortcutItem } from "react-native-actions-shortcuts";
-import { useEffect } from "react";
-import { NativeEventEmitter, NativeModule } from "react-native";
-import { useRef } from "react";
-import { Platform } from "react-native";
+import { NativeEventEmitter, NativeModule, Platform } from "react-native";
import deviceInfoModule from "react-native-device-info";
import { strings } from "@notesnook/intl";
+import { useSettingStore } from "../stores/use-setting-store";
+
const ShortcutsEmitter = new NativeEventEmitter(
Shortcuts as unknown as NativeModule
);
-function isSupported() {
+export function isShortcutsSupported() {
return Platform.OS !== "android" || deviceInfoModule.getApiLevelSync() > 25;
}
+
const defaultShortcuts: ShortcutItem[] = [
{
type: "notesnook.action.newnote",
@@ -44,24 +44,22 @@ const defaultShortcuts: ShortcutItem[] = [
iconName: Platform.OS === "android" ? "ic_newnote" : "plus"
}
];
-export const useShortcutManager = ({
- onShortcutPressed
-}: {
- onShortcutPressed: (shortcut: ShortcutItem | null) => void;
-}) => {
- useEffect(() => {
- if (!isSupported()) return;
- const subscription = ShortcutsEmitter.addListener(
- "onShortcutItemPressed",
- onShortcutPressed
- );
- return () => {
- subscription?.remove();
- };
- }, [onShortcutPressed]);
-};
-export const registerAppShortcuts = () => {
- if (!isSupported()) return;
- Shortcuts.setShortcuts(defaultShortcuts);
-};
+export function registerAppShortcuts(
+ shortcuts: ShortcutItem[] = defaultShortcuts
+) {
+ if (!isShortcutsSupported()) return;
+ Shortcuts.setShortcuts(shortcuts);
+}
+
+let listenerInitialized = false;
+export function initShortcutListener() {
+ if (!isShortcutsSupported() || listenerInitialized) return;
+ listenerInitialized = true;
+ ShortcutsEmitter.addListener(
+ "onShortcutItemPressed",
+ (shortcut: ShortcutItem) => {
+ useSettingStore.setState({ pendingShortcut: shortcut });
+ }
+ );
+}
diff --git a/apps/mobile/app/navigation/fluid-panels-view.tsx b/apps/mobile/app/navigation/fluid-panels-view.tsx
index 7a9a601d5..677f6ac31 100644
--- a/apps/mobile/app/navigation/fluid-panels-view.tsx
+++ b/apps/mobile/app/navigation/fluid-panels-view.tsx
@@ -41,10 +41,9 @@ import Animated, {
} from "react-native-reanimated";
import { notesnook } from "../../e2e/test.ids";
import { db } from "../common/database";
-import { FluidPanels } from "../components/fluid-panels";
+import { FluidPanels, FluidTabPage } from "../components/fluid-panels";
import { useSideBarDraggingStore } from "../components/side-menu/dragging-store";
import useGlobalSafeAreaInsets from "../hooks/use-global-safe-area-insets";
-import { useShortcutManager } from "../hooks/use-shortcut-manager";
import { hideAllTooltips } from "../hooks/use-tooltip";
import { useTabStore } from "../screens/editor/tiptap/use-tab-store";
import { editorController, editorState } from "../screens/editor/tiptap/utils";
@@ -67,9 +66,7 @@ import { valueLimiter } from "../utils/functions";
import { fluidTabsRef } from "../utils/global-refs";
import { AppNavigationStack } from "./navigation-stack";
import type { PaneWidths } from "../screens/editor/wrapper";
-import AddReminder from "../screens/add-reminder";
import Navigation from "../services/navigation";
-import { ShortcutItem } from "react-native-actions-shortcuts";
const MOBILE_SIDEBAR_SIZE = 0.85;
@@ -94,6 +91,18 @@ export const FluidPanelsView = React.memo(
);
const appLoading = useSettingStore((state) => state.isAppLoading);
const [isLoading, setIsLoading] = useState(false);
+ const pendingShortcut = useSettingStore((state) => state.pendingShortcut);
+ const [initialPane] = useState<FluidTabPage>(() =>
+ pendingShortcut?.type === "notesnook.action.newnote" ? "editor" : "home"
+ );
+
+ useEffect(() => {
+ if (pendingShortcut?.type === "notesnook.action.newnote") {
+ eSendEvent(eOnLoadNote, { newNote: true });
+ editorState().movedAway = false;
+ useSettingStore.setState({ pendingShortcut: null });
+ }
+ }, []);
useDeviceOrientationChange((o) => {
if (
@@ -114,38 +123,6 @@ export const FluidPanelsView = React.memo(
}
}, [appLoading]);
- useEffect(() => {
- const pending = globalThis.__pendingShortcut;
- if (
- pending?.type === "notesnook.action.newnote" &&
- fluidTabsRef.current &&
- !appLoading
- ) {
- eSendEvent(eOnLoadNote, { newNote: true });
- editorState().movedAway = false;
- fluidTabsRef.current.goToPage("editor", false);
- globalThis.__pendingShortcut = null;
- }
- }, [deviceMode, appLoading]);
-
- const onShortcutPressed = useCallback(async (item: ShortcutItem | null) => {
- if (!item) return;
-
- if (item?.type === "notesnook.action.newnote") {
- Navigation.navigate("FluidPanelsView");
- requestAnimationFrame(() => {
- eSendEvent(eOnLoadNote, { newNote: true });
- editorState().movedAway = false;
- fluidTabsRef.current?.goToPage("editor", false);
- });
- }
- if (item?.type === "notesnook.action.newreminder") {
- AddReminder.present();
- }
- }, []);
-
- useShortcutManager({ onShortcutPressed });
-
const showFullScreenEditor = useCallback(() => {
setFullscreen(true);
if (deviceMode === "smallTablet") {
@@ -368,6 +345,7 @@ export const FluidPanelsView = React.memo(
dimensions={dimensions}
widths={PANE_WIDTHS[deviceMode as keyof typeof PANE_WIDTHS]}
enabled={deviceMode !== "tablet" && !fullscreen}
+ initialPage={initialPane}
onScroll={onScroll}
onChangeTab={onChangeTab}
onDrawerStateChange={(state) => {
diff --git a/apps/mobile/app/navigation/navigation-stack.tsx b/apps/mobile/app/navigation/navigation-stack.tsx
index 6dd0e4121..7dc5cac78 100644
--- a/apps/mobile/app/navigation/navigation-stack.tsx
+++ b/apps/mobile/app/navigation/navigation-stack.tsx
@@ -27,10 +27,13 @@ import useNavigationStore, {
} from "../stores/use-navigation-store";
import { useSelectionStore } from "../stores/use-selection-store";
import { useSettingStore } from "../stores/use-setting-store";
-import { rootNavigatorRef } from "../utils/global-refs";
+import { fluidTabsRef, rootNavigatorRef } from "../utils/global-refs";
import Navigation from "../services/navigation";
import { isFeatureAvailable } from "@notesnook/common";
import { isInternalLink, parseInternalLink } from "@notesnook/core";
+import { eSendEvent } from "../services/event-manager";
+import { editorState } from "../screens/editor/tiptap/utils";
+import { eOnLoadNote } from "../utils/events";
const RootStack = createNativeStackNavigator();
const AppStack = createNativeStackNavigator();
@@ -300,8 +303,13 @@ export const RootNavigation = () => {
const introCompleted = useSettingStore(
(state) => state.settings.introCompleted
);
+ const pendingShortcut = useSettingStore((state) => state.pendingShortcut);
+ const pendingShortcutLoaded = useSettingStore(
+ (state) => state.pendingShortcutLoaded
+ );
const clearSelection = useSelectionStore((state) => state.clearSelection);
const resetTimer = React.useRef<NodeJS.Timeout>(undefined);
+ const isFirstRun = React.useRef(true);
const onStateChange = React.useCallback(
(state: any) => {
if (useSelectionStore.getState().selectionMode) {
@@ -316,28 +324,59 @@ export const RootNavigation = () => {
[clearSelection]
);
- const onNavigationReady = React.useCallback(() => {
- const pending = globalThis.__pendingShortcut;
- if (pending?.type === "notesnook.action.newreminder") {
- rootNavigatorRef.current?.navigate("AddReminder", {
- reminder: undefined,
- reference: undefined
- });
- globalThis.__pendingShortcut = null;
+ React.useEffect(() => {
+ if (pendingShortcut?.type === "notesnook.action.newreminder") {
+ useSettingStore.setState({ pendingShortcut: null });
}
}, []);
+ React.useEffect(() => {
+ if (isFirstRun.current) {
+ isFirstRun.current = false;
+ return;
+ }
+ if (!pendingShortcut) return;
+
+ const routes = rootNavigatorRef.current?.getState()?.routes;
+ const currentRoute = routes?.[routes.length - 1]?.name;
+
+ if (pendingShortcut.type === "notesnook.action.newreminder") {
+ if (currentRoute !== "AddReminder") {
+ rootNavigatorRef.current?.navigate("AddReminder" as any);
+ }
+ useSettingStore.setState({ pendingShortcut: null });
+ } else if (pendingShortcut.type === "notesnook.action.newnote") {
+ if (fluidTabsRef.current) {
+ if (currentRoute !== "FluidPanelsView") {
+ rootNavigatorRef.current?.navigate("FluidPanelsView" as any);
+ }
+ eSendEvent(eOnLoadNote, { newNote: true });
+ editorState().movedAway = false;
+ fluidTabsRef.current.goToPage("editor", true);
+ useSettingStore.setState({ pendingShortcut: null });
+ } else {
+ if (currentRoute !== "FluidPanelsView") {
+ rootNavigatorRef.current?.navigate("FluidPanelsView" as any);
+ }
+ }
+ }
+ }, [pendingShortcut]);
+
+ if (!pendingShortcutLoaded) return null;
+
+ const initialRouteName = !introCompleted
+ ? "Welcome"
+ : pendingShortcut?.type === "notesnook.action.newreminder"
+ ? "AddReminder"
+ : "FluidPanelsView";
+
return (
- <NavigationContainer
- onStateChange={onStateChange}
- onReady={onNavigationReady}
- ref={rootNavigatorRef}
- >
+ <NavigationContainer onStateChange={onStateChange} ref={rootNavigatorRef}>
<RootStack.Navigator
screenOptions={{
headerShown: false
}}
- initialRouteName={introCompleted ? "FluidPanelsView" : "Welcome"}
+ initialRouteName={initialRouteName}
>
<RootStack.Screen
name="Welcome"
diff --git a/apps/mobile/app/screens/add-reminder/index.tsx b/apps/mobile/app/screens/add-reminder/index.tsx
index c880c3e99..d5aadf7c6 100644
--- a/apps/mobile/app/screens/add-reminder/index.tsx
+++ b/apps/mobile/app/screens/add-reminder/index.tsx
@@ -20,8 +20,9 @@ import { Note, Reminder } from "@notesnook/core";
import { strings } from "@notesnook/intl";
import { useThemeColors } from "@notesnook/theme";
import dayjs from "dayjs";
-import React, { useRef, useState } from "react";
+import React, { useCallback, useEffect, useRef, useState } from "react";
import {
+ BackHandler,
KeyboardAvoidingView,
Platform,
ScrollView,
@@ -106,6 +107,23 @@ export default function AddReminder(props: NavigationProps<"AddReminder">) {
return false;
}
});
+ const handleBackNavigation = useCallback(() => {
+ const routes = props.navigation.getState()?.routes;
+ if (routes && routes.length <= 1) {
+ props.navigation.navigate("FluidPanelsView" as any);
+ return true;
+ }
+ Navigation.goBack();
+ return true;
+ }, [props.navigation]);
+
+ useEffect(() => {
+ const sub = BackHandler.addEventListener("hardwareBackPress", () => {
+ return handleBackNavigation();
+ });
+ return () => sub.remove();
+ }, [handleBackNavigation]);
+
const { colors, isDark } = useThemeColors();
const weekFormat = useSettingStore((state) => state.weekFormat);
const [reminderMode, setReminderMode] = useState<Reminder["mode"]>(
@@ -267,6 +285,7 @@ export default function AddReminder(props: NavigationProps<"AddReminder">) {
<Header
title={reminder ? strings.editReminder() : strings.newReminder()}
canGoBack
+ onLeftMenuButtonPress={handleBackNavigation}
rightButton={{
name: "check",
onPress: saveReminder
diff --git a/apps/mobile/app/stores/use-setting-store.ts b/apps/mobile/app/stores/use-setting-store.ts
index 6fc0f67f7..1726e7bb9 100644
--- a/apps/mobile/app/stores/use-setting-store.ts
+++ b/apps/mobile/app/stores/use-setting-store.ts
@@ -27,6 +27,7 @@ import { ThemeDark, ThemeLight, ThemeDefinition } from "@notesnook/theme";
import { DayFormat, WeekFormat, Reminder } from "@notesnook/core";
import { db } from "../common/database";
import { EDITOR_LINE_HEIGHT } from "../utils/constants";
+import { ShortcutItem } from "react-native-actions-shortcuts";
export const HostIds = [
"API_HOST",
"AUTH_HOST",
@@ -149,6 +150,8 @@ export interface SettingStore {
refresh: () => void;
inboxEnabled: boolean;
setInboxEnabled: (inboxEnabled: boolean) => void;
+ pendingShortcut: ShortcutItem | null;
+ pendingShortcutLoaded: boolean;
}
const { width, height } = Dimensions.get("window");
@@ -269,5 +272,7 @@ export const useSettingStore = create<SettingStore>((set, get) => ({
});
},
inboxEnabled: false,
- setInboxEnabled: (inboxEnabled) => set({ inboxEnabled })
+ setInboxEnabled: (inboxEnabled) => set({ inboxEnabled }),
+ pendingShortcut: null,
+ pendingShortcutLoaded: false
}));