Skip to content

Commit

Permalink
Bugfix: Native stack navigation related issues (#361)
Browse files Browse the repository at this point in the history
* fix navigation / camera issues

* clean up

* more clean up

* rename param

* fix android

* fix naming
  • Loading branch information
brunobar79 authored Feb 13, 2020
1 parent 31800fa commit 1d5a130
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 24 deletions.
14 changes: 8 additions & 6 deletions src/screens/CurrencySelectModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export const CurrencySelectionTypes = {

class CurrencySelectModal extends Component {
static propTypes = {
isFocused: PropTypes.bool,
navigation: PropTypes.object,
sortedUniswapAssets: PropTypes.array,
transitionPosition: PropTypes.object,
Expand All @@ -67,11 +66,14 @@ class CurrencySelectModal extends Component {
const currentAssetsUniqueId = buildUniqueIdForListData(currentAssets);
const nextAssetsUniqueId = buildUniqueIdForListData(nextAssets);
const isNewAssets = currentAssetsUniqueId !== nextAssetsUniqueId;
const isFocused = this.props.navigation.getParam('focused', false);
const willBeFocused = nextProps.navigation.getParam('focused', false);

const isNewProps = isNewValueForObjectPaths(this.props, nextProps, [
'isFocused',
'type',
]);
const isNewProps = isNewValueForObjectPaths(
{ ...this.props, isFocused },
{ ...nextProps, isFocused: willBeFocused },
['isFocused', 'type']
);

const isNewState = isNewValueForObjectPaths(this.state, nextState, [
'searchQuery',
Expand Down Expand Up @@ -142,7 +144,6 @@ class CurrencySelectModal extends Component {
render = () => {
const {
uniswapAssetsInWallet,
isFocused,
sortedUniswapAssets,
transitionPosition,
type,
Expand All @@ -164,6 +165,7 @@ class CurrencySelectModal extends Component {
}

const listItems = filterList(assets, searchQuery, 'uniqueId');
const isFocused = this.props.navigation.getParam('focused', false);

return (
<KeyboardFixedOpenLayout>
Expand Down
35 changes: 21 additions & 14 deletions src/screens/QRScannerScreenWithData.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@ class QRScannerScreenWithData extends Component {
};

componentDidUpdate = (prevProps, prevState) => {
if (this.props.isFocused && !prevProps.isFocused) {
const wasFocused = prevProps.navigation.getParam('focused', false);
const isFocused = this.props.navigation.getParam('focused', false);

if (isFocused && !wasFocused) {
Permissions.request('camera').then(permission => {
const isCameraAuthorized = permission === 'authorized';

if (prevState.isCameraAuthorized !== isCameraAuthorized) {
this.setState({ isCameraAuthorized });
}
});

// eslint-disable-next-line react/no-did-update-set-state
this.setState({ enableScanning: true });
!this.state.enableScanning && this.setState({ enableScanning: true });
}
};

Expand Down Expand Up @@ -103,17 +105,22 @@ class QRScannerScreenWithData extends Component {
});
};

render = () => (
<QRScannerScreen
{...this.props}
{...this.state}
enableScanning={this.state.enableScanning && this.props.isFocused}
onPressBackButton={this.handlePressBackButton}
onPressPasteSessionUri={this.handlePressPasteSessionUri}
onScanSuccess={this.handleScanSuccess}
onSheetLayout={this.handleSheetLayout}
/>
);
render = () => {
const isFocused = this.props.navigation.getParam('focused', false);

return (
<QRScannerScreen
{...this.props}
{...this.state}
isFocused={isFocused}
enableScanning={this.state.enableScanning && isFocused}
onPressBackButton={this.handlePressBackButton}
onPressPasteSessionUri={this.handlePressPasteSessionUri}
onScanSuccess={this.handleScanSuccess}
onSheetLayout={this.handleSheetLayout}
/>
);
};
}

export default compose(
Expand Down
43 changes: 41 additions & 2 deletions src/screens/Routes/Routes.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import analytics from '@segment/analytics-react-native';
import { get, omit } from 'lodash';
import React from 'react';
import { StatusBar } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createAppContainer, NavigationActions } from 'react-navigation';
import { createMaterialTopTabNavigator } from 'react-navigation-tabs';
import ViewPagerAdapter from 'react-native-tab-view-viewpager-adapter';
// eslint-disable-next-line import/no-unresolved
Expand Down Expand Up @@ -219,13 +219,52 @@ const AppContainerWithAnalytics = React.forwardRef((props, ref) => (
const { params, routeName } = Navigation.getActiveRoute(currentState);
const prevRouteName = Navigation.getActiveRouteName(prevState);
// native stack rn does not support onTransitionEnd and onTransitionStart
// Set focus manually on route changes
if (prevRouteName !== routeName) {
Navigation.handleAction(
NavigationActions.setParams({
key: routeName,
params: { focused: true },
})
);

Navigation.handleAction(
NavigationActions.setParams({
key: prevRouteName,
params: { focused: false },
})
);
}

if (
prevRouteName !== 'QRScannerScreen' &&
routeName === 'QRScannerScreen'
) {
StatusBar.setBarStyle('light-content');
}

if (
prevRouteName === 'QRScannerScreen' &&
routeName !== 'QRScannerScreen'
) {
StatusBar.setBarStyle('dark-content');
}

if (
prevRouteName === 'ImportSeedPhraseSheet' &&
(routeName === 'ProfileScreen' || routeName === 'WalletScreen')
) {
StatusBar.setBarStyle('dark-content');
}

if (prevRouteName === 'WalletScreen' && routeName === 'SendSheet') {
StatusBar.setBarStyle('light-content');
}

if (prevRouteName === 'SendSheet' && routeName === 'WalletScreen') {
StatusBar.setBarStyle('dark-content');
}

if (routeName === 'SettingsModal') {
let subRoute = get(params, 'section.title');
if (subRoute === 'Settings') subRoute = null;
Expand All @@ -235,7 +274,7 @@ const AppContainerWithAnalytics = React.forwardRef((props, ref) => (
}

if (routeName !== prevRouteName) {
let paramsToTrack = {};
let paramsToTrack = null;

if (
prevRouteName === 'MainExchangeScreen' &&
Expand Down
33 changes: 32 additions & 1 deletion src/screens/Routes/Routes.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import analytics from '@segment/analytics-react-native';
import { get, omit } from 'lodash';
import React from 'react';
import { StatusBar } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { createAppContainer, NavigationActions } from 'react-navigation';
import { createMaterialTopTabNavigator } from 'react-navigation-tabs-v1';
// eslint-disable-next-line import/no-unresolved
import { enableScreens } from 'react-native-screens';
Expand Down Expand Up @@ -261,6 +261,37 @@ const AppContainerWithAnalytics = React.forwardRef((props, ref) => (
const { params, routeName } = Navigation.getActiveRoute(currentState);
const prevRouteName = Navigation.getActiveRouteName(prevState);
// native stack rn does not support onTransitionEnd and onTransitionStart
// Set focus manually on route changes
if (prevRouteName !== routeName) {
Navigation.handleAction(
NavigationActions.setParams({
key: routeName,
params: { focused: true },
})
);

Navigation.handleAction(
NavigationActions.setParams({
key: prevRouteName,
params: { focused: false },
})
);
}

if (
prevRouteName !== 'QRScannerScreen' &&
routeName === 'QRScannerScreen'
) {
StatusBar.setBarStyle('light-content');
}

if (
prevRouteName === 'QRScannerScreen' &&
routeName !== 'QRScannerScreen'
) {
StatusBar.setBarStyle('dark-content');
}

if (
prevRouteName === 'ImportSeedPhraseSheet' &&
(routeName === 'ProfileScreen' || routeName === 'WalletScreen')
Expand Down
3 changes: 2 additions & 1 deletion src/screens/WalletScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class WalletScreen extends Component {
try {
await this.props.initializeWallet();
const keyboardheight = await getKeyboardHeight();

if (keyboardheight) {
this.props.setKeyboardHeight(keyboardheight);
}
Expand All @@ -61,7 +62,7 @@ class WalletScreen extends Component {
};

shouldComponentUpdate = nextProps =>
nextProps.navigation.isFocused() &&
nextProps.navigation.getParam('focused', true) &&
isNewValueForObjectPaths(this.props, nextProps, [
'fetchingAssets',
'fetchingUniqueTokens',
Expand Down

0 comments on commit 1d5a130

Please sign in to comment.