From 320fc3fd9630731aabc9ecf30999afa7b74c277f Mon Sep 17 00:00:00 2001 From: Aman Harwara Date: Fri, 9 Sep 2022 13:32:23 +0530 Subject: [PATCH] feat(mobile): add setting to always open web view on launch (#1500) --- .../WebFrame/DeviceInterface.template.js | 3 +- packages/mobile/src/AppStack.tsx | 29 +++++++++++++++-- packages/mobile/src/Lib/constants.ts | 4 +-- packages/mobile/src/MobileWebAppContainer.tsx | 2 +- .../Settings/Sections/OptionsSection.tsx | 31 +++++++++++++++---- .../src/Domain/Device/Environments.ts | 1 + packages/snjs/lib/Application/Platforms.ts | 2 ++ .../ComponentManager/ComponentManager.ts | 8 +++-- 8 files changed, 64 insertions(+), 16 deletions(-) diff --git a/packages/mobile/WebFrame/DeviceInterface.template.js b/packages/mobile/WebFrame/DeviceInterface.template.js index 43adb537a..0e7429ed3 100644 --- a/packages/mobile/WebFrame/DeviceInterface.template.js +++ b/packages/mobile/WebFrame/DeviceInterface.template.js @@ -2,11 +2,12 @@ class WebProcessDeviceInterface { constructor(messageSender) { this.appVersion = '1.2.3' - this.environment = 1 + this.environment = 4 this.databases = [] this.messageSender = messageSender } + // eslint-disable-next-line @typescript-eslint/no-empty-function setApplication() {} sendMessage(functionName, args) { diff --git a/packages/mobile/src/AppStack.tsx b/packages/mobile/src/AppStack.tsx index 8bc2bce96..d1abe60a6 100644 --- a/packages/mobile/src/AppStack.tsx +++ b/packages/mobile/src/AppStack.tsx @@ -1,18 +1,19 @@ import { AppStateEventType, AppStateType, TabletModeChangeData } from '@Lib/ApplicationState' +import { AlwaysOpenWebAppOnLaunchKey } from '@Lib/constants' import { useHasEditor, useIsLocked } from '@Lib/SnjsHelperHooks' import { ScreenStatus } from '@Lib/StatusManager' import { IsDev } from '@Lib/Utils' -import { CompositeNavigationProp, RouteProp } from '@react-navigation/native' +import { CompositeNavigationProp, RouteProp, useNavigation } from '@react-navigation/native' import { createStackNavigator, StackNavigationProp } from '@react-navigation/stack' import { HeaderTitleView } from '@Root/Components/HeaderTitleView' import { IoniconsHeaderButton } from '@Root/Components/IoniconsHeaderButton' import { Compose } from '@Root/Screens/Compose/Compose' -import { SCREEN_COMPOSE, SCREEN_NOTES, SCREEN_VIEW_PROTECTED_NOTE } from '@Root/Screens/screens' +import { SCREEN_COMPOSE, SCREEN_NOTES, SCREEN_VIEW_PROTECTED_NOTE, SCREEN_WEB_APP } from '@Root/Screens/screens' import { MainSideMenu } from '@Root/Screens/SideMenu/MainSideMenu' import { NoteSideMenu } from '@Root/Screens/SideMenu/NoteSideMenu' import { ViewProtectedNote } from '@Root/Screens/ViewProtectedNote/ViewProtectedNote' import { Root } from '@Screens/Root' -import { UuidString } from '@standardnotes/snjs' +import { ApplicationEvent, StorageValueModes, UuidString } from '@standardnotes/snjs' import { ICON_MENU } from '@Style/Icons' import { ThemeService } from '@Style/ThemeService' import { getDefaultDrawerWidth } from '@Style/Utils' @@ -123,6 +124,28 @@ export const AppStackComponent = (props: ModalStackNavigationProp<'AppStack'>) = [application], ) + const navigation = useNavigation['navigation']>() + + useEffect(() => { + if (!application) { + return + } + + const removeObserver = application.addEventObserver(async (event) => { + if (event === ApplicationEvent.Launched) { + const value = (await application.getValue(AlwaysOpenWebAppOnLaunchKey, StorageValueModes.Nonwrapped)) as + | boolean + | undefined + const shouldAlwaysOpenWebAppOnLaunch = value ?? false + if (shouldAlwaysOpenWebAppOnLaunch) { + navigation.push(SCREEN_WEB_APP) + } + } + }) + + return removeObserver + }, [application, navigation]) + return ( { class WebProcessDeviceInterface { constructor(messageSender) { this.appVersion = '1.2.3' - this.environment = 1 + this.environment = 4 this.databases = [] this.messageSender = messageSender } diff --git a/packages/mobile/src/Screens/Settings/Sections/OptionsSection.tsx b/packages/mobile/src/Screens/Settings/Sections/OptionsSection.tsx index c8d99b5da..b84236073 100644 --- a/packages/mobile/src/Screens/Settings/Sections/OptionsSection.tsx +++ b/packages/mobile/src/Screens/Settings/Sections/OptionsSection.tsx @@ -1,4 +1,4 @@ -import { WebAppOptionEnabled } from '@Lib/constants' +import { AlwaysOpenWebAppOnLaunchKey } from '@Lib/constants' import { useSignedIn } from '@Lib/SnjsHelperHooks' import { useNavigation } from '@react-navigation/native' import { ButtonCell } from '@Root/Components/ButtonCell' @@ -9,9 +9,9 @@ import { TableSection } from '@Root/Components/TableSection' import { useSafeApplicationContext } from '@Root/Hooks/useSafeApplicationContext' import { ModalStackNavigationProp } from '@Root/ModalStack' import { SCREEN_MANAGE_SESSIONS, SCREEN_SETTINGS, SCREEN_WEB_APP } from '@Root/Screens/screens' -import { ButtonType, PrefKey } from '@standardnotes/snjs' +import { ButtonType, PrefKey, StorageValueModes } from '@standardnotes/snjs' import moment from 'moment' -import React, { useCallback, useMemo, useState } from 'react' +import React, { useCallback, useEffect, useMemo, useState } from 'react' import { Platform } from 'react-native' import DocumentPicker from 'react-native-document-picker' import RNFS from 'react-native-fs' @@ -183,6 +183,18 @@ export const OptionsSection = ({ title, encryptionAvailable }: Props) => { ) }, [application.alertService]) + const [shouldAlwaysOpenWebAppOnLaunch, setShouldAlwaysOpenWebAppOnLaunch] = useState(false) + + useEffect(() => { + const getSetting = async () => { + const value = (await application.getValue(AlwaysOpenWebAppOnLaunchKey, StorageValueModes.Nonwrapped)) as + | boolean + | undefined + setShouldAlwaysOpenWebAppOnLaunch(value ?? false) + } + void getSetting() + }, [application]) + const openWebApp = useCallback(() => { navigation.push(SCREEN_WEB_APP) }, [navigation]) @@ -226,9 +238,16 @@ export const OptionsSection = ({ title, encryptionAvailable }: Props) => { onPress={onExportPress} /> - {WebAppOptionEnabled && ( - openWebApp()} /> - )} + openWebApp()} /> + { + const newValue = !shouldAlwaysOpenWebAppOnLaunch + setShouldAlwaysOpenWebAppOnLaunch(newValue) + void application.setValue(AlwaysOpenWebAppOnLaunchKey, newValue, StorageValueModes.Nonwrapped) + }} + text="Always Open Web App On Launch" + selected={() => shouldAlwaysOpenWebAppOnLaunch} + /> {!signedIn && (