Skip to content

Commit

Permalink
update getPartialStateDiff.ts fullscreen diff
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedGaber93 committed Apr 2, 2024
1 parent 882af82 commit 220d866
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/libs/Navigation/AppNavigator/getPartialStateDiff.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import getTopmostBottomTabRoute from '@libs/Navigation/getTopmostBottomTabRoute';
import getTopmostCentralPaneRoute from '@libs/Navigation/getTopmostCentralPaneRoute';
import getTopmostFullScreenRoute from '@libs/Navigation/getTopmostFullScreenRoute';
import type {Metainfo} from '@libs/Navigation/linkingConfig/getAdaptedStateFromPath';
import type {NavigationPartialRoute, RootStackParamList, State} from '@libs/Navigation/types';
import NAVIGATORS from '@src/NAVIGATORS';
Expand Down Expand Up @@ -73,10 +74,19 @@ function getPartialStateDiff(state: State<RootStackParamList>, templateState: St
// This one is heuristic and may need to be improved if we will be able to navigate from modal screen with full screen in background to another modal screen with full screen in background.
// For now this simple check is enough.
if (metainfo.isFullScreenNavigatorMandatory) {
const stateTopmostFullScreen = state.routes.filter((route) => route.name === NAVIGATORS.FULL_SCREEN_NAVIGATOR).at(-1);
const templateStateTopmostFullScreen = templateState.routes.filter((route) => route.name === NAVIGATORS.FULL_SCREEN_NAVIGATOR).at(-1) as NavigationPartialRoute;
if (!stateTopmostFullScreen && templateStateTopmostFullScreen) {
diff[NAVIGATORS.FULL_SCREEN_NAVIGATOR] = templateStateTopmostFullScreen;
const stateTopmostFullScreen = getTopmostFullScreenRoute(state);
const templateStateTopmostFullScreen = getTopmostFullScreenRoute(templateState);
const fullScreenDiff = templateState.routes.filter((route) => route.name === NAVIGATORS.FULL_SCREEN_NAVIGATOR).at(-1) as NavigationPartialRoute;

if (
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
(!stateTopmostFullScreen && templateStateTopmostFullScreen) ||
(stateTopmostFullScreen &&
templateStateTopmostFullScreen &&
stateTopmostFullScreen.name !== templateStateTopmostFullScreen.name &&
!shallowCompare(stateTopmostFullScreen.params, templateStateTopmostFullScreen.params))
) {
diff[NAVIGATORS.FULL_SCREEN_NAVIGATOR] = fullScreenDiff;
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/libs/Navigation/getTopmostFullScreenRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import NAVIGATORS from '@src/NAVIGATORS';
import type {FullScreenName, NavigationPartialRoute, RootStackParamList, State} from './types';

// Get the name of topmost fullscreen route in the navigation stack.
function getTopmostFullScreenRoute(state: State<RootStackParamList>): NavigationPartialRoute<FullScreenName> | undefined {
if (!state) {
return;
}

const topmostFullScreenRoute = state.routes.filter((route) => route.name === NAVIGATORS.FULL_SCREEN_NAVIGATOR).at(-1);

if (!topmostFullScreenRoute) {
return;
}

if (!!topmostFullScreenRoute.params && 'screen' in topmostFullScreenRoute.params) {
return {name: topmostFullScreenRoute.params.screen as FullScreenName, params: topmostFullScreenRoute.params.params};
}

if (!topmostFullScreenRoute.state) {
return;
}

// There will be at least one route in the fullscreen navigator.
const {name, params} = topmostFullScreenRoute.state.routes.at(-1) as NavigationPartialRoute<FullScreenName>;

return {name, params};
}

export default getTopmostFullScreenRoute;

0 comments on commit 220d866

Please sign in to comment.