diff --git a/ts/navigation/AppStackNavigator.tsx b/ts/navigation/AppStackNavigator.tsx index b471a8cd648..35c0e5be766 100644 --- a/ts/navigation/AppStackNavigator.tsx +++ b/ts/navigation/AppStackNavigator.tsx @@ -1,5 +1,9 @@ /* eslint-disable functional/immutable-data */ -import { LinkingOptions, NavigationContainer } from "@react-navigation/native"; +import { + LinkingOptions, + NavigationContainer, + NavigationContainerProps +} from "@react-navigation/native"; import * as React from "react"; import { useRef } from "react"; import { View } from "react-native"; @@ -38,10 +42,22 @@ import { useStoredExperimentalDesign } from "../common/context/DSExperimentalCon import { IONavigationLightTheme } from "../theme/navigations"; import { MESSAGES_ROUTES } from "../features/messages/navigation/routes"; import AuthenticatedStackNavigator from "./AuthenticatedStackNavigator"; -import NavigationService, { navigationRef } from "./NavigationService"; +import NavigationService, { + navigationRef, + setMainNavigatorReady +} from "./NavigationService"; import NotAuthenticatedStackNavigator from "./NotAuthenticatedStackNavigator"; import ROUTES from "./routes"; +type OnStateChangeStateType = Parameters< + NonNullable +>[0]; +const isMainNavigatorReady = (state: OnStateChangeStateType) => + state && + state.routes && + state.routes.length > 0 && + state.routes[0].name === ROUTES.MAIN; + export const AppStackNavigator = (): React.ReactElement => { // This hook is used since we are in a child of the Context Provider // to setup the experimental design system value from AsyncStorage @@ -153,7 +169,10 @@ const InnerNavigationContainer = (props: { children: React.ReactElement }) => { NavigationService.setNavigationReady(); routeNameRef.current = navigationRef.current?.getCurrentRoute()?.name; }} - onStateChange={async () => { + onStateChange={async state => { + if (isMainNavigatorReady(state)) { + setMainNavigatorReady(); + } const previousRouteName = routeNameRef.current; const currentRouteName = navigationRef.current?.getCurrentRoute()?.name; if (currentRouteName !== undefined) { diff --git a/ts/navigation/NavigationService.ts b/ts/navigation/NavigationService.ts index 31b2bc4d218..3679e48dd66 100644 --- a/ts/navigation/NavigationService.ts +++ b/ts/navigation/NavigationService.ts @@ -10,6 +10,13 @@ export const navigationRef = React.createRef(); // eslint-disable-next-line functional/no-let let isNavigationReady: boolean = false; +// eslint-disable-next-line functional/no-let +let isMainNavigatorReady = false; + +export const setMainNavigatorReady = () => { + isMainNavigatorReady = true; +}; + export const setNavigationReady = () => { // eslint-disable-next-line functional/immutable-data isNavigationReady = true; @@ -44,6 +51,8 @@ const dispatchNavigationAction = (action: NavigationAction) => { navigationRef.current?.dispatch(action); }; +const getIsMainNavigatorReady = () => isMainNavigatorReady; + const getCurrentRouteName = (): string | undefined => navigationRef.current?.getCurrentRoute()?.name; @@ -66,5 +75,6 @@ export default { getCurrentRoute, getCurrentState, getIsNavigationReady, - setNavigationReady + setNavigationReady, + getIsMainNavigatorReady }; diff --git a/ts/sagas/startup.ts b/ts/sagas/startup.ts index 652e9a5e3c6..e8d0594bb0d 100644 --- a/ts/sagas/startup.ts +++ b/ts/sagas/startup.ts @@ -426,13 +426,11 @@ export function* initializeApplicationSaga( const watchAbortOnboardingSagaTask = yield* fork(watchAbortOnboardingSaga); yield* put(startupLoadSuccess(StartupStatusEnum.ONBOARDING)); - // FIXME IOPID-1298: find any better way to handle this - // We need this workaround to let the inner AppStackNavigator stack be ready, - // before continuing with any other navigation action to avoid: - // Error: The 'navigation' object hasn't been initialized yet... - // Here the navigationRef is ready, but because we changed the navigation inner stack - // based on StartupStatusEnum value, we need to wait for the new stack to be ready. - yield* delay(0 as Millisecond); + if (!handleSessionExpiration) { + yield* call(waitForMainNavigator); + } + + // yield* delay(0 as Millisecond); const hasPreviousSessionAndPin = previousSessionToken && O.isSome(maybeStoredPin); if (hasPreviousSessionAndPin && showIdentificationModal) { @@ -517,9 +515,6 @@ export function* initializeApplicationSaga( yield* call(updateInstallationSaga, backendClient.createOrUpdateInstallation); yield* put(startupLoadSuccess(StartupStatusEnum.AUTHENTICATED)); - // FIXME IOPID-1298: find any better way to handle this - // As above for StartupStatusEnum.ONBOARDING - yield* delay(0 as Millisecond); // // User is autenticated, session token is valid // @@ -695,6 +690,33 @@ function* waitForNavigatorServiceInitialization() { }); } +function* waitForMainNavigator() { + // eslint-disable-next-line functional/no-let + let isMainNavReady = yield* call(NavigationService.getIsMainNavigatorReady); + + // eslint-disable-next-line functional/no-let + let timeoutLogged = false; + const startTime = performance.now(); + + // before continuing we must wait for the main navigator tack to be ready + while (!isMainNavReady) { + const elapsedTime = performance.now() - startTime; + if (!timeoutLogged && elapsedTime >= warningWaitNavigatorTime) { + timeoutLogged = true; + + yield* call(mixpanelTrack, "MAIN_NAVIGATOR_STACK_READY_TIMEOUT"); + } + yield* delay(navigatorPollingTime); + isMainNavReady = yield* call(NavigationService.getIsMainNavigatorReady); + } + + const initTime = performance.now() - startTime; + + yield* call(mixpanelTrack, "MAIN_NAVIGATOR_STACK_READY_OK", { + elapsedTime: initTime + }); +} + /** * Remove all the local notifications related to authentication with spid. *