Search in sources :

Example 21 with Measurement

use of com.faltenreich.diaguard.shared.data.database.entity.Measurement in project Diaguard by Faltenreich.

the class EntryDao method getAverageDataTable.

/**
 * @return HashMap with non-null but zeroed and default values for given categories and time periods
 */
public LinkedHashMap<Category, CategoryValueListItem[]> getAverageDataTable(DateTime day, Category[] categories, int hoursToSkip) {
    if (day == null) {
        return new LinkedHashMap<>();
    }
    int indices = DateTimeConstants.HOURS_PER_DAY / hoursToSkip;
    // Key: Category, Value: Fixed-size array of values per hour-index
    LinkedHashMap<Category, CategoryValueListItem[]> values = new LinkedHashMap<>();
    for (Category category : categories) {
        values.put(category, new CategoryValueListItem[indices]);
    }
    for (Category category : categories) {
        // Key: Hour-index, Value: Values of hour-index
        LinkedHashMap<Integer, List<CategoryValueListItem>> valuesOfHours = new LinkedHashMap<>();
        for (int index = 0; index < indices; index++) {
            valuesOfHours.put(index, new ArrayList<>());
        }
        List<Measurement> measurements = MeasurementDao.getInstance(category.toClass()).getMeasurements(day);
        for (Measurement measurement : measurements) {
            int index = measurement.getEntry().getDate().hourOfDay().get() / hoursToSkip;
            CategoryValueListItem item = new CategoryValueListItem(category);
            switch(category) {
                case INSULIN:
                    Insulin insulin = (Insulin) measurement;
                    item.setValueOne(insulin.getBolus());
                    item.setValueTwo(insulin.getCorrection());
                    item.setValueThree(insulin.getBasal());
                    break;
                case PRESSURE:
                    Pressure pressure = (Pressure) measurement;
                    item.setValueOne(pressure.getSystolic());
                    item.setValueTwo(pressure.getDiastolic());
                    break;
                default:
                    float value = category.stackValues() ? ArrayUtils.sum(measurement.getValues()) : ArrayUtils.avg(measurement.getValues());
                    if (category == Category.MEAL) {
                        for (FoodEaten foodEaten : ((Meal) measurement).getFoodEaten()) {
                            value += foodEaten.getCarbohydrates();
                        }
                    }
                    item.setValueOne(value);
                    break;
            }
            List<CategoryValueListItem> valuesOfHour = valuesOfHours.get(index);
            if (valuesOfHour == null) {
                valuesOfHours.put(index, new ArrayList<>());
            }
            valuesOfHours.get(index).add(item);
        }
        // Average for old values
        for (int index = 0; index < indices; index++) {
            List<CategoryValueListItem> valuesOfHour = valuesOfHours.get(index);
            CategoryValueListItem value = category.stackValues() ? CategoryListItemUtils.sum(category, valuesOfHour) : CategoryListItemUtils.avg(category, valuesOfHour);
            values.get(category)[index] = value;
        }
    }
    return values;
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) Meal(com.faltenreich.diaguard.shared.data.database.entity.Meal) FoodEaten(com.faltenreich.diaguard.shared.data.database.entity.FoodEaten) CategoryValueListItem(com.faltenreich.diaguard.feature.timeline.table.CategoryValueListItem) Insulin(com.faltenreich.diaguard.shared.data.database.entity.Insulin) Pressure(com.faltenreich.diaguard.shared.data.database.entity.Pressure) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) List(java.util.List)

Example 22 with Measurement

use of com.faltenreich.diaguard.shared.data.database.entity.Measurement in project Diaguard by Faltenreich.

the class EntryDao method delete.

@Override
public int delete(Entry entry) {
    for (Measurement measurement : getMeasurements(entry)) {
        entry.getMeasurementCache().add(measurement);
        MeasurementDao.getInstance(measurement.getClass()).delete(measurement);
    }
    EntryTagDao.getInstance().delete(EntryTagDao.getInstance().getAll(entry));
    return super.delete(entry);
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement)

Example 23 with Measurement

use of com.faltenreich.diaguard.shared.data.database.entity.Measurement in project Diaguard by Faltenreich.

the class EntryDao method getMeasurements.

public List<Measurement> getMeasurements(Entry entry, Category[] categories) {
    List<Measurement> measurements = new ArrayList<>();
    for (Category category : categories) {
        Measurement measurement = MeasurementDao.getInstance(category.toClass()).getMeasurement(entry);
        if (measurement != null) {
            measurements.add(measurement);
        }
    }
    Collections.sort(measurements, CategoryComparatorFactory.getInstance().createComparatorFromMeasurements());
    return measurements;
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) ArrayList(java.util.ArrayList)

Example 24 with Measurement

use of com.faltenreich.diaguard.shared.data.database.entity.Measurement in project Diaguard by Faltenreich.

the class BaseFragment method onEvent.

@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(final EntryDeletedEvent event) {
    if (getView() == null) {
        return;
    }
    ViewUtils.showSnackbar(getView(), getString(R.string.entry_deleted), v -> {
        Entry entry = event.context;
        EntryDao.getInstance().createOrUpdate(entry);
        for (Measurement measurement : entry.getMeasurementCache()) {
            measurement.setEntry(entry);
            // noinspection unchecked
            MeasurementDao.getInstance(measurement.getClass()).createOrUpdate(measurement);
        }
        for (EntryTag entryTag : event.entryTags) {
            entryTag.setEntry(entry);
            EntryTagDao.getInstance().createOrUpdate(entryTag);
        }
        Events.post(new EntryAddedEvent(entry, event.entryTags, event.foodEatenList));
    });
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) EntryAddedEvent(com.faltenreich.diaguard.shared.event.data.EntryAddedEvent) Subscribe(org.greenrobot.eventbus.Subscribe)

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