use of com.faltenreich.diaguard.shared.data.database.entity.Measurement in project Diaguard by Faltenreich.
the class DayChartData method createEntries.
private void createEntries() {
if (values.size() > 0) {
for (Measurement value : values) {
int xValue = value.getEntry().getDate().getMinuteOfDay();
float yValue = ArrayUtils.sum(value.getValues());
yValue = PreferenceStore.getInstance().formatDefaultToCustomUnit(Category.BLOODSUGAR, yValue);
Entry chartEntry = new Entry(xValue, yValue, value.getEntry());
addEntry(chartEntry);
}
} else {
// Add fake entry to display empty chart
addEntry(new Entry(-1, 0));
}
}
use of com.faltenreich.diaguard.shared.data.database.entity.Measurement in project Diaguard by Faltenreich.
the class StatisticFragment method invalidateAverage.
private void invalidateAverage() {
Interval interval = timeSpan.getInterval(DateTime.now(), -1);
long days = interval.toDuration().getStandardDays();
getBinding().categoryImageView.setImageResource(category.getIconImageResourceId());
Measurement avgMeasurement = MeasurementDao.getInstance(category.toClass()).getAvgMeasurement(category, interval);
getBinding().averageUnitLabel.setText(category.stackValues() ? String.format("%s %s", PreferenceStore.getInstance().getUnitName(category), getString(R.string.per_day)) : PreferenceStore.getInstance().getUnitName(category));
getBinding().averageValueLabel.setText(avgMeasurement.toString());
long count = EntryDao.getInstance().count(category, interval.getStart(), interval.getEnd());
float avgCountPerDay = (float) count / (float) days;
getBinding().measurementCountAverageLabel.setText(FloatUtils.parseFloat(avgCountPerDay));
if (category == Category.BLOODSUGAR) {
getBinding().hypergylcemiaCountAverageLayout.setVisibility(View.VISIBLE);
getBinding().hypogylcemiaCountAverageLayout.setVisibility(View.VISIBLE);
long hyperCount = EntryDao.getInstance().countAbove(interval.getStart(), interval.getEnd(), PreferenceStore.getInstance().getLimitHyperglycemia());
long hypoCount = EntryDao.getInstance().countBelow(interval.getStart(), interval.getEnd(), PreferenceStore.getInstance().getLimitHypoglycemia());
float avgHypersPerDay = (float) hyperCount / (float) days;
float avgHyposPerDay = (float) hypoCount / (float) days;
getBinding().hypergylcemiaCountAverageLabel.setText(FloatUtils.parseFloat(avgHypersPerDay));
getBinding().hypogylcemiaCountAverageLabel.setText(FloatUtils.parseFloat(avgHyposPerDay));
} else {
getBinding().hypergylcemiaCountAverageLayout.setVisibility(View.GONE);
getBinding().hypogylcemiaCountAverageLayout.setVisibility(View.GONE);
}
}
use of com.faltenreich.diaguard.shared.data.database.entity.Measurement in project Diaguard by Faltenreich.
the class EntryEditFragment method addCategory.
private void addCategory(Category category, boolean atStart) {
Measurement measurement = ObjectFactory.createFromClass(category.toClass());
addMeasurement(measurement, atStart);
Entry entry = viewModel.getEntry();
int indexInCache = entry.indexInMeasurementCache(category);
if (indexInCache != -1) {
entry.getMeasurementCache().set(indexInCache, measurement);
} else {
measurement.setEntry(entry);
entry.getMeasurementCache().add(measurement);
}
}
use of com.faltenreich.diaguard.shared.data.database.entity.Measurement in project Diaguard by Faltenreich.
the class EntryEditFragment method submit.
private void submit() {
Entry entry = viewModel.getEntry();
List<Measurement> measurements = viewModel.getMeasurements();
entry.setMeasurementCache(measurements);
boolean isNewEntry = !entry.isPersisted();
entry = EntryDao.getInstance().createOrUpdate(entry);
for (Measurement measurement : EntryDao.getInstance().getMeasurements(entry)) {
boolean isObsolete = !measurements.contains(measurement);
if (isObsolete) {
MeasurementDao.getInstance(measurement.getClass()).delete(measurement);
}
}
for (Measurement measurement : measurements) {
MeasurementDao.getInstance(measurement.getClass()).createOrUpdate(measurement);
}
List<Tag> tags = new ArrayList<>();
List<EntryTag> entryTags = new ArrayList<>();
for (int index = 0; index < tagListView.getChildCount(); index++) {
View view = tagListView.getChildAt(index);
if (view.getTag() instanceof Tag) {
Tag tag = (Tag) view.getTag();
if (tag.getId() < 0) {
tag = TagDao.getInstance().createOrUpdate(tag);
Tag legacy = TagDao.getInstance().getByName(tag.getName());
if (legacy != null) {
tag.setId(legacy.getId());
}
}
tag.setUpdatedAt(DateTime.now());
tags.add(tag);
EntryTag entryTag = new EntryTag();
entryTag.setEntry(entry);
entryTag.setTag(tag);
entryTags.add(entryTag);
}
}
TagDao.getInstance().bulkCreateOrUpdate(tags);
// TODO: Update instead of delete
EntryTagDao.getInstance().deleteAll(entry);
EntryTagDao.getInstance().bulkCreateOrUpdate(entryTags);
List<FoodEaten> foodEatenList = getFoodEaten();
// Force update in order to synchronize the measurement cache
entry = EntryDao.getInstance().getById(entry.getId());
entry.setMeasurementCache(EntryDao.getInstance().getMeasurements(entry));
if (isNewEntry) {
Toast.makeText(getContext(), getString(R.string.entry_added), Toast.LENGTH_LONG).show();
Events.post(new EntryAddedEvent(entry, entryTags, foodEatenList));
} else {
Events.post(new EntryUpdatedEvent(entry, entryTags, foodEatenList));
}
int alarmInMinutes = viewModel.getAlarmInMinutes();
if (alarmInMinutes > 0) {
AlarmUtils.setAlarm(alarmInMinutes * DateTimeConstants.MILLIS_PER_MINUTE);
}
finish();
}
use of com.faltenreich.diaguard.shared.data.database.entity.Measurement in project Diaguard by Faltenreich.
the class EntryEditFragment method inputIsValid.
private boolean inputIsValid() {
boolean inputIsValid = true;
List<Measurement> measurements = viewModel.getMeasurements();
if (measurements.isEmpty()) {
// Allow entries with no measurements but with a note or tag
if (StringUtils.isBlank(viewModel.getEntry().getNote()) && tagListView.getChildCount() == 0) {
ViewUtils.showSnackbar(root, getString(R.string.validator_value_none));
inputIsValid = false;
}
} else {
for (int index = 0; index < measurementContainer.getChildCount(); index++) {
View view = measurementContainer.getChildAt(index);
if (view instanceof MeasurementView<?>) {
MeasurementView<?> measurementView = (MeasurementView<?>) view;
if (!measurementView.getInputView().isValid()) {
inputIsValid = false;
}
}
}
}
return inputIsValid;
}
Aggregations