use onAttach for context in WizardDialog

This commit is contained in:
Milos Kozak 2017-03-09 10:57:20 +01:00
parent aa8fa729ad
commit 7c6d708d06
2 changed files with 44 additions and 45 deletions

View file

@ -85,17 +85,19 @@ public class WizardDialog extends DialogFragment implements OnClickListener {
Handler mHandler; Handler mHandler;
public static HandlerThread mHandlerThread; public static HandlerThread mHandlerThread;
Context parentContext; Context context;
public WizardDialog() { public WizardDialog() {
super();
mHandlerThread = new HandlerThread(WizardDialog.class.getSimpleName()); mHandlerThread = new HandlerThread(WizardDialog.class.getSimpleName());
mHandlerThread.start(); mHandlerThread.start();
mHandler = new Handler(mHandlerThread.getLooper()); mHandler = new Handler(mHandlerThread.getLooper());
} }
@Override
public void setContext(Context context) { public void onAttach(Context context) {
parentContext = context; super.onAttach(context);
this.context = context;
} }
final private TextWatcher textWatcher = new TextWatcher() { final private TextWatcher textWatcher = new TextWatcher() {
@ -129,7 +131,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener {
@Override @Override
public void onNothingSelected(AdapterView<?> parent) { public void onNothingSelected(AdapterView<?> parent) {
ToastUtils.showToastInUiThread(parentContext, MainApp.sResources.getString(R.string.noprofileselected)); ToastUtils.showToastInUiThread(context, MainApp.sResources.getString(R.string.noprofileselected));
wizardDialogDeliverButton.setVisibility(View.GONE); wizardDialogDeliverButton.setVisibility(View.GONE);
} }
}; };
@ -200,8 +202,8 @@ public class WizardDialog extends DialogFragment implements OnClickListener {
confirmMessage += "\n" + getString(R.string.bolus) + ": " + formatNumber2decimalplaces.format(insulinAfterConstraints) + "U"; confirmMessage += "\n" + getString(R.string.bolus) + ": " + formatNumber2decimalplaces.format(insulinAfterConstraints) + "U";
confirmMessage += "\n" + getString(R.string.carbs) + ": " + carbsAfterConstraints + "g"; confirmMessage += "\n" + getString(R.string.carbs) + ": " + carbsAfterConstraints + "g";
if (insulinAfterConstraints - calculatedTotalInsulin != 0 || !carbsAfterConstraints.equals(calculatedCarbs)) { if (insulinAfterConstraints - calculatedTotalInsulin != 0 || !carbsAfterConstraints.equals(calculatedCarbs)) {
AlertDialog.Builder builder = new AlertDialog.Builder(parentContext); AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(MainApp.sResources.getString(R.string.treatmentdeliveryerror)); builder.setTitle(MainApp.sResources.getString(R.string.treatmentdeliveryerror));
builder.setMessage(getString(R.string.constraints_violation) + "\n" + getString(R.string.changeyourinput)); builder.setMessage(getString(R.string.constraints_violation) + "\n" + getString(R.string.changeyourinput));
builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), null); builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), null);
@ -212,45 +214,43 @@ public class WizardDialog extends DialogFragment implements OnClickListener {
final Double finalInsulinAfterConstraints = insulinAfterConstraints; final Double finalInsulinAfterConstraints = insulinAfterConstraints;
final Integer finalCarbsAfterConstraints = carbsAfterConstraints; final Integer finalCarbsAfterConstraints = carbsAfterConstraints;
if (parentContext != null) { AlertDialog.Builder builder = new AlertDialog.Builder(context);
AlertDialog.Builder builder = new AlertDialog.Builder(parentContext); 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) { 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() { @Override
@Override public void run() {
public void run() { PumpEnactResult result = pump.deliverTreatmentFromBolusWizard(
PumpEnactResult result = pump.deliverTreatmentFromBolusWizard( context,
parentContext, finalInsulinAfterConstraints,
finalInsulinAfterConstraints, finalCarbsAfterConstraints,
finalCarbsAfterConstraints, SafeParse.stringToDouble(bgInput.getText().toString()),
SafeParse.stringToDouble(bgInput.getText().toString()), "Manual",
"Manual", SafeParse.stringToInt(carbTimeEdit.getText().toString()),
SafeParse.stringToInt(carbTimeEdit.getText().toString()), boluscalcJSON
boluscalcJSON );
); if (!result.success) {
if (!result.success) { AlertDialog.Builder builder = new AlertDialog.Builder(context);
AlertDialog.Builder builder = new AlertDialog.Builder(parentContext); builder.setTitle(MainApp.sResources.getString(R.string.treatmentdeliveryerror));
builder.setTitle(MainApp.sResources.getString(R.string.treatmentdeliveryerror)); builder.setMessage(result.comment);
builder.setMessage(result.comment); builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), null);
builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), null); builder.show();
builder.show();
}
} }
}); }
Answers.getInstance().logCustom(new CustomEvent("Wizard")); });
} Answers.getInstance().logCustom(new CustomEvent("Wizard"));
} }
}); }
builder.setNegativeButton(getString(R.string.cancel), null); });
builder.show(); builder.setNegativeButton(getString(R.string.cancel), null);
dismiss(); builder.show();
} else { dismiss();
log.error("parentContext == null"); } else {
} log.error("parentContext == null");
} }
break; break;
} }

View file

@ -216,7 +216,6 @@ public class OverviewFragment extends Fragment {
public void onClick(View view) { public void onClick(View view) {
FragmentManager manager = getFragmentManager(); FragmentManager manager = getFragmentManager();
WizardDialog wizardDialog = new WizardDialog(); WizardDialog wizardDialog = new WizardDialog();
wizardDialog.setContext(getContext());
wizardDialog.show(manager, "WizardDialog"); wizardDialog.show(manager, "WizardDialog");
} }
}); });