Search in sources :

Example 1 with Measurement

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));
    }
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) Entry(com.github.mikephil.charting.data.Entry)

Example 2 with Measurement

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);
    }
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) Interval(org.joda.time.Interval)

Example 3 with Measurement

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);
    }
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry)

Example 4 with 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();
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) ArrayList(java.util.ArrayList) FoodEaten(com.faltenreich.diaguard.shared.data.database.entity.FoodEaten) MeasurementView(com.faltenreich.diaguard.feature.entry.edit.measurement.MeasurementView) NestedScrollView(androidx.core.widget.NestedScrollView) ImageView(android.widget.ImageView) View(android.view.View) ChipView(com.faltenreich.diaguard.shared.view.chip.ChipView) AutoCompleteTextView(android.widget.AutoCompleteTextView) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) EntryUpdatedEvent(com.faltenreich.diaguard.shared.event.data.EntryUpdatedEvent) Tag(com.faltenreich.diaguard.shared.data.database.entity.Tag) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) EntryAddedEvent(com.faltenreich.diaguard.shared.event.data.EntryAddedEvent)

Example 5 with Measurement

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;
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) MeasurementView(com.faltenreich.diaguard.feature.entry.edit.measurement.MeasurementView) MeasurementView(com.faltenreich.diaguard.feature.entry.edit.measurement.MeasurementView) NestedScrollView(androidx.core.widget.NestedScrollView) ImageView(android.widget.ImageView) View(android.view.View) ChipView(com.faltenreich.diaguard.shared.view.chip.ChipView) AutoCompleteTextView(android.widget.AutoCompleteTextView)

Aggregations

Measurement (com.faltenreich.diaguard.shared.data.database.entity.Measurement)24 Entry (com.faltenreich.diaguard.shared.data.database.entity.Entry)15 Category (com.faltenreich.diaguard.shared.data.database.entity.Category)12 ArrayList (java.util.ArrayList)12 EntryTag (com.faltenreich.diaguard.shared.data.database.entity.EntryTag)8 FoodEaten (com.faltenreich.diaguard.shared.data.database.entity.FoodEaten)8 Meal (com.faltenreich.diaguard.shared.data.database.entity.Meal)6 Tag (com.faltenreich.diaguard.shared.data.database.entity.Tag)6 View (android.view.View)4 ImageView (android.widget.ImageView)4 ChipView (com.faltenreich.diaguard.shared.view.chip.ChipView)4 List (java.util.List)4 DateTime (org.joda.time.DateTime)4 AutoCompleteTextView (android.widget.AutoCompleteTextView)3 NestedScrollView (androidx.core.widget.NestedScrollView)3 MeasurementView (com.faltenreich.diaguard.feature.entry.edit.measurement.MeasurementView)3 BloodSugar (com.faltenreich.diaguard.shared.data.database.entity.BloodSugar)3 CategoryDeprecated (com.faltenreich.diaguard.shared.data.database.entity.deprecated.CategoryDeprecated)3 Food (com.faltenreich.diaguard.shared.data.database.entity.Food)2 EntryAddedEvent (com.faltenreich.diaguard.shared.event.data.EntryAddedEvent)2