Merge pull request #1001 from jotomo/carbs-gen-refactor
Extract and deduplicate eCarbs generation
This commit is contained in:
commit
57973d4d3a
3 changed files with 90 additions and 113 deletions
|
@ -1,9 +1,7 @@
|
||||||
package info.nightscout.androidaps.plugins.Overview.Dialogs;
|
package info.nightscout.androidaps.plugins.Overview.Dialogs;
|
||||||
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.HandlerThread;
|
import android.os.HandlerThread;
|
||||||
import android.support.annotation.Nullable;
|
|
||||||
import android.support.v4.app.DialogFragment;
|
import android.support.v4.app.DialogFragment;
|
||||||
import android.support.v7.app.AlertDialog;
|
import android.support.v7.app.AlertDialog;
|
||||||
import android.text.Editable;
|
import android.text.Editable;
|
||||||
|
@ -33,15 +31,13 @@ import java.util.List;
|
||||||
import info.nightscout.androidaps.Constants;
|
import info.nightscout.androidaps.Constants;
|
||||||
import info.nightscout.androidaps.MainApp;
|
import info.nightscout.androidaps.MainApp;
|
||||||
import info.nightscout.androidaps.R;
|
import info.nightscout.androidaps.R;
|
||||||
import info.nightscout.androidaps.data.DetailedBolusInfo;
|
|
||||||
import info.nightscout.androidaps.data.Profile;
|
import info.nightscout.androidaps.data.Profile;
|
||||||
import info.nightscout.androidaps.db.CareportalEvent;
|
import info.nightscout.androidaps.db.CareportalEvent;
|
||||||
import info.nightscout.androidaps.db.Source;
|
import info.nightscout.androidaps.db.Source;
|
||||||
import info.nightscout.androidaps.db.TempTarget;
|
import info.nightscout.androidaps.db.TempTarget;
|
||||||
import info.nightscout.androidaps.interfaces.Constraint;
|
import info.nightscout.androidaps.interfaces.Constraint;
|
||||||
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
|
import info.nightscout.androidaps.plugins.Treatments.CarbsGenerator;
|
||||||
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
|
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
|
||||||
import info.nightscout.androidaps.queue.Callback;
|
|
||||||
import info.nightscout.utils.DateUtil;
|
import info.nightscout.utils.DateUtil;
|
||||||
import info.nightscout.utils.DecimalFormatter;
|
import info.nightscout.utils.DecimalFormatter;
|
||||||
import info.nightscout.utils.DefaultValueHelper;
|
import info.nightscout.utils.DefaultValueHelper;
|
||||||
|
@ -364,48 +360,40 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, C
|
||||||
}
|
}
|
||||||
accepted = true;
|
accepted = true;
|
||||||
|
|
||||||
if (startActivityTTCheckbox.isChecked()) {
|
if (startActivityTTCheckbox.isChecked()) {
|
||||||
TempTarget tempTarget = new TempTarget()
|
TempTarget tempTarget = new TempTarget()
|
||||||
.date(System.currentTimeMillis())
|
.date(System.currentTimeMillis())
|
||||||
.duration(finalActivityTTDuration)
|
.duration(finalActivityTTDuration)
|
||||||
.reason(MainApp.gs(R.string.activity))
|
.reason(MainApp.gs(R.string.activity))
|
||||||
.source(Source.USER)
|
.source(Source.USER)
|
||||||
.low(Profile.toMgdl(finalActivityTT, currentProfile.getUnits()))
|
.low(Profile.toMgdl(finalActivityTT, currentProfile.getUnits()))
|
||||||
.high(Profile.toMgdl(finalActivityTT, currentProfile.getUnits()));
|
.high(Profile.toMgdl(finalActivityTT, currentProfile.getUnits()));
|
||||||
TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget);
|
TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget);
|
||||||
} else if (startEatingSoonTTCheckbox.isChecked()) {
|
} else if (startEatingSoonTTCheckbox.isChecked()) {
|
||||||
TempTarget tempTarget = new TempTarget()
|
TempTarget tempTarget = new TempTarget()
|
||||||
.date(System.currentTimeMillis())
|
.date(System.currentTimeMillis())
|
||||||
.duration(finalEatingSoonTTDuration)
|
.duration(finalEatingSoonTTDuration)
|
||||||
.reason(MainApp.gs(R.string.eatingsoon))
|
.reason(MainApp.gs(R.string.eatingsoon))
|
||||||
.source(Source.USER)
|
.source(Source.USER)
|
||||||
.low(Profile.toMgdl(finalEatigSoonTT, currentProfile.getUnits()))
|
.low(Profile.toMgdl(finalEatigSoonTT, currentProfile.getUnits()))
|
||||||
.high(Profile.toMgdl(finalEatigSoonTT, currentProfile.getUnits()));
|
.high(Profile.toMgdl(finalEatigSoonTT, currentProfile.getUnits()));
|
||||||
TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget);
|
TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget);
|
||||||
} else if (startHypoTTCheckbox.isChecked()) {
|
} else if (startHypoTTCheckbox.isChecked()) {
|
||||||
TempTarget tempTarget = new TempTarget()
|
TempTarget tempTarget = new TempTarget()
|
||||||
.date(System.currentTimeMillis())
|
.date(System.currentTimeMillis())
|
||||||
.duration(finalHypoTTDuration)
|
.duration(finalHypoTTDuration)
|
||||||
.reason(MainApp.gs(R.string.hypo))
|
.reason(MainApp.gs(R.string.hypo))
|
||||||
.source(Source.USER)
|
.source(Source.USER)
|
||||||
.low(Profile.toMgdl(finalHypoTT, currentProfile.getUnits()))
|
.low(Profile.toMgdl(finalHypoTT, currentProfile.getUnits()))
|
||||||
.high(Profile.toMgdl(finalHypoTT, currentProfile.getUnits()));
|
.high(Profile.toMgdl(finalHypoTT, currentProfile.getUnits()));
|
||||||
TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget);
|
TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (carbsAfterConstraints > 0) {
|
if (carbsAfterConstraints > 0) {
|
||||||
if (duration == 0) {
|
if (duration == 0) {
|
||||||
createCarb(carbsAfterConstraints, time, notes);
|
CarbsGenerator.createCarb(carbsAfterConstraints, time, CareportalEvent.CARBCORRECTION, notes);
|
||||||
} else {
|
} else {
|
||||||
long remainingCarbs = carbsAfterConstraints;
|
CarbsGenerator.generateCarbs(carbsAfterConstraints, time, duration, notes);
|
||||||
int ticks = (duration * 4); //duration guaranteed to be integer greater zero
|
|
||||||
for (int i = 0; i < ticks; i++){
|
|
||||||
long carbTime = time + i * 15 * 60 * 1000;
|
|
||||||
long smallCarbAmount = Math.round((1d * remainingCarbs) / (ticks-i)); //on last iteration (ticks-i) is 1 -> smallCarbAmount == remainingCarbs
|
|
||||||
remainingCarbs -= smallCarbAmount;
|
|
||||||
if (smallCarbAmount > 0)
|
|
||||||
createCarb(smallCarbAmount, carbTime, notes);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -420,31 +408,4 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, C
|
||||||
log.error("Unhandled exception", e);
|
log.error("Unhandled exception", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createCarb(long carbs, long time, @Nullable String notes) {
|
|
||||||
DetailedBolusInfo carbInfo = new DetailedBolusInfo();
|
|
||||||
carbInfo.date = time;
|
|
||||||
carbInfo.eventType = CareportalEvent.CARBCORRECTION;
|
|
||||||
carbInfo.carbs = carbs;
|
|
||||||
carbInfo.context = getContext();
|
|
||||||
carbInfo.source = Source.USER;
|
|
||||||
carbInfo.notes = notes;
|
|
||||||
if (ConfigBuilderPlugin.getActivePump().getPumpDescription().storesCarbInfo && carbInfo.date <= now()) {
|
|
||||||
ConfigBuilderPlugin.getCommandQueue().bolus(carbInfo, new Callback() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
if (!result.success) {
|
|
||||||
Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class);
|
|
||||||
i.putExtra("soundid", R.raw.boluserror);
|
|
||||||
i.putExtra("status", result.comment);
|
|
||||||
i.putExtra("title", MainApp.gs(R.string.treatmentdeliveryerror));
|
|
||||||
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
||||||
MainApp.instance().startActivity(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
TreatmentsPlugin.getPlugin().addToHistoryTreatment(carbInfo);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,56 @@
|
||||||
|
package info.nightscout.androidaps.plugins.Treatments;
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
|
import info.nightscout.androidaps.MainApp;
|
||||||
|
import info.nightscout.androidaps.R;
|
||||||
|
import info.nightscout.androidaps.data.DetailedBolusInfo;
|
||||||
|
import info.nightscout.androidaps.db.CareportalEvent;
|
||||||
|
import info.nightscout.androidaps.db.Source;
|
||||||
|
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
|
||||||
|
import info.nightscout.androidaps.plugins.Overview.Dialogs.ErrorHelperActivity;
|
||||||
|
import info.nightscout.androidaps.queue.Callback;
|
||||||
|
|
||||||
|
import static info.nightscout.utils.DateUtil.now;
|
||||||
|
|
||||||
|
public class CarbsGenerator {
|
||||||
|
public static void generateCarbs(int amount, long startTime, int duration, @Nullable String notes) {
|
||||||
|
long remainingCarbs = amount;
|
||||||
|
int ticks = (duration * 4); //duration guaranteed to be integer greater zero
|
||||||
|
for (int i = 0; i < ticks; i++){
|
||||||
|
long carbTime = startTime + i * 15 * 60 * 1000;
|
||||||
|
int smallCarbAmount = (int) Math.round((1d * remainingCarbs) / (ticks-i)); //on last iteration (ticks-i) is 1 -> smallCarbAmount == remainingCarbs
|
||||||
|
remainingCarbs -= smallCarbAmount;
|
||||||
|
if (smallCarbAmount > 0)
|
||||||
|
createCarb(smallCarbAmount, carbTime, CareportalEvent.MEALBOLUS, notes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void createCarb(int carbs, long time, String eventType, @Nullable String notes) {
|
||||||
|
DetailedBolusInfo carbInfo = new DetailedBolusInfo();
|
||||||
|
carbInfo.date = time;
|
||||||
|
carbInfo.eventType = eventType;
|
||||||
|
carbInfo.carbs = carbs;
|
||||||
|
carbInfo.context = MainApp.instance();
|
||||||
|
carbInfo.source = Source.USER;
|
||||||
|
carbInfo.notes = notes;
|
||||||
|
if (ConfigBuilderPlugin.getActivePump().getPumpDescription().storesCarbInfo && carbInfo.date <= now()) {
|
||||||
|
ConfigBuilderPlugin.getCommandQueue().bolus(carbInfo, new Callback() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (!result.success) {
|
||||||
|
Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class);
|
||||||
|
i.putExtra("soundid", R.raw.boluserror);
|
||||||
|
i.putExtra("status", result.comment);
|
||||||
|
i.putExtra("title", MainApp.gs(R.string.treatmentdeliveryerror));
|
||||||
|
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
|
MainApp.instance().startActivity(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
TreatmentsPlugin.getPlugin().addToHistoryTreatment(carbInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,9 +1,7 @@
|
||||||
package info.nightscout.androidaps.plugins.Wear;
|
package info.nightscout.androidaps.plugins.Wear;
|
||||||
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.os.HandlerThread;
|
import android.os.HandlerThread;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.support.annotation.Nullable;
|
|
||||||
|
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
|
@ -38,7 +36,6 @@ import info.nightscout.androidaps.plugins.IobCobCalculator.CobInfo;
|
||||||
import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin;
|
import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin;
|
||||||
import info.nightscout.androidaps.plugins.Loop.APSResult;
|
import info.nightscout.androidaps.plugins.Loop.APSResult;
|
||||||
import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
|
import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
|
||||||
import info.nightscout.androidaps.plugins.Overview.Dialogs.ErrorHelperActivity;
|
|
||||||
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
|
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
|
||||||
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin;
|
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin;
|
||||||
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump;
|
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump;
|
||||||
|
@ -46,6 +43,7 @@ import info.nightscout.androidaps.plugins.PumpDanaRKorean.DanaRKoreanPlugin;
|
||||||
import info.nightscout.androidaps.plugins.PumpDanaRS.DanaRSPlugin;
|
import info.nightscout.androidaps.plugins.PumpDanaRS.DanaRSPlugin;
|
||||||
import info.nightscout.androidaps.plugins.PumpDanaRv2.DanaRv2Plugin;
|
import info.nightscout.androidaps.plugins.PumpDanaRv2.DanaRv2Plugin;
|
||||||
import info.nightscout.androidaps.plugins.PumpInsight.InsightPlugin;
|
import info.nightscout.androidaps.plugins.PumpInsight.InsightPlugin;
|
||||||
|
import info.nightscout.androidaps.plugins.Treatments.CarbsGenerator;
|
||||||
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
|
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
|
||||||
import info.nightscout.androidaps.queue.Callback;
|
import info.nightscout.androidaps.queue.Callback;
|
||||||
import info.nightscout.utils.BolusWizard;
|
import info.nightscout.utils.BolusWizard;
|
||||||
|
@ -56,8 +54,6 @@ import info.nightscout.utils.SP;
|
||||||
import info.nightscout.utils.SafeParse;
|
import info.nightscout.utils.SafeParse;
|
||||||
import info.nightscout.utils.ToastUtils;
|
import info.nightscout.utils.ToastUtils;
|
||||||
|
|
||||||
import static info.nightscout.utils.DateUtil.now;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by adrian on 09/02/17.
|
* Created by adrian on 09/02/17.
|
||||||
*/
|
*/
|
||||||
|
@ -633,49 +629,13 @@ public class ActionStringHandler {
|
||||||
private static void doECarbs(int carbs, long time, int duration) {
|
private static void doECarbs(int carbs, long time, int duration) {
|
||||||
if (carbs > 0) {
|
if (carbs > 0) {
|
||||||
if (duration == 0) {
|
if (duration == 0) {
|
||||||
createCarb(carbs, time, "watch");
|
CarbsGenerator.createCarb(carbs, time, CareportalEvent.CARBCORRECTION, "watch");
|
||||||
} else {
|
} else {
|
||||||
long remainingCarbs = carbs;
|
CarbsGenerator.generateCarbs(carbs, time, duration, "watch eCarbs");
|
||||||
int ticks = (duration * 4); //duration guaranteed to be integer greater zero
|
|
||||||
for (int i = 0; i < ticks; i++){
|
|
||||||
long carbTime = time + i * 15 * 60 * 1000;
|
|
||||||
long smallCarbAmount = Math.round((1d * remainingCarbs) / (ticks-i)); //on last iteration (ticks-i) is 1 -> smallCarbAmount == remainingCarbs
|
|
||||||
remainingCarbs -= smallCarbAmount;
|
|
||||||
if (smallCarbAmount > 0)
|
|
||||||
createCarb(smallCarbAmount, carbTime, "watch eCarbs");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void createCarb(long carbs, long time, @Nullable String notes) {
|
|
||||||
DetailedBolusInfo carbInfo = new DetailedBolusInfo();
|
|
||||||
carbInfo.date = time;
|
|
||||||
carbInfo.eventType = CareportalEvent.CARBCORRECTION;
|
|
||||||
carbInfo.carbs = carbs;
|
|
||||||
carbInfo.context = MainApp.instance();
|
|
||||||
carbInfo.source = Source.USER;
|
|
||||||
carbInfo.notes = notes;
|
|
||||||
if (ConfigBuilderPlugin.getActivePump().getPumpDescription().storesCarbInfo && carbInfo.date <= now()) {
|
|
||||||
ConfigBuilderPlugin.getCommandQueue().bolus(carbInfo, new Callback() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
if (!result.success) {
|
|
||||||
Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class);
|
|
||||||
i.putExtra("soundid", R.raw.boluserror);
|
|
||||||
i.putExtra("status", result.comment);
|
|
||||||
i.putExtra("title", MainApp.gs(R.string.treatmentdeliveryerror));
|
|
||||||
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
||||||
MainApp.instance().startActivity(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
TreatmentsPlugin.getPlugin().addToHistoryTreatment(carbInfo);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private static void setCPP(int timeshift, int percentage) {
|
private static void setCPP(int timeshift, int percentage) {
|
||||||
|
|
||||||
String msg = "";
|
String msg = "";
|
||||||
|
|
Loading…
Reference in a new issue