use of com.faltenreich.diaguard.shared.data.database.entity.BloodSugar in project Diaguard by Faltenreich.
the class MeasurementDao method getAvgMeasurement.
public Measurement getAvgMeasurement(Category category, Interval interval) {
long daysBetween = interval.toDuration().getStandardDays() + 1;
switch(category) {
case BLOODSUGAR:
BloodSugar bloodSugar = new BloodSugar();
bloodSugar.setMgDl(function(SqlFunction.AVG, BloodSugar.Column.MGDL, interval));
return bloodSugar;
case INSULIN:
Insulin insulin = new Insulin();
insulin.setBolus(function(SqlFunction.SUM, Insulin.Column.BOLUS, interval) / daysBetween);
insulin.setBasal(function(SqlFunction.SUM, Insulin.Column.BASAL, interval) / daysBetween);
insulin.setCorrection(function(SqlFunction.SUM, Insulin.Column.CORRECTION, interval) / daysBetween);
return insulin;
case MEAL:
Meal meal = new Meal();
float avg = function(SqlFunction.SUM, Meal.Column.CARBOHYDRATES, interval) / daysBetween;
float foodEatenSum = 0;
List<FoodEaten> foodEatenList = FoodEatenDao.getInstance().getAll(interval);
for (FoodEaten foodEaten : foodEatenList) {
foodEatenSum += foodEaten.getCarbohydrates();
}
avg = avg + (foodEatenSum / daysBetween);
meal.setCarbohydrates(avg);
return meal;
case ACTIVITY:
Activity activity = new Activity();
activity.setMinutes((int) (function(SqlFunction.SUM, Activity.Column.MINUTES, interval) / daysBetween));
return activity;
case HBA1C:
HbA1c hbA1c = new HbA1c();
hbA1c.setPercent(function(SqlFunction.AVG, HbA1c.Column.PERCENT, interval));
return hbA1c;
case WEIGHT:
Weight weight = new Weight();
weight.setKilogram(function(SqlFunction.AVG, Weight.Column.KILOGRAM, interval));
return weight;
case PULSE:
Pulse pulse = new Pulse();
pulse.setFrequency(function(SqlFunction.AVG, Pulse.Column.FREQUENCY, interval));
return pulse;
case PRESSURE:
Pressure pressure = new Pressure();
pressure.setSystolic(function(SqlFunction.AVG, Pressure.Column.SYSTOLIC, interval));
pressure.setDiastolic(function(SqlFunction.AVG, Pressure.Column.DIASTOLIC, interval));
return pressure;
case OXYGEN_SATURATION:
OxygenSaturation oxygenSaturation = new OxygenSaturation();
oxygenSaturation.setPercent(function(SqlFunction.AVG, OxygenSaturation.Column.PERCENT, interval));
return oxygenSaturation;
default:
return null;
}
}
use of com.faltenreich.diaguard.shared.data.database.entity.BloodSugar in project Diaguard by Faltenreich.
the class CalculatorFragment method storeValues.
private void storeValues(float mgDl, float carbohydrates, float bolus, float correction) {
DateTime now = DateTime.now();
Entry entry = new Entry();
entry.setDate(now);
EntryDao.getInstance().createOrUpdate(entry);
BloodSugar bloodSugar = new BloodSugar();
bloodSugar.setMgDl(mgDl);
bloodSugar.setEntry(entry);
MeasurementDao.getInstance(BloodSugar.class).createOrUpdate(bloodSugar);
List<FoodEaten> foodEatenList = new ArrayList<>();
if (carbohydrates > 0) {
FoodInputView foodInputView = getBinding().foodInputView;
foodEatenList.addAll(foodInputView.getFoodEatenList());
meal.setCarbohydrates(foodInputView.getInputCarbohydrates());
meal.setEntry(entry);
MeasurementDao.getInstance(Meal.class).createOrUpdate(meal);
for (FoodEaten foodEaten : foodEatenList) {
if (foodEaten.getAmountInGrams() > 0) {
foodEaten.setMeal(meal);
FoodEatenDao.getInstance().createOrUpdate(foodEaten);
}
}
}
if (bolus > 0 || correction > 0) {
Insulin insulin = new Insulin();
insulin.setBolus(bolus);
insulin.setCorrection(correction);
insulin.setEntry(entry);
MeasurementDao.getInstance(Insulin.class).createOrUpdate(insulin);
}
Events.post(new EntryAddedEvent(entry, null, foodEatenList));
openEntry(entry);
clearInput();
update();
}
use of com.faltenreich.diaguard.shared.data.database.entity.BloodSugar in project Diaguard by Faltenreich.
the class DashboardFragment method updateLatest.
private void updateLatest() {
if (getContext() == null) {
return;
}
if (latestEntry != null) {
latestValueLabel.setTextSize(54);
BloodSugar bloodSugar = (BloodSugar) MeasurementDao.getInstance(BloodSugar.class).getMeasurement(latestEntry);
// Value
latestValueLabel.setText(bloodSugar.toString());
// Highlighting
if (PreferenceStore.getInstance().limitsAreHighlighted()) {
if (bloodSugar.getMgDl() > PreferenceStore.getInstance().getLimitHyperglycemia()) {
latestValueLabel.setTextColor(ContextCompat.getColor(getContext(), R.color.red));
} else if (bloodSugar.getMgDl() < PreferenceStore.getInstance().getLimitHypoglycemia()) {
latestValueLabel.setTextColor(ContextCompat.getColor(getContext(), R.color.blue));
} else {
latestValueLabel.setTextColor(ContextCompat.getColor(getContext(), R.color.green));
}
}
// Time
latestTimeLabel.setText(String.format("%s %s - ", Helper.getDateFormat().print(latestEntry.getDate()), Helper.getTimeFormat().print(latestEntry.getDate())));
int differenceInMinutes = Minutes.minutesBetween(latestEntry.getDate(), new DateTime()).getMinutes();
// Highlight if last measurement is more than eight hours ago
latestAgoLabel.setTextColor(ContextCompat.getColor(getContext(), R.color.green));
if (differenceInMinutes > DateTimeConstants.MINUTES_PER_HOUR * 8) {
latestAgoLabel.setTextColor(ContextCompat.getColor(getContext(), R.color.red));
}
latestAgoLabel.setText(Helper.getTextAgo(getActivity(), differenceInMinutes));
} else {
latestValueLabel.setTextSize(32);
latestValueLabel.setText(R.string.first_visit);
latestValueLabel.setTextColor(ContextCompat.getColor(getContext(), R.color.green));
latestTimeLabel.setText(R.string.first_visit_desc);
latestAgoLabel.setText(null);
latestAgoLabel.setTextColor(ContextCompat.getColor(getContext(), R.color.gray_darker));
}
}
use of com.faltenreich.diaguard.shared.data.database.entity.BloodSugar in project Diaguard by Faltenreich.
the class PdfTimeline method drawChart.
private Point drawChart(PdfPage page, Point position, List<BloodSugar> bloodSugars) throws Exception {
chart.setColor(Color.transparent);
chart.setPosition(position.getX(), position.getY());
float[] coordinates = chart.drawOn(page);
TextLine label = new TextLine(cache.getFontNormal());
label.setColor(Color.gray);
Line line = new Line();
line.setColor(Color.gray);
float chartWidth = chart.getWidth();
float chartHeight = chart.getHeight();
float chartStartX = 0;
float chartEndX = chartStartX + chart.getWidth();
float chartStartY = 0;
float chartEndY = chartStartY + chartHeight;
float contentStartX = getLabelWidth();
float contentStartY = chartStartY + label.getHeight() + PADDING;
float contentEndX = chartEndX;
float contentEndY = chartEndY;
float contentWidth = contentEndX - contentStartX;
float contentHeight = contentEndY - contentStartY;
int xStep = DateTimeConstants.MINUTES_PER_HOUR * HOUR_INTERVAL;
float xMax = DateTimeConstants.MINUTES_PER_DAY;
TextLine header = new TextLine(cache.getFontBold());
header.setText(DateTimeUtils.toWeekDayAndDate(cache.getDateTime()));
header.setPosition(chartStartX, chartStartY + header.getHeight());
header.placeIn(chart);
header.drawOn(page);
// Labels for x axis
int minutes = 0;
while (minutes <= xMax) {
float x = contentStartX + ((float) minutes / xMax) * contentWidth;
label.setText(String.valueOf(minutes / 60));
label.setPosition(x - label.getWidth() / 2, chartStartY + header.getHeight());
label.placeIn(chart);
label.drawOn(page);
line.setStartPoint(x, chartStartY + header.getHeight() + 8);
line.setEndPoint(x, contentEndY);
line.placeIn(chart);
line.drawOn(page);
minutes += xStep;
}
if (showChartForBloodSugar) {
float yMin = 40;
float yMax = 210;
for (BloodSugar bloodSugar : bloodSugars) {
if (bloodSugar.getMgDl() > yMax) {
yMax = bloodSugar.getMgDl();
}
}
int yStep = (int) ((yMax - yMin) / 5);
yStep = Math.round((yStep + 10) / 10) * 10;
// Labels for y axis
int labelValue = yStep;
float labelY;
while ((labelY = contentStartY + contentHeight - ((labelValue / yMax) * contentHeight)) >= contentStartY) {
label.setText(PreferenceStore.getInstance().getMeasurementForUi(Category.BLOODSUGAR, labelValue));
label.setPosition(chartStartX, labelY + (label.getHeight() / 4));
label.placeIn(chart);
label.drawOn(page);
line.setStartPoint(chartStartX + label.getWidth() + PADDING, labelY);
line.setEndPoint(contentEndX, labelY);
line.placeIn(chart);
line.drawOn(page);
labelValue += yStep;
}
Point point = new Point();
point.setFillShape(true);
point.setRadius(POINT_RADIUS);
for (BloodSugar bloodSugar : bloodSugars) {
Entry entry = bloodSugar.getEntry();
float minute = entry.getDate().getMinuteOfDay();
float value = bloodSugar.getMgDl();
float x = contentStartX + ((minute / xMax) * contentWidth);
float y = contentStartY + (contentHeight - (value / yMax) * contentHeight);
point.setPosition(x, y);
int color = Color.black;
if (cache.getConfig().highlightLimits()) {
if (value > PreferenceStore.getInstance().getLimitHyperglycemia()) {
color = cache.getColorHyperglycemia();
} else if (value < PreferenceStore.getInstance().getLimitHypoglycemia()) {
color = cache.getColorHypoglycemia();
}
}
point.setColor(color);
point.placeIn(chart);
point.drawOn(page);
}
}
return new Point(position.getX(), coordinates[1]);
}
use of com.faltenreich.diaguard.shared.data.database.entity.BloodSugar in project Diaguard by Faltenreich.
the class TestDataImport method importData.
@Override
public void importData() {
for (int count = 0; count < DATA_COUNT; count++) {
DateTime dateTime = DateTime.now().minusHours(count);
Entry entry = new Entry();
entry.setDate(dateTime);
EntryDao.getInstance().createOrUpdate(entry);
BloodSugar bloodSugar = new BloodSugar();
bloodSugar.setMgDl(120);
bloodSugar.setEntry(entry);
MeasurementDao.getInstance(BloodSugar.class).createOrUpdate(bloodSugar);
Log.d(TAG, "Created test data: " + (count + 1) + "/" + DATA_COUNT);
}
}
Aggregations