Skip to content

Commit

Permalink
moved global back handling to App.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
myxmaster committed May 6, 2024
1 parent 3d60445 commit 86a26bf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
38 changes: 38 additions & 0 deletions App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,31 @@ import CustodialWalletWarning from './views/Settings/CustodialWalletWarning';

import PSBT from './views/PSBT';
import TxHex from './views/TxHex';
import { BackHandler, NativeEventSubscription } from 'react-native';

export default class App extends React.PureComponent {
private backPressListenerSubscription: NativeEventSubscription;

private handleBackPress = (navigation: any) => {
const dialogHasBeenClosed = Stores.modalStore.closeVisibleModalDialog();
if (dialogHasBeenClosed) {
return true;
}

if (Stores.settingsStore.loginRequired()) {
BackHandler.exitApp();
return true;
}

const navigationState = navigation.getState();
if (navigationState.routes.length > 1) {
navigation.pop();
return true;
}

return false;
};

render() {
const Stack = createStackNavigator();
return (
Expand Down Expand Up @@ -190,6 +213,21 @@ export default class App extends React.PureComponent {
headerShown: false,
animationEnabled: false
}}
screenListeners={({ navigation }) => ({
focus: () => {
this.backPressListenerSubscription?.remove();
this.backPressListenerSubscription =
BackHandler.addEventListener(
'hardwareBackPress',
() =>
this.handleBackPress(
navigation
)
);
},
blur: () =>
this.backPressListenerSubscription?.remove()
})}
>
<Stack.Screen
name="Wallet"
Expand Down
36 changes: 11 additions & 25 deletions views/Wallet/Wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,6 @@ export default class Wallet extends React.Component<WalletProps, WalletState> {
}

private handleBackButton() {
const dialogHasBeenClosed =
this.props.ModalStore.closeVisibleModalDialog();
if (dialogHasBeenClosed) {
return true;
}

const navigation = this.props.navigation;

if (this.props.SettingsStore.loginRequired()) {
// popToTop to close all screens and return false to close the app
navigation.popToTop();
return false;
}

const navigationState = navigation.getState();
if (navigationState.routes.length > 1) {
navigation.pop();
return true;
}

const tabNavigator = this.tabNavigationRef.current;
if (!tabNavigator) {
return false;
Expand All @@ -197,20 +177,26 @@ export default class Wallet extends React.Component<WalletProps, WalletState> {
return false;
}

handleFocus = () => this.getSettingsAndNavigate();
private handleFocus = () => {
this.backPressSubscription?.remove();
this.backPressSubscription = BackHandler.addEventListener(
'hardwareBackPress',
this.handleBackButton.bind(this)
);
this.getSettingsAndNavigate();
};

private handleBlur = () => this.backPressSubscription?.remove();

async componentDidMount() {
// triggers when loaded from navigation or back action
this.props.navigation.addListener('focus', this.handleFocus);
this.props.navigation.addListener('blur', this.handleBlur);

this.handleAppStateChangeSubscription = AppState.addEventListener(
'change',
this.handleAppStateChange
);
this.backPressSubscription = BackHandler.addEventListener(
'hardwareBackPress',
this.handleBackButton.bind(this)
);
}

componentWillUnmount() {
Expand Down

0 comments on commit 86a26bf

Please sign in to comment.