guard double clicks

This commit is contained in:
AdrianLxM 2017-11-06 15:07:39 +01:00
parent 8c8674ed63
commit e4f15c0e0d
3 changed files with 146 additions and 103 deletions

View file

@ -51,6 +51,10 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene
private Handler mHandler; private Handler mHandler;
//one shot guards
private boolean accepted;
private boolean okClicked;
public NewTreatmentDialog() { public NewTreatmentDialog() {
HandlerThread mHandlerThread = new HandlerThread(NewTreatmentDialog.class.getSimpleName()); HandlerThread mHandlerThread = new HandlerThread(NewTreatmentDialog.class.getSimpleName());
mHandlerThread.start(); mHandlerThread.start();
@ -109,10 +113,15 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene
} }
@Override @Override
public void onClick(View view) { public synchronized void onClick(View view) {
switch (view.getId()) { switch (view.getId()) {
case R.id.ok: case R.id.ok:
if (okClicked){
log.debug("guarding: ok already clicked");
dismiss();
return;
}
okClicked = true;
try { try {
Double insulin = SafeParse.stringToDouble(editInsulin.getText()); Double insulin = SafeParse.stringToDouble(editInsulin.getText());
final Integer carbs = SafeParse.stringToInt(editCarbs.getText()); final Integer carbs = SafeParse.stringToInt(editCarbs.getText());
@ -132,12 +141,18 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene
final int finalCarbsAfterConstraints = carbsAfterConstraints; final int finalCarbsAfterConstraints = carbsAfterConstraints;
final Context context = getContext(); final Context context = getContext();
AlertDialog.Builder builder = new AlertDialog.Builder(context); final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(this.getContext().getString(R.string.confirmation)); builder.setTitle(this.getContext().getString(R.string.confirmation));
builder.setMessage(Html.fromHtml(confirmMessage)); builder.setMessage(Html.fromHtml(confirmMessage));
builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() { builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { public void onClick(DialogInterface dialog, int id) {
synchronized (builder) {
if (accepted) {
log.debug("guarding: already accepted");
return;
}
accepted = true;
if (finalInsulinAfterConstraints > 0 || finalCarbsAfterConstraints > 0) { if (finalInsulinAfterConstraints > 0 || finalCarbsAfterConstraints > 0) {
final PumpInterface pump = MainApp.getConfigBuilder(); final PumpInterface pump = MainApp.getConfigBuilder();
mHandler.post(new Runnable() { mHandler.post(new Runnable() {
@ -171,6 +186,7 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene
Answers.getInstance().logCustom(new CustomEvent("Bolus")); Answers.getInstance().logCustom(new CustomEvent("Bolus"));
} }
} }
}
}); });
builder.setNegativeButton(getString(R.string.cancel), null); builder.setNegativeButton(getString(R.string.cancel), null);
builder.show(); builder.show();

View file

@ -113,6 +113,11 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com
Context context; Context context;
//one shot guards
private boolean accepted;
private boolean okClicked;
public WizardDialog() { public WizardDialog() {
super(); super();
mHandlerThread = new HandlerThread(WizardDialog.class.getSimpleName()); mHandlerThread = new HandlerThread(WizardDialog.class.getSimpleName());
@ -296,9 +301,15 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com
} }
@Override @Override
public void onClick(View view) { public synchronized void onClick(View view) {
switch (view.getId()) { switch (view.getId()) {
case R.id.ok: case R.id.ok:
if (okClicked){
log.debug("guarding: ok already clicked");
dismiss();
return;
}
okClicked = true;
if (calculatedTotalInsulin > 0d || calculatedCarbs > 0d) { if (calculatedTotalInsulin > 0d || calculatedCarbs > 0d) {
DecimalFormat formatNumber2decimalplaces = new DecimalFormat("0.00"); DecimalFormat formatNumber2decimalplaces = new DecimalFormat("0.00");
@ -326,11 +337,17 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com
final int carbTime = SafeParse.stringToInt(editCarbTime.getText()); final int carbTime = SafeParse.stringToInt(editCarbTime.getText());
final boolean useSuperBolus = superbolusCheckbox.isChecked(); final boolean useSuperBolus = superbolusCheckbox.isChecked();
AlertDialog.Builder builder = new AlertDialog.Builder(context); final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(MainApp.sResources.getString(R.string.confirmation)); builder.setTitle(MainApp.sResources.getString(R.string.confirmation));
builder.setMessage(Html.fromHtml(confirmMessage)); builder.setMessage(Html.fromHtml(confirmMessage));
builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() { builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { public void onClick(DialogInterface dialog, int id) {
synchronized (builder) {
if (accepted) {
log.debug("guarding: already accepted");
return;
}
accepted = true;
if (finalInsulinAfterConstraints > 0 || finalCarbsAfterConstraints > 0) { if (finalInsulinAfterConstraints > 0 || finalCarbsAfterConstraints > 0) {
final ConfigBuilderPlugin pump = MainApp.getConfigBuilder(); final ConfigBuilderPlugin pump = MainApp.getConfigBuilder();
mHandler.post(new Runnable() { mHandler.post(new Runnable() {
@ -378,6 +395,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com
Answers.getInstance().logCustom(new CustomEvent("Wizard")); Answers.getInstance().logCustom(new CustomEvent("Wizard"));
} }
} }
}
}); });
builder.setNegativeButton(getString(R.string.cancel), null); builder.setNegativeButton(getString(R.string.cancel), null);
builder.show(); builder.show();

View file

@ -52,6 +52,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import info.nightscout.androidaps.BuildConfig; import info.nightscout.androidaps.BuildConfig;
import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Config;
@ -689,11 +690,18 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
final Double finalInsulinAfterConstraints = insulinAfterConstraints; final Double finalInsulinAfterConstraints = insulinAfterConstraints;
final Integer finalCarbsAfterConstraints = carbsAfterConstraints; final Integer finalCarbsAfterConstraints = carbsAfterConstraints;
final Context context = getContext(); final Context context = getContext();
AlertDialog.Builder builder = new AlertDialog.Builder(context); final AlertDialog.Builder builder = new AlertDialog.Builder(context);
final AtomicBoolean accepted = new AtomicBoolean(false);
builder.setTitle(MainApp.sResources.getString(R.string.confirmation)); builder.setTitle(MainApp.sResources.getString(R.string.confirmation));
builder.setMessage(confirmMessage); builder.setMessage(confirmMessage);
builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() { builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { public void onClick(DialogInterface dialog, int id) {
synchronized (accepted) {
if(accepted.get()) {
log.debug("Guarding: already accepted!");
return;
}
accepted.set(true);
if (finalInsulinAfterConstraints > 0 || finalCarbsAfterConstraints > 0) { if (finalInsulinAfterConstraints > 0 || finalCarbsAfterConstraints > 0) {
final ConfigBuilderPlugin pump = MainApp.getConfigBuilder(); final ConfigBuilderPlugin pump = MainApp.getConfigBuilder();
sHandler.post(new Runnable() { sHandler.post(new Runnable() {
@ -725,6 +733,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
Answers.getInstance().logCustom(new CustomEvent("QuickWizard")); Answers.getInstance().logCustom(new CustomEvent("QuickWizard"));
} }
} }
}
}); });
builder.setNegativeButton(getString(R.string.cancel), null); builder.setNegativeButton(getString(R.string.cancel), null);
builder.show(); builder.show();