Skip to content

Feat/panning fixes #483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions src/components/panningViews/panDismissibleView.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import PanningProvider from './panningProvider';
const DEFAULT_SPEED = 20;
const DEFAULT_BOUNCINESS = 6;
const DEFAULT_DISMISS_ANIMATION_DURATION = 280;
const DEFAULT_MAXIMUM_DRAGS_AFTER_SWIPE = 1;

/**
* @description: PanDismissibleView component created to making listening to swipe and drag events easier,
Expand Down Expand Up @@ -37,6 +38,10 @@ class PanDismissibleView extends PureComponent {
bounciness: PropTypes.number,
duration: PropTypes.number,
}),
/**
* The maximum number of swipes that can occur after a swipe before resetting the swipe (default is 1)
*/
maximumDragsAfterSwipe: PropTypes.number,
};

static defaultProps = {
Expand All @@ -51,6 +56,7 @@ class PanDismissibleView extends PureComponent {
bounciness: DEFAULT_BOUNCINESS,
duration: DEFAULT_DISMISS_ANIMATION_DURATION,
},
maximumDragsAfterSwipe: DEFAULT_MAXIMUM_DRAGS_AFTER_SWIPE,
onDismiss: _.noop,
};

Expand All @@ -61,6 +67,7 @@ class PanDismissibleView extends PureComponent {
animTranslateX: new Animated.Value(0),
animTranslateY: new Animated.Value(0),
isAnimating: false,
forceDismissIsPending: false
};
this.ref = React.createRef();
}
Expand Down Expand Up @@ -110,22 +117,32 @@ class PanDismissibleView extends PureComponent {
}
};

initPositions = () => {
initPositions = (extraDataForSetState, runAfterSetState) => {
this.setNativeProps(0, 0);
this.setState({
animTranslateX: new Animated.Value(0),
animTranslateY: new Animated.Value(0),
});
...extraDataForSetState
}, runAfterSetState);
};

onPanStart = () => {
this.swipe = {};
this.counter = 0;
};

onDrag = deltas => {
const {maximumDragsAfterSwipe} = this.props;
const left = deltas.x ? Math.round(deltas.x) : 0;
const top = deltas.y ? Math.round(deltas.y) : 0;
this.setNativeProps(left, top);
if (this.swipe.x || this.swipe.y) {
if (this.counter < maximumDragsAfterSwipe) {
this.counter += 1;
} else {
this.swipe = {};
}
}
};

setNativeProps = (left, top) => {
Expand Down Expand Up @@ -193,8 +210,9 @@ class PanDismissibleView extends PureComponent {
};

onInitAnimationFinished = () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change animateToInitialPosition to resetPosition
change onInitAnimationFinished to onResetPositionFinished
forceDismissIsPending change to shouldDismissAfterReset

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

this.setState({isAnimating: false});
this.initPositions();
const {forceDismissIsPending} = this.state;
const runAfterSetState = forceDismissIsPending ? this.animateDismiss : undefined;
this.initPositions({isAnimating: false, forceDismissIsPending: false}, runAfterSetState);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need isAnimating in state

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need it in the state (but I removed shouldDismissAfterReset from the state)

};

getDismissAnimationDirection = () => {
Expand Down Expand Up @@ -232,14 +250,19 @@ class PanDismissibleView extends PureComponent {
// isDown === true --> animate to the bottom
// isDown === false --> animate to the top
animateDismiss = () => {
const {directions = []} = this.props;
const hasUp = directions.includes(PanningProvider.Directions.UP);
const hasRight = directions.includes(PanningProvider.Directions.RIGHT);
const hasLeft = directions.includes(PanningProvider.Directions.LEFT);
const hasDown = !hasUp && !hasLeft && !hasRight; // default
const verticalDismiss = hasDown ? true : hasUp ? false : undefined;
const horizontalDismiss = hasRight ? true : hasLeft ? false : undefined;
this._animateDismiss(horizontalDismiss, verticalDismiss);
const {isAnimating} = this.state;
if (isAnimating) {
this.setState({forceDismissIsPending: true});
} else {
const {directions = []} = this.props;
const hasUp = directions.includes(PanningProvider.Directions.UP);
const hasRight = directions.includes(PanningProvider.Directions.RIGHT);
const hasLeft = directions.includes(PanningProvider.Directions.LEFT);
const hasDown = !hasUp && !hasLeft && !hasRight; // default
const verticalDismiss = hasDown ? true : hasUp ? false : undefined;
const horizontalDismiss = hasRight ? true : hasLeft ? false : undefined;
this._animateDismiss(horizontalDismiss, verticalDismiss);
}
};

_animateDismiss = (isRight, isDown) => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/panningViews/panningProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ export default class PanningProvider extends Component {
};

onDrag = ({directions, deltas}) => {
this.setState({dragDirections: directions, dragDeltas: deltas});
this.setState({dragDirections: directions, dragDeltas: deltas, swipeDirections: {}, swipeVelocities: {}});
};

onSwipe = ({directions, velocities}) => {
this.setState({swipeDirections: directions, swipeVelocities: velocities});
this.setState({swipeDirections: directions, swipeVelocities: velocities, dragDirections: {}, dragDeltas: {}});
};

render() {
Expand Down