From bfd9185284d5f5f238648d0522f736c494fb91c0 Mon Sep 17 00:00:00 2001 From: ricardodalarme Date: Tue, 9 Jan 2024 15:07:06 -0300 Subject: [PATCH] refactor: improve _onEndAnimation legibility --- lib/src/card_swiper.dart | 52 ++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/lib/src/card_swiper.dart b/lib/src/card_swiper.dart index f341871..2dea8fc 100644 --- a/lib/src/card_swiper.dart +++ b/lib/src/card_swiper.dart @@ -409,32 +409,42 @@ class _CardSwiperState extends State } void _onEndAnimation() { + final direction = _getEndAnimationDirection(); + final isValidDirection = this._isValidDirection(direction); + + if (isValidDirection) { + _swipe(direction); + } else { + _goBack(); + } + } + + CardSwiperDirection _getEndAnimationDirection() { if (_cardAnimation.left.abs() > widget.threshold) { - final direction = _cardAnimation.left.isNegative + return _cardAnimation.left.isNegative ? CardSwiperDirection.left : CardSwiperDirection.right; - if (direction == CardSwiperDirection.left && - widget.allowedSwipeDirection.left || - direction == CardSwiperDirection.right && - widget.allowedSwipeDirection.right) { - _swipe(direction); - } else { - _goBack(); - } - } else if (_cardAnimation.top.abs() > widget.threshold) { - final direction = _cardAnimation.top.isNegative + } + if (_cardAnimation.top.abs() > widget.threshold) { + return _cardAnimation.top.isNegative ? CardSwiperDirection.top : CardSwiperDirection.bottom; - if (direction == CardSwiperDirection.top && - widget.allowedSwipeDirection.up || - direction == CardSwiperDirection.bottom && - widget.allowedSwipeDirection.down) { - _swipe(direction); - } else { - _goBack(); - } - } else { - _goBack(); + } + return CardSwiperDirection.none; + } + + bool _isValidDirection(CardSwiperDirection direction) { + switch (direction) { + case CardSwiperDirection.left: + return widget.allowedSwipeDirection.left; + case CardSwiperDirection.right: + return widget.allowedSwipeDirection.right; + case CardSwiperDirection.top: + return widget.allowedSwipeDirection.up; + case CardSwiperDirection.bottom: + return widget.allowedSwipeDirection.down; + default: + return false; } }