]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/OnSwipeTouchListener.java
40a95ab3becd86ec1b3ad0befbab2051d58a2c9a
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / OnSwipeTouchListener.java
1 /*
2  * Copyright © 2019 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.ui;
19
20 import android.content.Context;
21 import android.util.Log;
22 import android.view.GestureDetector;
23 import android.view.GestureDetector.SimpleOnGestureListener;
24 import android.view.MotionEvent;
25 import android.view.View;
26
27 public abstract class OnSwipeTouchListener implements View.OnTouchListener {
28     public final GestureDetector gestureDetector;
29
30     protected OnSwipeTouchListener(Context ctx) {
31         gestureDetector = new GestureDetector(ctx, new GestureListener() );
32     }
33
34     private final class GestureListener extends SimpleOnGestureListener {
35         private static final int SWIPE_THRESHOLD = 100;
36         private static final int SWIPE_VELOCITY_THRESHOLD = 100;
37
38         @Override
39         public boolean onDown(MotionEvent e) {
40             Log.d("sw-l", "onDown");
41             return false;
42         }
43
44         @Override
45         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
46             boolean result = false;
47
48             Log.d("sw-l", "onFling");
49
50             try {
51                 float diffX = e2.getX() - e1.getX();
52                 float diffY = e2.getY() - e1.getY();
53                 if (Math.abs(diffX) > Math.abs(diffY)) {
54                     if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
55                         if (diffX > 0) {
56                             Log.d("sw-l", "calling onSwipeRight");
57                             onSwipeRight();
58                         }
59                         else {
60                             Log.d("sw-l", "calling onSwipeLeft");
61                             onSwipeLeft();
62                         }
63                     }
64                     result = true;
65                 }
66                 else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
67                     if (diffY > 0) {
68                         onSwipeDown();
69                     }
70                     else {
71                         onSwipeUp();
72                     }
73                     result = true;
74                 }
75             }
76             catch (Exception e) {
77                 e.printStackTrace();
78             }
79
80             return result;
81         }
82     }
83
84     public void onSwipeRight() {}
85     public void onSwipeLeft() {
86         Log.d("sw-l", "LEFT");
87     }
88     public void onSwipeUp() {}
89     public void onSwipeDown() {}
90 }