-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43500 from software-mansion-labs/fix/android-sear…
…ch-ws [CP Staging] [Search v1] Fix for workspace switcher and back to search tab on Android
- Loading branch information
Showing
9 changed files
with
97 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/libs/Navigation/setupCustomAndroidBackHandler/index.android.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import {findFocusedRoute, StackActions} from '@react-navigation/native'; | ||
import type {StackScreenProps} from '@react-navigation/stack'; | ||
import {BackHandler} from 'react-native'; | ||
import getTopmostCentralPaneRoute from '@navigation/getTopmostCentralPaneRoute'; | ||
import navigationRef from '@navigation/navigationRef'; | ||
import type {BottomTabNavigatorParamList, RootStackParamList, State} from '@navigation/types'; | ||
import NAVIGATORS from '@src/NAVIGATORS'; | ||
import SCREENS from '@src/SCREENS'; | ||
|
||
type SearchPageProps = StackScreenProps<BottomTabNavigatorParamList, typeof SCREENS.SEARCH.BOTTOM_TAB>; | ||
|
||
// We need to do some custom handling for the back button on Android for actions related to the search page. | ||
function setupCustomAndroidBackHandler() { | ||
const onBackPress = () => { | ||
const rootState = navigationRef.getRootState(); | ||
|
||
const bottomTabRoute = rootState.routes.find((route) => route.name === NAVIGATORS.BOTTOM_TAB_NAVIGATOR); | ||
const bottomTabRoutes = bottomTabRoute?.state?.routes; | ||
const focusedRoute = findFocusedRoute(rootState); | ||
|
||
// Shoudn't happen but for type safety. | ||
if (!bottomTabRoutes) { | ||
return false; | ||
} | ||
|
||
// Handle back press on the search page. | ||
// We need to pop two screens, from the central pane and from the bottom tab. | ||
if (bottomTabRoutes[bottomTabRoutes.length - 1].name === SCREENS.SEARCH.BOTTOM_TAB && focusedRoute?.name === SCREENS.SEARCH.CENTRAL_PANE) { | ||
navigationRef.dispatch({...StackActions.pop(), target: bottomTabRoute?.state?.key}); | ||
navigationRef.dispatch({...StackActions.pop()}); | ||
|
||
const centralPaneRouteAfterPop = getTopmostCentralPaneRoute({routes: [rootState.routes.at(-2)]} as State<RootStackParamList>); | ||
const bottomTabRouteAfterPop = bottomTabRoutes.at(-2); | ||
|
||
// It's possible that central pane search is desynchronized with the bottom tab search. | ||
// e.g. opening a tab different than search will wipe out central pane screens. | ||
// In that case we have to push the proper one. | ||
if ( | ||
bottomTabRouteAfterPop && | ||
bottomTabRouteAfterPop.name === SCREENS.SEARCH.BOTTOM_TAB && | ||
(!centralPaneRouteAfterPop || centralPaneRouteAfterPop.name !== SCREENS.SEARCH.CENTRAL_PANE) | ||
) { | ||
const {policyID, ...restParams} = bottomTabRoutes[bottomTabRoutes.length - 2].params as SearchPageProps['route']['params']; | ||
navigationRef.dispatch({...StackActions.push(NAVIGATORS.CENTRAL_PANE_NAVIGATOR, {screen: SCREENS.SEARCH.CENTRAL_PANE, params: {...restParams, policyIDs: policyID}})}); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
// Handle back press to go back to the search page. | ||
// It's possible that central pane search is desynchronized with the bottom tab search. | ||
// e.g. opening a tab different than search will wipe out central pane screens. | ||
// In that case we have to push the proper one. | ||
if (bottomTabRoutes && bottomTabRoutes?.length >= 2 && bottomTabRoutes[bottomTabRoutes.length - 2].name === SCREENS.SEARCH.BOTTOM_TAB && rootState.routes.length === 1) { | ||
const {policyID, ...restParams} = bottomTabRoutes[bottomTabRoutes.length - 2].params as SearchPageProps['route']['params']; | ||
navigationRef.dispatch({...StackActions.push(NAVIGATORS.CENTRAL_PANE_NAVIGATOR, {screen: SCREENS.SEARCH.CENTRAL_PANE, params: {...restParams, policyIDs: policyID}})}); | ||
navigationRef.dispatch({...StackActions.pop(), target: bottomTabRoute?.state?.key}); | ||
return true; | ||
} | ||
|
||
// Handle all other cases with default handler. | ||
return false; | ||
}; | ||
|
||
BackHandler.addEventListener('hardwareBackPress', onBackPress); | ||
} | ||
|
||
export default setupCustomAndroidBackHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Do nothing for platforms different than Android. | ||
function setupCustomAndroidBackHandler() {} | ||
|
||
export default setupCustomAndroidBackHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.