Search in sources :

Example 1 with Insulin

use of com.faltenreich.diaguard.shared.data.database.entity.Insulin 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;
    }
}
Also used : Meal(com.faltenreich.diaguard.shared.data.database.entity.Meal) Pulse(com.faltenreich.diaguard.shared.data.database.entity.Pulse) FoodEaten(com.faltenreich.diaguard.shared.data.database.entity.FoodEaten) Activity(com.faltenreich.diaguard.shared.data.database.entity.Activity) OxygenSaturation(com.faltenreich.diaguard.shared.data.database.entity.OxygenSaturation) BloodSugar(com.faltenreich.diaguard.shared.data.database.entity.BloodSugar) Insulin(com.faltenreich.diaguard.shared.data.database.entity.Insulin) Pressure(com.faltenreich.diaguard.shared.data.database.entity.Pressure) Weight(com.faltenreich.diaguard.shared.data.database.entity.Weight) HbA1c(com.faltenreich.diaguard.shared.data.database.entity.HbA1c)

Example 2 with Insulin

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

the class MeasurementView method init.

private void init() {
    binding = ListItemMeasurementBinding.inflate(LayoutInflater.from(getContext()), this);
    View inputView;
    Category category = measurement.getCategory();
    switch(category) {
        case INSULIN:
            inputView = new InsulinInputView(getContext(), (Insulin) measurement);
            break;
        case MEAL:
            inputView = new MealInputView(getContext(), (Meal) measurement);
            break;
        case PRESSURE:
            inputView = new PressureInputView(getContext(), (Pressure) measurement);
            break;
        default:
            inputView = new GenericInputView<>(getContext(), category.toClass(), measurement);
    }
    // noinspection unchecked
    this.inputView = (MeasurementInputView<?, T>) inputView;
}
Also used : PressureInputView(com.faltenreich.diaguard.feature.entry.edit.input.PressureInputView) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) InsulinInputView(com.faltenreich.diaguard.feature.entry.edit.input.InsulinInputView) Meal(com.faltenreich.diaguard.shared.data.database.entity.Meal) GenericInputView(com.faltenreich.diaguard.feature.entry.edit.input.GenericInputView) PressureInputView(com.faltenreich.diaguard.feature.entry.edit.input.PressureInputView) ImageView(android.widget.ImageView) InsulinInputView(com.faltenreich.diaguard.feature.entry.edit.input.InsulinInputView) View(android.view.View) CardView(androidx.cardview.widget.CardView) MeasurementInputView(com.faltenreich.diaguard.feature.entry.edit.input.MeasurementInputView) MealInputView(com.faltenreich.diaguard.feature.entry.edit.input.MealInputView) TextView(android.widget.TextView) Insulin(com.faltenreich.diaguard.shared.data.database.entity.Insulin) Pressure(com.faltenreich.diaguard.shared.data.database.entity.Pressure) MealInputView(com.faltenreich.diaguard.feature.entry.edit.input.MealInputView)

Example 3 with Insulin

use of com.faltenreich.diaguard.shared.data.database.entity.Insulin 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();
}
Also used : Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) FoodInputView(com.faltenreich.diaguard.feature.food.input.FoodInputView) Meal(com.faltenreich.diaguard.shared.data.database.entity.Meal) FoodEaten(com.faltenreich.diaguard.shared.data.database.entity.FoodEaten) ArrayList(java.util.ArrayList) EntryAddedEvent(com.faltenreich.diaguard.shared.event.data.EntryAddedEvent) BloodSugar(com.faltenreich.diaguard.shared.data.database.entity.BloodSugar) Insulin(com.faltenreich.diaguard.shared.data.database.entity.Insulin) DateTime(org.joda.time.DateTime)

Example 4 with Insulin

use of com.faltenreich.diaguard.shared.data.database.entity.Insulin 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 5 with Insulin

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

the class CalculatorFragment method showResult.

