1
1
package org .indilib .i4j .iparcos ;
2
2
3
- import android .os .Handler ;
4
- import android .view .MotionEvent ;
5
3
import android .view .View ;
6
4
7
5
/**
8
6
* @author Noman Rafique
9
7
* @author marcocipriani01
10
8
* @see <a href="https://stackoverflow.com/a/41466381">Continuously increase integer value as the button is pressed</a>
11
9
*/
12
- public class CounterHandler {
10
+ public class CounterHandler extends LongPressHandler {
13
11
14
- private final Handler handler = new Handler ();
15
- private final View incrementalView ;
16
- private final View decrementalView ;
17
12
private final int steps ;
18
- private final long delay ;
19
13
private final boolean isCycle ;
14
+ private final CounterListener listener ;
20
15
private int minValue ;
21
16
private int maxValue ;
22
17
private int currentValue ;
23
- private boolean autoIncrement = false ;
24
- private boolean autoDecrement = false ;
25
- private CounterListener listener ;
26
18
27
- private final Runnable counterRunnable = new Runnable () {
28
- @ Override
29
- public void run () {
30
- if (autoIncrement ) {
31
- increment ();
32
- handler .postDelayed (this , delay );
33
-
34
- } else if (autoDecrement ) {
35
- decrement ();
36
- handler .postDelayed (this , delay );
37
- }
38
- }
39
- };
40
-
41
- public CounterHandler (View incrementalView , View decrementalView , int minValue ,
19
+ public CounterHandler (View incrementView , View decrementView , int minValue ,
42
20
int maxValue , int initialValue , int steps ,
43
21
long delay , boolean isCycle , CounterListener listener ) {
44
- this (incrementalView , decrementalView , minValue , maxValue , initialValue , steps , delay , isCycle , listener , true );
45
- }
46
-
47
- public CounterHandler (View incrementalView , View decrementalView , int minValue ,
48
- int maxValue , int initialValue , int steps ,
49
- long delay , boolean isCycle , CounterListener listener , boolean setNow ) {
50
- if ((minValue != -1 ) && (maxValue != -1 )) {
51
- if (maxValue <= minValue ) {
52
- throw new IllegalArgumentException ("Max value < min value!" );
53
- } else if (minValue >= maxValue ) {
54
- throw new IllegalArgumentException ("Min value > max value!" );
55
- }
22
+ super (incrementView , decrementView , delay );
23
+ if ((minValue != -1 ) && (maxValue != -1 ) && (minValue >= maxValue )) {
24
+ throw new IllegalArgumentException ("Counter bound error!" );
56
25
}
57
26
this .minValue = minValue ;
58
27
this .maxValue = maxValue ;
@@ -61,53 +30,8 @@ public CounterHandler(View incrementalView, View decrementalView, int minValue,
61
30
}
62
31
this .currentValue = initialValue ;
63
32
this .steps = steps ;
64
- this .delay = delay ;
65
33
this .isCycle = isCycle ;
66
- this .incrementalView = incrementalView ;
67
- this .decrementalView = decrementalView ;
68
34
this .listener = listener ;
69
-
70
- this .decrementalView .setOnClickListener (v -> decrement ());
71
- this .decrementalView .setOnLongClickListener (v -> {
72
- autoDecrement = true ;
73
- handler .postDelayed (counterRunnable , CounterHandler .this .delay );
74
- return false ;
75
- });
76
- this .decrementalView .setOnTouchListener ((v , event ) -> {
77
- if (event .getAction () == MotionEvent .ACTION_UP && autoDecrement ) {
78
- autoDecrement = false ;
79
- }
80
- return false ;
81
- });
82
-
83
- this .incrementalView .setOnClickListener (v -> increment ());
84
- this .incrementalView .setOnLongClickListener (v -> {
85
- autoIncrement = true ;
86
- handler .postDelayed (counterRunnable , CounterHandler .this .delay );
87
- return false ;
88
- });
89
- this .incrementalView .setOnTouchListener ((v , event ) -> {
90
- if (event .getAction () == MotionEvent .ACTION_UP && autoIncrement ) {
91
- autoIncrement = false ;
92
- }
93
- return false ;
94
- });
95
-
96
- if (this .listener != null && setNow ) {
97
- this .listener .onIncrement (this .incrementalView , this .currentValue );
98
- this .listener .onDecrement (this .decrementalView , this .currentValue );
99
- }
100
- }
101
-
102
- public void stop () {
103
- listener = null ;
104
- autoDecrement = autoIncrement = false ;
105
- incrementalView .setOnClickListener (null );
106
- incrementalView .setOnLongClickListener (null );
107
- incrementalView .setOnTouchListener (null );
108
- decrementalView .setOnClickListener (null );
109
- decrementalView .setOnLongClickListener (null );
110
- decrementalView .setOnTouchListener (null );
111
35
}
112
36
113
37
public int getValue () {
@@ -117,10 +41,8 @@ public int getValue() {
117
41
public void setValue (int newValue ) {
118
42
if ((maxValue != -1 ) && (newValue > maxValue )) {
119
43
currentValue = maxValue ;
120
-
121
44
} else if ((minValue != -1 ) && (newValue < minValue )) {
122
45
currentValue = minValue ;
123
-
124
46
} else {
125
47
currentValue = newValue ;
126
48
}
@@ -131,31 +53,24 @@ public void setMaxValue(int maxValue) {
131
53
throw new IllegalArgumentException ("Max value < min value!" );
132
54
}
133
55
this .maxValue = maxValue ;
134
- if (currentValue > this .maxValue ) {
135
- currentValue = this .maxValue ;
136
- }
56
+ if (currentValue > this .maxValue ) currentValue = this .maxValue ;
137
57
}
138
58
139
59
public void setMinValue (int minValue ) {
140
- if (minValue >= maxValue ) {
141
- throw new IllegalArgumentException ("Min value > max value!" );
142
- }
60
+ if (minValue >= maxValue ) throw new IllegalArgumentException ("Min value > max value!" );
143
61
this .minValue = minValue ;
144
- if (currentValue < this .minValue ) {
145
- currentValue = this .minValue ;
146
- }
62
+ if (currentValue < this .minValue ) currentValue = this .minValue ;
147
63
}
148
64
149
- private void increment () {
65
+ @ Override
66
+ protected void increment () {
150
67
int number = this .currentValue ;
151
68
if (maxValue != -1 ) {
152
69
if (number + steps <= maxValue ) {
153
70
number += steps ;
154
-
155
71
} else if (isCycle ) {
156
72
number = minValue == -1 ? 0 : minValue ;
157
73
}
158
-
159
74
} else {
160
75
number += steps ;
161
76
}
@@ -165,16 +80,15 @@ private void increment() {
165
80
}
166
81
}
167
82
168
- private void decrement () {
83
+ @ Override
84
+ protected void decrement () {
169
85
int number = this .currentValue ;
170
86
if (minValue != -1 ) {
171
87
if (number - steps >= minValue ) {
172
88
number -= steps ;
173
-
174
89
} else if (isCycle ) {
175
90
number = maxValue == -1 ? 0 : maxValue ;
176
91
}
177
-
178
92
} else {
179
93
number -= steps ;
180
94
}
@@ -185,7 +99,6 @@ private void decrement() {
185
99
}
186
100
187
101
public interface CounterListener {
188
-
189
102
void onIncrement (View view , int number );
190
103
191
104
void onDecrement (View view , int number );
0 commit comments