11package org .indilib .i4j .iparcos ;
22
3- import android .os .Handler ;
4- import android .view .MotionEvent ;
53import android .view .View ;
64
75/**
86 * @author Noman Rafique
97 * @author marcocipriani01
108 * @see <a href="https://stackoverflow.com/a/41466381">Continuously increase integer value as the button is pressed</a>
119 */
12- public class CounterHandler {
10+ public class CounterHandler extends LongPressHandler {
1311
14- private final Handler handler = new Handler ();
15- private final View incrementalView ;
16- private final View decrementalView ;
1712 private final int steps ;
18- private final long delay ;
1913 private final boolean isCycle ;
14+ private final CounterListener listener ;
2015 private int minValue ;
2116 private int maxValue ;
2217 private int currentValue ;
23- private boolean autoIncrement = false ;
24- private boolean autoDecrement = false ;
25- private CounterListener listener ;
2618
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 ,
4220 int maxValue , int initialValue , int steps ,
4321 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!" );
5625 }
5726 this .minValue = minValue ;
5827 this .maxValue = maxValue ;
@@ -61,53 +30,8 @@ public CounterHandler(View incrementalView, View decrementalView, int minValue,
6130 }
6231 this .currentValue = initialValue ;
6332 this .steps = steps ;
64- this .delay = delay ;
6533 this .isCycle = isCycle ;
66- this .incrementalView = incrementalView ;
67- this .decrementalView = decrementalView ;
6834 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 );
11135 }
11236
11337 public int getValue () {
@@ -117,10 +41,8 @@ public int getValue() {
11741 public void setValue (int newValue ) {
11842 if ((maxValue != -1 ) && (newValue > maxValue )) {
11943 currentValue = maxValue ;
120-
12144 } else if ((minValue != -1 ) && (newValue < minValue )) {
12245 currentValue = minValue ;
123-
12446 } else {
12547 currentValue = newValue ;
12648 }
@@ -131,31 +53,24 @@ public void setMaxValue(int maxValue) {
13153 throw new IllegalArgumentException ("Max value < min value!" );
13254 }
13355 this .maxValue = maxValue ;
134- if (currentValue > this .maxValue ) {
135- currentValue = this .maxValue ;
136- }
56+ if (currentValue > this .maxValue ) currentValue = this .maxValue ;
13757 }
13858
13959 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!" );
14361 this .minValue = minValue ;
144- if (currentValue < this .minValue ) {
145- currentValue = this .minValue ;
146- }
62+ if (currentValue < this .minValue ) currentValue = this .minValue ;
14763 }
14864
149- private void increment () {
65+ @ Override
66+ protected void increment () {
15067 int number = this .currentValue ;
15168 if (maxValue != -1 ) {
15269 if (number + steps <= maxValue ) {
15370 number += steps ;
154-
15571 } else if (isCycle ) {
15672 number = minValue == -1 ? 0 : minValue ;
15773 }
158-
15974 } else {
16075 number += steps ;
16176 }
@@ -165,16 +80,15 @@ private void increment() {
16580 }
16681 }
16782
168- private void decrement () {
83+ @ Override
84+ protected void decrement () {
16985 int number = this .currentValue ;
17086 if (minValue != -1 ) {
17187 if (number - steps >= minValue ) {
17288 number -= steps ;
173-
17489 } else if (isCycle ) {
17590 number = maxValue == -1 ? 0 : maxValue ;
17691 }
177-
17892 } else {
17993 number -= steps ;
18094 }
@@ -185,7 +99,6 @@ private void decrement() {
18599 }
186100
187101 public interface CounterListener {
188-
189102 void onIncrement (View view , int number );
190103
191104 void onDecrement (View view , int number );
0 commit comments