progressive plusminus element

This commit is contained in:
Milos Kozak 2016-08-08 16:03:03 +02:00
parent 2e72e18c62
commit 1ced97ced6

View file

@ -44,17 +44,27 @@ public class PlusMinusEditText implements View.OnKeyListener,
private class UpdateCounterTask implements Runnable { private class UpdateCounterTask implements Runnable {
private boolean mInc; private boolean mInc;
private int repeated = 0;
private int multiplier = 1;
private final int doubleLimit = 5;
public UpdateCounterTask(boolean inc) { public UpdateCounterTask(boolean inc) {
mInc = inc; mInc = inc;
} }
public void run() { public void run() {
Message msg = new Message();
if (repeated % doubleLimit == 0) multiplier *= 2;
repeated++;
msg.arg1 = multiplier;
msg.arg2 = repeated;
if (mInc) { if (mInc) {
mHandler.sendEmptyMessage(MSG_INC); msg.what = MSG_INC;
} else { } else {
mHandler.sendEmptyMessage(MSG_DEC); msg.what = MSG_DEC;
} }
mHandler.sendMessage(msg);
} }
} }
@ -78,10 +88,10 @@ public class PlusMinusEditText implements View.OnKeyListener,
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
switch (msg.what) { switch (msg.what) {
case MSG_INC: case MSG_INC:
inc(); inc(msg.arg1);
return; return;
case MSG_DEC: case MSG_DEC:
dec(); dec(msg.arg1);
return; return;
} }
super.handleMessage(msg); super.handleMessage(msg);
@ -110,8 +120,8 @@ public class PlusMinusEditText implements View.OnKeyListener,
this.step = step; this.step = step;
} }
private void inc() { private void inc(int multiplier) {
value += step; value += step * multiplier;
if (value > maxValue) { if (value > maxValue) {
value = maxValue; value = maxValue;
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.youareonallowedlimit)); ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.youareonallowedlimit));
@ -120,8 +130,8 @@ public class PlusMinusEditText implements View.OnKeyListener,
updateEditText(); updateEditText();
} }
private void dec() { private void dec( int multiplier) {
value -= step; value -= step * multiplier;
if (value < minValue) { if (value < minValue) {
value = minValue; value = minValue;
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.youareonallowedlimit)); ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.youareonallowedlimit));
@ -139,7 +149,7 @@ public class PlusMinusEditText implements View.OnKeyListener,
private void startUpdating(boolean inc) { private void startUpdating(boolean inc) {
if (mUpdater != null) { if (mUpdater != null) {
log.debug(getClass().getSimpleName(), "Another executor is still active"); log.debug("Another executor is still active");
return; return;
} }
mUpdater = Executors.newSingleThreadScheduledExecutor(); mUpdater = Executors.newSingleThreadScheduledExecutor();
@ -158,9 +168,9 @@ public class PlusMinusEditText implements View.OnKeyListener,
public void onClick(View v) { public void onClick(View v) {
if (mUpdater == null) { if (mUpdater == null) {
if (v == plusImage) { if (v == plusImage) {
inc(); inc(1);
} else { } else {
dec(); dec(1);
} }
} }
} }