Skip to content

Commit

Permalink
Merge branch 'master' into IOBP-316-resume-start-point-after-payment
Browse files Browse the repository at this point in the history
  • Loading branch information
Hantex9 authored Jan 24, 2024
2 parents 5ca24f7 + 71d6507 commit e963a35
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 14 deletions.
25 changes: 22 additions & 3 deletions ts/navigation/AppStackNavigator.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<NavigationContainerProps["onStateChange"]>
>[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
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 11 additions & 1 deletion ts/navigation/NavigationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ export const navigationRef = React.createRef<NavigationContainerRef>();
// 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;
Expand Down Expand Up @@ -44,6 +51,8 @@ const dispatchNavigationAction = (action: NavigationAction) => {
navigationRef.current?.dispatch(action);
};

const getIsMainNavigatorReady = () => isMainNavigatorReady;

const getCurrentRouteName = (): string | undefined =>
navigationRef.current?.getCurrentRoute()?.name;

Expand All @@ -66,5 +75,6 @@ export default {
getCurrentRoute,
getCurrentState,
getIsNavigationReady,
setNavigationReady
setNavigationReady,
getIsMainNavigatorReady
};
42 changes: 32 additions & 10 deletions ts/sagas/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
//
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit e963a35

Please sign in to comment.