9
9
import android .support .v7 .app .AlertDialog ;
10
10
import android .support .v7 .widget .CardView ;
11
11
import android .util .AttributeSet ;
12
+ import android .view .MotionEvent ;
12
13
import android .view .View ;
13
14
import android .widget .Button ;
14
15
import android .widget .ImageView ;
15
16
import android .widget .ProgressBar ;
16
17
import android .widget .RelativeLayout ;
17
18
import android .widget .TextView ;
18
19
20
+ import android .widget .Toast ;
19
21
import fr .free .nrw .commons .R ;
20
22
import fr .free .nrw .commons .contributions .MainActivity ;
21
23
import fr .free .nrw .commons .utils .ViewUtil ;
@@ -39,6 +41,8 @@ public class NearbyNoificationCardView extends CardView{
39
41
40
42
public PermissionType permissionType ;
41
43
44
+ float x1 ,x2 ;
45
+
42
46
public NearbyNoificationCardView (@ NonNull Context context ) {
43
47
super (context );
44
48
this .context = context ;
@@ -79,30 +83,6 @@ private void init() {
79
83
@ Override
80
84
protected void onAttachedToWindow () {
81
85
super .onAttachedToWindow ();
82
- // Add swipe and dismiss property
83
- SwipeDismissBehavior swipeDismissBehavior = new SwipeDismissBehavior ();
84
- swipeDismissBehavior .setSwipeDirection (SwipeDismissBehavior .SWIPE_DIRECTION_ANY );
85
- swipeDismissBehavior .setListener (new SwipeDismissBehavior .OnDismissListener () {
86
- @ Override
87
- public void onDismiss (View view ) {
88
- /**
89
- * Only dismissing view results a space after dismissed view. Since, we need to
90
- * make view invisible at all.
91
- */
92
- NearbyNoificationCardView .this .setVisibility (GONE );
93
- // Save shared preference for nearby card view accordingly
94
- ((MainActivity ) context ).prefs .edit ().putBoolean ("displayNearbyCardView" , false ).apply ();
95
- ViewUtil .showLongToast (context , getResources ().getString (R .string .nearby_notification_dismiss_message ));
96
- }
97
-
98
- @ Override
99
- public void onDragStateChanged (int state ) {
100
-
101
- }
102
- });
103
- CoordinatorLayout .LayoutParams layoutParams = (CoordinatorLayout .LayoutParams ) this .getLayoutParams ();
104
- layoutParams .setBehavior (swipeDismissBehavior );
105
-
106
86
// If you don't setVisibility after getting layout params, then you will se an empty space in place of nerabyNotificationCardView
107
87
if (((MainActivity )context ).prefs .getBoolean ("displayNearbyCardView" , true )) {
108
88
this .setVisibility (VISIBLE );
@@ -113,12 +93,37 @@ public void onDragStateChanged(int state) {
113
93
114
94
115
95
private void setActionListeners () {
116
- this .setOnClickListener (new OnClickListener () {
117
- @ Override
118
- public void onClick (View view ) {
119
- ((MainActivity )context ).viewPager .setCurrentItem (1 );
120
- }
121
- });
96
+ this .setOnClickListener (view -> ((MainActivity )context ).viewPager .setCurrentItem (1 ));
97
+
98
+ this .setOnTouchListener (
99
+ (v , event ) -> {
100
+ boolean isSwipe = false ;
101
+ switch (event .getAction ()) {
102
+ case MotionEvent .ACTION_DOWN :
103
+ x1 = event .getX ();
104
+ break ;
105
+ case MotionEvent .ACTION_UP :
106
+ x2 = event .getX ();
107
+ float deltaX = x2 - x1 ;
108
+ if (deltaX < 0 ) {
109
+ //Right to left swipe
110
+ isSwipe = true ;
111
+ } else if (deltaX > 0 ) {
112
+ //Left to right swipe
113
+ isSwipe = true ;
114
+ }
115
+ break ;
116
+ }
117
+ if (isSwipe ) {
118
+ v .setVisibility (GONE );
119
+ // Save shared preference for nearby card view accordingly
120
+ ((MainActivity ) context ).prefs .edit ()
121
+ .putBoolean ("displayNearbyCardView" , false ).apply ();
122
+ ViewUtil .showLongToast (context , getResources ().getString (R .string .nearby_notification_dismiss_message ));
123
+ return true ;
124
+ }
125
+ return false ;
126
+ });
122
127
}
123
128
124
129
/**
0 commit comments