// Values are normalized
private void showResult(String formula, String formulaContent, final float bloodSugar, final float meal, final float bolus, final float correction) {
    float insulin = bolus + correction;
    // Build AlertDialog
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    final View viewPopup = inflater.inflate(R.layout.dialog_calculator_result, null);
    final ViewGroup infoLayout = viewPopup.findViewById(R.id.dialog_calculator_result_info);
    TextView textViewFormula = viewPopup.findViewById(R.id.dialog_calculator_result_formula);
    textViewFormula.setText(formula);
    TextView textViewFormulaContent = viewPopup.findViewById(R.id.dialog_calculator_result_formula_content);
    textViewFormulaContent.setText(formulaContent);
    // Handle negative insulin
    TextView textViewInfo = viewPopup.findViewById(R.id.textViewInfo);
    if (insulin <= 0) {
        // Advice skipping bolus
        viewPopup.findViewById(R.id.result).setVisibility(View.GONE);
        textViewInfo.setVisibility(View.VISIBLE);
        if (insulin < -1) {
            // Advice consuming carbohydrates
            textViewInfo.setText(String.format("%s %s", textViewInfo.getText().toString(), getString(R.string.bolus_no2)));
        }
    } else {
        viewPopup.findViewById(R.id.result).setVisibility(View.VISIBLE);
        textViewInfo.setVisibility(View.GONE);
    }
    TextView textViewValue = viewPopup.findViewById(R.id.textViewResult);
    textViewValue.setText(FloatUtils.parseFloat(insulin));
    TextView textViewUnit = viewPopup.findViewById(R.id.textViewUnit);
    textViewUnit.setText(PreferenceStore.getInstance().getUnitAcronym(Category.INSULIN));
    dialogBuilder.setView(viewPopup).setTitle(R.string.bolus).setNegativeButton(R.string.info, (dialog, id) -> {
    /* Set down below */
    }).setPositiveButton(R.string.store_values, (dialog, id) -> storeValues(bloodSugar, meal, bolus, correction)).setNeutralButton(R.string.back, (dialog, id) -> dialog.cancel());
    AlertDialog dialog = dialogBuilder.create();
    dialog.setCanceledOnTouchOutside(true);
    dialog.show();
    dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(view -> {
        infoLayout.setVisibility(View.VISIBLE);
        view.setEnabled(false);
    });
}
Also used : AlertDialog(android.app.AlertDialog) Bundle(android.os.Bundle) FloatUtils(com.faltenreich.diaguard.shared.data.primitive.FloatUtils) FoodSearchEvent(com.faltenreich.diaguard.shared.event.ui.FoodSearchEvent) MainButton(com.faltenreich.diaguard.feature.navigation.MainButton) NonNull(androidx.annotation.NonNull) PreferenceStore(com.faltenreich.diaguard.feature.preference.data.PreferenceStore) FoodSearchFragment(com.faltenreich.diaguard.feature.food.search.FoodSearchFragment) Meal(com.faltenreich.diaguard.shared.data.database.entity.Meal) ArrayList(java.util.ArrayList) FactorChangedEvent(com.faltenreich.diaguard.shared.event.preference.FactorChangedEvent) EntryAddedEvent(com.faltenreich.diaguard.shared.event.data.EntryAddedEvent) FragmentCalculatorBinding(com.faltenreich.diaguard.databinding.FragmentCalculatorBinding) ToolbarDescribing(com.faltenreich.diaguard.feature.navigation.ToolbarDescribing) Events(com.faltenreich.diaguard.shared.event.Events) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) BloodSugar(com.faltenreich.diaguard.shared.data.database.entity.BloodSugar) View(android.view.View) Validator(com.faltenreich.diaguard.shared.data.validation.Validator) EntryEditFragment(com.faltenreich.diaguard.feature.entry.edit.EntryEditFragment) FoodEatenDao(com.faltenreich.diaguard.shared.data.database.dao.FoodEatenDao) FoodEaten(com.faltenreich.diaguard.shared.data.database.entity.FoodEaten) FoodInputView(com.faltenreich.diaguard.feature.food.input.FoodInputView) Insulin(com.faltenreich.diaguard.shared.data.database.entity.Insulin) BaseFragment(com.faltenreich.diaguard.shared.view.fragment.BaseFragment) EntryDao(com.faltenreich.diaguard.shared.data.database.dao.EntryDao) LayoutInflater(android.view.LayoutInflater) UnitChangedEvent(com.faltenreich.diaguard.shared.event.preference.UnitChangedEvent) DateTime(org.joda.time.DateTime) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) StickyHintInputView(com.faltenreich.diaguard.shared.view.edittext.StickyHintInputView) MainButtonProperties(com.faltenreich.diaguard.feature.navigation.MainButtonProperties) BloodSugarPreferenceChangedEvent(com.faltenreich.diaguard.shared.event.preference.BloodSugarPreferenceChangedEvent) ThreadMode(org.greenrobot.eventbus.ThreadMode) ViewGroup(android.view.ViewGroup) AlertDialog(android.app.AlertDialog) R(com.faltenreich.diaguard.R) List(java.util.List) TextView(android.widget.TextView) Nullable(androidx.annotation.Nullable) Subscribe(org.greenrobot.eventbus.Subscribe) MeasurementDao(com.faltenreich.diaguard.shared.data.database.dao.MeasurementDao) ToolbarProperties(com.faltenreich.diaguard.feature.navigation.ToolbarProperties) MealFactorUnitChangedEvent(com.faltenreich.diaguard.shared.event.preference.MealFactorUnitChangedEvent) ViewGroup(android.view.ViewGroup) LayoutInflater(android.view.LayoutInflater) TextView(android.widget.TextView) View(android.view.View) FoodInputView(com.faltenreich.diaguard.feature.food.input.FoodInputView) StickyHintInputView(com.faltenreich.diaguard.shared.view.edittext.StickyHintInputView) TextView(android.widget.TextView)

Aggregations

Insulin (com.faltenreich.diaguard.shared.data.database.entity.Insulin)5 Meal (com.faltenreich.diaguard.shared.data.database.entity.Meal)5 FoodEaten (com.faltenreich.diaguard.shared.data.database.entity.FoodEaten)4 BloodSugar (com.faltenreich.diaguard.shared.data.database.entity.BloodSugar)3 Category (com.faltenreich.diaguard.shared.data.database.entity.Category)3 Pressure (com.faltenreich.diaguard.shared.data.database.entity.Pressure)3 ArrayList (java.util.ArrayList)3 View (android.view.View)2 TextView (android.widget.TextView)2 FoodInputView (com.faltenreich.diaguard.feature.food.input.FoodInputView)2 Entry (com.faltenreich.diaguard.shared.data.database.entity.Entry)2 EntryAddedEvent (com.faltenreich.diaguard.shared.event.data.EntryAddedEvent)2 List (java.util.List)2 AlertDialog (android.app.AlertDialog)1 Bundle (android.os.Bundle)1 LayoutInflater (android.view.LayoutInflater)1 ViewGroup (android.view.ViewGroup)1 ImageView (android.widget.ImageView)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1