]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/OnSwipeTouchListener.java
fix method signature
[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.view.GestureDetector;
22 import android.view.GestureDetector.SimpleOnGestureListener;
23 import android.view.MotionEvent;
24 import android.view.View;
25
26 import static net.ktnx.mobileledger.utils.Logger.debug;
27
28 public abstract class OnSwipeTouchListener implements View.OnTouchListener {
29     public final GestureDetector gestureDetector;
30
31     protected OnSwipeTouchListener(Context ctx) {
32         gestureDetector = new GestureDetector(ctx, new GestureListener() );
33     }
34
35     private final class GestureListener extends SimpleOnGestureListener {
36         private static final int SWIPE_THRESHOLD = 100;
37         private static final int SWIPE_VELOCITY_THRESHOLD = 100;
38
39         @Override
40         public boolean onDown(MotionEvent e) {
41             debug("sw-l", "onDown");
42             return false;
43         }
44
45         @Override
46         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
47             boolean result = false;
48
49             debug("sw-l", "onFling");
50
51             try {
52                 float diffX = e2.getX() - e1.getX();
53                 float diffY = e2.getY() - e1.getY();
54                 if (Math.abs(diffX) > Math.abs(diffY)) {
55                     if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
56                         if (diffX > 0) {
57                             debug("sw-l", "calling onSwipeRight");
58                             onSwipeRight();
59                         }
60                         else {
61                             debug("sw-l", "calling onSwipeLeft");
62                             onSwipeLeft();
63                         }
64                     }
65                     result = true;
66                 }
67                 else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
68                     if (diffY > 0) {
69                         onSwipeDown();
70                     }
71                     else {
72                         onSwipeUp();
73                     }
74                     result = true;
75                 }
76             }
77             catch (Exception e) {
78                 e.printStackTrace();
79             }
80
81             return result;
82         }
83     }
84
85     public void onSwipeRight() {}
86     public void onSwipeLeft() {
87         debug("sw-l", "LEFT");
88     }
89     public void onSwipeUp() {}
90     public void onSwipeDown() {}
91 }