/*
 * Copyright 2016-2019 Adrian Cotfas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under the License.
 */

package com.apps.adrcotfas.goodtime.Util;

import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

public abstract class OnSwipeTouchListener implements View.OnTouchListener {

    private final GestureDetector gestureDetector;
    private View view;
    private boolean mIsScrolling;

    enum SwipeDirection {
        LEFT,
        RIGHT,
        UP,
        DOWN,
        CENTER
    }

    private SwipeDirection mDirection;

    public OnSwipeTouchListener(Context context) {
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        this.view = view;
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            onPress(view);
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            if(mIsScrolling) {
                mIsScrolling  = false;
                switch (mDirection) {
                    case UP:
                        onSwipeTop(view);
                        break;
                    case DOWN:
                        onSwipeBottom(view);
                        break;
                    case LEFT:
                        onSwipeLeft(view);
                        break;
                    case RIGHT:
                        onSwipeRight(view);
                        break;
                    default:
                        break;
                }
            }
            onRelease(view);
        }
        return gestureDetector.onTouchEvent(event);
    }

    protected abstract void onSwipeRight(View view);
    protected abstract void onSwipeLeft(View view);
    protected abstract void onSwipeBottom(View view);
    protected abstract void onSwipeTop(View view);
    protected abstract void onClick(View view);
    protected abstract void onLongClick(View view);
    protected abstract void onPress(View view);
    protected abstract void onRelease(View view);

    private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 150;

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            onLongClick(view);
            super.onLongPress(e);
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            onClick(view);
            return super.onSingleTapUp(e);
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD) {
                        mIsScrolling = true;
                        if (diffX > 0) {
                            mDirection = SwipeDirection.RIGHT;
                        } else {
                            mDirection = SwipeDirection.LEFT;
                        }
                        result = true;
                    }
                }
                else if (Math.abs(diffY) > SWIPE_THRESHOLD) {
                    mIsScrolling = true;
                    if (diffY > 0) {
                        mDirection = SwipeDirection.DOWN;
                    } else {
                        mDirection = SwipeDirection.UP;
                    }
                    result = true;
                } else {
                    mDirection = SwipeDirection.CENTER;
                    mIsScrolling = true;
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }
}
@@ -913,185 +913,8 @@
 iew;
-%0A    private boolean mIsScrolling;%0A%0A    enum SwipeDirection %7B%0A        LEFT,%0A        RIGHT,%0A        UP,%0A        DOWN,%0A        CENTER%0A    %7D%0A%0A    private SwipeDirection mDirection;
 %0A%0A  
@@ -1303,629 +1303,8 @@
 ) %7B%0A
-            if(mIsScrolling) %7B%0A                mIsScrolling  = false;%0A                switch (mDirection) %7B%0A                    case UP:%0A                        onSwipeTop(view);%0A                        break;%0A                    case DOWN:%0A                        onSwipeBottom(view);%0A                        break;%0A                    case LEFT:%0A                        onSwipeLeft(view);%0A                        break;%0A                    case RIGHT:%0A                        onSwipeRight(view);%0A                        break;%0A                    default:%0A                        break;%0A                %7D%0A            %7D%0A
     
@@ -1953,9 +1953,74 @@
  = 1
-5
+00;%0A        private static final int SWIPE_VELOCITY_THRESHOLD = 10
 0;%0A%0A
@@ -2459,22 +2459,21 @@
 olean on
-Scroll
+Fling
 (MotionE
@@ -2503,24 +2503,24 @@
 , float 
-distance
+velocity
 X, float
@@ -2524,16 +2524,16 @@
 oat 
-distance
+velocity
 Y) %7B
@@ -2811,56 +2811,61 @@
 HOLD
-) %7B%0A                        mIsScrolling = true;
+ && Math.abs(velocityX) %3E SWIPE_VELOCITY_THRESHOLD) %7B
 %0A   
@@ -2934,41 +2934,26 @@
     
-mDirection = SwipeDirection.RIGHT
+onSwipeRight(view)
 ;%0A  
@@ -3015,40 +3015,25 @@
     
-mDirection = SwipeDirection.LEFT
+onSwipeLeft(view)
 ;%0A  
@@ -3197,52 +3197,61 @@
 HOLD
-) %7B%0A                    mIsScrolling = true;
+ && Math.abs(velocityY) %3E SWIPE_VELOCITY_THRESHOLD) %7B
 %0A   
@@ -3312,40 +3312,27 @@
     
-mDirection = SwipeDirection.DOWN
+onSwipeBottom(view)
 ;%0A  
@@ -3386,38 +3386,24 @@
     
-mDirection = SwipeDirection.UP
+onSwipeTop(view)
 ;%0A  
@@ -3461,130 +3461,8 @@
 ue;%0A
-                %7D else %7B%0A                    mDirection = SwipeDirection.CENTER;%0A                    mIsScrolling = true;%0A
     
@@ -3611,19 +3611,20 @@
 ;%0A        %7D%0A
+%0A
     %7D%0A%7D

Revert "gestures: change fling to scroll"