From c08cde9f4bc7e75c920d9ea43f404cbe2a1f1691 Mon Sep 17 00:00:00 2001 From: ammarahm-ed Date: Wed, 14 Sep 2022 14:24:51 +0500 Subject: [PATCH] mobile: open app from quick-note widget ios --- apps/mobile/app/hooks/use-app-events.js | 36 +++++---- apps/mobile/app/hooks/use-shortcut-manager.ts | 6 ++ apps/mobile/app/navigation/tabs-holder.js | 2 + .../native/ios/Notesnook/AppDelegate.mm | 78 +++---------------- 4 files changed, 42 insertions(+), 80 deletions(-) diff --git a/apps/mobile/app/hooks/use-app-events.js b/apps/mobile/app/hooks/use-app-events.js index 8098d87ba..fdc8866ed 100644 --- a/apps/mobile/app/hooks/use-app-events.js +++ b/apps/mobile/app/hooks/use-app-events.js @@ -46,6 +46,7 @@ import { MMKV } from "../common/database/mmkv"; import { eClearEditor, eCloseProgressDialog, + eOnLoadNote, refreshNotesPage } from "../utils/events"; import Sync from "../services/sync"; @@ -64,6 +65,8 @@ import { import { useEditorStore } from "../stores/use-editor-store"; import { useDragState } from "../screens/settings/editor/state"; import { useCallback } from "react"; +import { clearAppState } from "../screens/editor/tiptap/utils"; +import { tabBarRef } from "../utils/global-refs"; const SodiumEventEmitter = new NativeEventEmitter(NativeModules.Sodium); export const useAppEvents = () => { @@ -79,7 +82,8 @@ export const useAppEvents = () => { prevState: null, showingDialog: false, removeInternetStateListener: null, - isReconnecting: false + isReconnecting: false, + initialUrl: null }); const onLoadingAttachmentProgress = (data) => { @@ -165,22 +169,24 @@ export const useAppEvents = () => { }; useEffect(() => { + if (!refValues.current.initialUrl) { + Linking.getInitialURL().then((url) => { + refValues.current.initialUrl = url; + }); + } let sub; if (!loading && !verify) { setTimeout(() => { sub = AppState.addEventListener("change", onAppStateChanged); - }, 1000); - (async () => { - try { - let url = await Linking.getInitialURL(); - if (url?.startsWith("https://app.notesnook.com/account/verified")) { - await onEmailVerified(); - } - await onUserUpdated(); - } catch (e) { - console.error(e); + if ( + refValues.current.initialUrl?.startsWith( + "https://app.notesnook.com/account/verified" + ) + ) { + onEmailVerified(); } - })(); + }, 1000); + onUserUpdated(); refValues.current.removeInternetStateListener = NetInfo.addEventListener( onInternetStateChanged ); @@ -223,7 +229,11 @@ export const useAppEvents = () => { try { if (url.startsWith("https://app.notesnook.com/account/verified")) { await onEmailVerified(); - } else { + } else if (url.startsWith("ShareMedia://QuickNoteWidget")) { + clearAppState(); + editorState().movedAway = false; + eSendEvent(eOnLoadNote, { type: "new" }); + tabBarRef.current?.goToPage(1, false); return; } } catch (e) { diff --git a/apps/mobile/app/hooks/use-shortcut-manager.ts b/apps/mobile/app/hooks/use-shortcut-manager.ts index a53d8d6d4..5f03c1f0f 100644 --- a/apps/mobile/app/hooks/use-shortcut-manager.ts +++ b/apps/mobile/app/hooks/use-shortcut-manager.ts @@ -21,6 +21,7 @@ import { useEffect } from "react"; import { NativeEventEmitter, NativeModule } from "react-native"; import { useRef } from "react"; import { Platform } from "react-native"; +import { Linking } from "react-native"; const ShortcutsEmitter = new NativeEventEmitter( Shortcuts as unknown as NativeModule ); @@ -46,6 +47,11 @@ export const useShortcutManager = ({ }, [shortcuts]); useEffect(() => { + Linking.getInitialURL().then((url) => { + if (url?.startsWith("ShareMedia://QuickNoteWidget")) { + onShortcutPressed(defaultShortcuts[0]); + } + }); Shortcuts.getInitialShortcut().then((shortcut) => { if (initialShortcutRecieved.current) return; onShortcutPressed(shortcut); diff --git a/apps/mobile/app/navigation/tabs-holder.js b/apps/mobile/app/navigation/tabs-holder.js index befb4341a..1fe530425 100644 --- a/apps/mobile/app/navigation/tabs-holder.js +++ b/apps/mobile/app/navigation/tabs-holder.js @@ -98,10 +98,12 @@ const _TabsHolder = () => { clearAppState(); if (!tabBarRef.current) { await sleep(3000); + eSendEvent(eOnLoadNote, { type: "new" }); editorState().movedAway = false; tabBarRef.current?.goToPage(1, false); return; } + eSendEvent(eOnLoadNote, { type: "new" }); editorState().movedAway = false; tabBarRef.current?.goToPage(1, false); } diff --git a/apps/mobile/native/ios/Notesnook/AppDelegate.mm b/apps/mobile/native/ios/Notesnook/AppDelegate.mm index e31b21d91..1947363b4 100644 --- a/apps/mobile/native/ios/Notesnook/AppDelegate.mm +++ b/apps/mobile/native/ios/Notesnook/AppDelegate.mm @@ -38,7 +38,6 @@ RCTBridge *bridge; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { RCTAppSetupPrepareApp(application); - shareViewController = [UIViewController new]; bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions]; #if RCT_NEW_ARCH_ENABLED @@ -49,43 +48,19 @@ RCTBridge *bridge; bridge.surfacePresenter = _bridgeAdapter.surfacePresenter; #endif - NSURL *url = (NSURL *) [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey]; - if (url != nil) { - navController = [[UINavigationController alloc] initWithRootViewController:shareViewController]; + rootViewController = [UIViewController new]; + NSDictionary *initProps = [self prepareInitialProps]; + UIView *rootView = RCTAppSetupDefaultRootView(bridge, @"Notesnook" , initProps); + + if (@available(iOS 13.0, *)) { + rootView.backgroundColor = [UIColor systemBackgroundColor]; } else { - rootViewController = [UIViewController new]; - navController = [[UINavigationController alloc] initWithRootViewController:rootViewController]; + rootView.backgroundColor = [UIColor whiteColor]; } - - - navController.navigationBarHidden = YES; - - if (url != nil) { - RCTRootView *shareView = [[RCTRootView alloc] initWithBridge:bridge - moduleName:@"QuickNoteIOS" - initialProperties:nil]; - if (@available(iOS 13.0, *)) { - shareView.backgroundColor = [UIColor systemBackgroundColor]; - } else { - shareView.backgroundColor = [UIColor whiteColor]; - } - shareViewController.view = shareView; - [RNBootSplash initWithStoryboard:@"BootSplash" rootView:shareView]; - } else { - NSDictionary *initProps = [self prepareInitialProps]; - UIView *rootView = RCTAppSetupDefaultRootView(bridge, @"Notesnook" , initProps); - - if (@available(iOS 13.0, *)) { - rootView.backgroundColor = [UIColor systemBackgroundColor]; - } else { - rootView.backgroundColor = [UIColor whiteColor]; - } - rootViewController.view = rootView; - [RNBootSplash initWithStoryboard:@"BootSplash" rootView:rootView]; - } - + rootViewController.view = rootView; + [RNBootSplash initWithStoryboard:@"BootSplash" rootView:rootView]; self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; - self.window.rootViewController = navController; + self.window.rootViewController = rootViewController; [self.window makeKeyAndVisible]; return YES; } @@ -119,44 +94,13 @@ RCTBridge *bridge; } - (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler { - [RNShortcuts performActionForShortcutItem:shortcutItem completionHandler:completionHandler]; + [RNShortcuts performActionForShortcutItem:shortcutItem completionHandler:completionHandler]; } - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary *)options { - - if ([url.absoluteString isEqual:@"ShareMedia://QuickNoteWidget"]) { - if (rootViewController != nil) { - - RCTRootView *shareView = [[RCTRootView alloc] initWithBridge:bridge - moduleName:@"QuickNoteIOS" - initialProperties:nil]; - shareView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1]; - shareViewController.view = shareView; - [navController pushViewController:shareViewController animated:false]; - } - - } - - if ([url.absoluteString isEqual:@"ShareMedia://MainApp"]) { - if (rootViewController == nil) { - UIApplication *app = [UIApplication sharedApplication]; - [app performSelector:@selector(suspend)]; - [NSThread sleepForTimeInterval:1.0]; - exit(0); - } else { - UIApplication *app = [UIApplication sharedApplication]; - [app performSelector:@selector(suspend)]; - [NSThread sleepForTimeInterval:0.5]; - [navController popToRootViewControllerAnimated:false]; - - } - - - } - return [RCTLinkingManager application:application openURL:url options:options]; }