/*
 * 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;

    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) {
            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 = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

        @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 onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            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 && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight(view);
                        } else {
                            onSwipeLeft(view);
                        }
                        result = true;
                    }
                }
                else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom(view);
                    } else {
                        onSwipeTop(view);
                    }
                    result = true;
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }

    }
}
@@ -909,16 +909,193 @@
 ew view;
+%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    pu
@@ -1472,24 +1472,645 @@
 CTION_UP) %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
             
@@ -2751,74 +2751,9 @@
  = 1
-00;%0A        private static final int SWIPE_VELOCITY_THRESHOLD = 10
+5
 0;%0A%0A
@@ -3196,13 +3196,14 @@
 n on
-Fling
+Scroll
 (Mot
@@ -3237,24 +3237,24 @@
 , float 
-velocity
+distance
 X, float
@@ -3254,24 +3254,24 @@
 , float 
-velocity
+distance
 Y) %7B%0A   
@@ -3545,61 +3545,56 @@
 HOLD
- && Math.abs(velocityX) %3E SWIPE_VELOCITY_THRESHOLD) %7B
+) %7B%0A                        mIsScrolling = true;
 %0A   
@@ -3659,34 +3659,49 @@
         
-onSwipeRight(view)
+mDirection = SwipeDirection.RIGHT
 ;%0A      
@@ -3755,33 +3755,48 @@
         
-onSwipeLeft(view)
+mDirection = SwipeDirection.LEFT
 ;%0A      
@@ -3956,61 +3956,52 @@
 HOLD
- && Math.abs(velocityY) %3E SWIPE_VELOCITY_THRESHOLD) %7B
+) %7B%0A                    mIsScrolling = true;
 %0A   
@@ -4058,35 +4058,48 @@
         
-onSwipeBottom(view)
+mDirection = SwipeDirection.DOWN
 ;%0A      
@@ -4145,32 +4145,46 @@
         
-onSwipeTop(view)
+mDirection = SwipeDirection.UP
 ;%0A      
@@ -4226,32 +4226,154 @@
  result = true;%0A
+                %7D else %7B%0A                    mDirection = SwipeDirection.CENTER;%0A                    mIsScrolling = true;%0A
                 
@@ -4510,20 +4510,19 @@
 ;%0A        %7D%0A
-%0A
     %7D%0A%7D

change fling to scroll